Polynomial Curve Fitting

13 views (last 30 days)
Stephen Shortridge
Stephen Shortridge on 29 Mar 2011
For my introductory MATLAB programming class, I need to create a function that fits an n'th order polynomial curve to given data using least squares method.
[fx,a] = curvepoly (x,y,n)
fx=y-axis values on the curve-fit corresponding to the cc-values
a=the polynomial coefficients found
x=[-4. -3.5 -3. -2.5 -2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. 2.5]
y=[0.0013 0.0026 0.0052 0.0106 0.0213 0.0429 0.0863 0.1739 0.35 0.7048 1.4193 2.8582 5.7556 11.5904]
n=order of polynomial desired
Please use the most basic syntax possible Thanks

Answers (1)

Walter Roberson
Walter Roberson on 29 Mar 2011
One "most basic syntax" function coming up:
function [fx,a]=curvefit(x,y,n)
a=polyfit(x,y,n)
fx=polyval(a,x)
  2 Comments
Stephen Shortridge
Stephen Shortridge on 29 Mar 2011
I wish I could use that but I need to write a code that does what polyfit does.
Walter Roberson
Walter Roberson on 29 Mar 2011
function [fx,a]=curvefit(x,y,n)
x=x(:);
for K=0:n-1
b(:,n-K)=x.^K;
end
a=b\y;
fx=polyval(a,x);

Sign in to comment.

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!