How do I use the results of the polyfit command for the rest of my code?

Depending on a single variable 'x', I get a 2nd degree polynomial equation.I needed the values of the coefficients of this polynomial equation. I got those values using the 'polyfit' command. Now i need to use these values in the rest of the procedure. I think this procedure can be automated. How do i use the output of the 'polyfit' command, as input for the rest of the procedure?

Answers (1)

Useully you would like to use the parameters of the fit to obtained fitted values. Something along these lines, perhaps?
x = 1:10
y = 2 * x - 8
ynoise = y + randn(size(y)) % y-values with noise
p = polyfit(x,ynoise,1)
yfit = polyval(p,x) % fitted Y values
plot(x,y,'bo',x,ynoise,'rs', x,yfit,'r.-')
legend({'real','noisy','fitted'})
res = yfit - y % residuals

This question is closed.

Asked:

Nik
on 3 Jul 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!