how to modify a fitted model

5 views (last 30 days)
matteo bottoni
matteo bottoni on 8 Sep 2020
Answered: Alan Stevens on 8 Sep 2020
I would like to have better results in terms of graphs of polyfit. Thus, I would like that
  • the fit line will appear countinuos
  • to put the legend of x1=1 year and x2=2°year
  • a better confidence interval (any tips?)
  • more numbers discrimination on x axes
Furthermore I have only a number that I have to add in the end. For this one there is no a data about the age, because it's a mean and it's about adults (so the months shall be more and more) I would like to understand how can I add this point, with also another color and another legend.
Note that the degree of fredom are 6 and 4 to avoid some incorrection
Below the code and the result. I have more and more graphs to do, but with the same format, so I am grateful for your help
fig1=figure
x1 = Toddlers_Matrix(1:25,1);
y1 = Toddlers_Matrix(1:25,6);
[p,S] = polyfit(x1,y1,6);
[y1_fit,delta] = polyval(p,x1,S);
plot(x1,y1,'bo')
hold on
plot(x1,y1_fit,'r-')
plot(x1,y1_fit+2*delta,'m--',x1,y1_fit-2*delta,'m--')
hold on
x2 = Toddlers_Matrix(26:end,1);
y2 = Toddlers_Matrix(26:end,6);
[p,S] = polyfit(x2,y2,4);
[y2_fit,delta] = polyval(p,x2,S);
plot(x2,y2,'go')
hold on
plot(x2,y2_fit,'r-')
plot(x2,y2_fit+2*delta,'m--',x2,y2_fit-2*delta,'m--')
ylabel ST
xlabel Age[months]
legend('Data','Linear Fit','95% Prediction Interval')

Answers (1)

Alan Stevens
Alan Stevens on 8 Sep 2020
1. To get continuous fit lines, Instead of
plot(x1,y1_fit,'r-')
do something like
x = min(x1):max(x1);
y = polyval(p,x);
plot(x,y,'r--')
2. Have you tried fitting all the data (from 1 to 24) as a single set, using a lower order polynomial. Looks almost quadratic or cubic taken as a whole.
3. You can use the set command to set the MarkerIndices of the x-axis.

Tags

Community Treasure Hunt

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

Start Hunting!