Plotting of a prediction line over scatterplot

Hello, I am trying to run this program and plot the prediction line "y" over the scatterplot of age and ulna data points. However, the prediction line results with "doubling back" sections. Any suggestions as to fixing this so that the prediction line is smooth?
%Program Hollingrsquaredulna.m
%Load data
load ulnaestimation.txt;
data = ulnaestimation;
age = data(:,1);
ulna = data(:,2);
%Set parameters
a = 24.18358006;
b = 1.214523208;
K = 13.94615385;
y = (((K-b)*(age.^2))./((a.^2)+(age.^2)))+b;
%Compute RSS
residuals = sqrt(ulna) - sqrt(y);
squareresiduals = residuals.^2;
RSS = sum(squareresiduals);
%Compute denominator
residuald = sqrt(ulna) - mean(sqrt(ulna));
squareresiduald = residuald.^2;
denominator = sum(squareresiduald);
%Compute rsquared
rsquared = 1 - (RSS/denominator)
%Set parameters and compute AIC
q = length(age);
variance = RSS/q;
k = 3;
AIC = (q*log(variance))+2*k
scatter(age,ulna)
hold on
plot(age,y)
Any help is greatly appreciated!!

 Accepted Answer

dpb
dpb on 21 Oct 2015
Edited: dpb on 21 Oct 2015
Sort age before computing y(*); it must be they're not in order so the line follows their sequence in order on the x axis.
() You can, of course, *sort after computing y, saving the order vector optional output and use it to sort the associated y but might as well use the simpler expedient it would seem. If they're already sorted, then the yhat will be those associated with them automagically in the same order (a fairly obvious observation one would presume).

More Answers (0)

Asked:

on 21 Oct 2015

Edited:

dpb
on 21 Oct 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!