Polynomials in Matlab - Plotting Results

2 replies · 2K views · last reply
ineedhelpnow
Messages
649
Reaction score
0
View attachment 5037

Code:
y = [0.053 1.477 1.970 3.279 4.153 4.934 5.178 5.828 6.082 6.484]; % data
coef = polyfit(x,y,3) 
X=0:.1:9;
Y=polyval(coef,X);
plot(x,y,'o',X,Y)

that's my code. did i do it right?
 
Attachments
  • Two-column table of x and y values for x = 0 to 9 with y from 0.053 to 6.484, followed by printed instructions to fit a third-order polynomial and plot the data and curve.
    1.png
    16.9 KB · Views: 158
You need to define your $x$ values. This should be right:

Code:
y = [0.053 1.477 1.970 3.279 4.153 4.934 5.178 5.828 6.082 6.484]; % data
x = [0 1 2 3 4 5 6 7 8 9];
coef = polyfit(x,y,3) 
X=0:.1:9;
Y=polyval(coef,X);
plot(x,y,'o',X,Y)

Output:

View attachment 5038

You can add a title, legend, and axis labels if you want, using [m]title()[/m], [m]legend()[/m], [m]xlabel()[/m], [m]ylabel()[/m], respectively.
 
Attachments
  • MATLAB figure plotting ten blue circle data points against a red fitted curve; the curve rises steeply then flattens, from about 0 at x=0 to about 6.5 at x=9.
    interpolation.PNG
    5.2 KB · Views: 153
i cut that part of the code out by mistake but i have it in there. i see it's the same graph though. thanks rido!