MATLAB Polynomials in Matlab - Plotting Results

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Matlab Polynomials
AI Thread Summary
The discussion revolves around a MATLAB code snippet for polynomial fitting and plotting. The original code lacked defined x values, which was corrected by providing an array for x. The corrected code successfully fits a cubic polynomial to the given y data and plots both the original data points and the fitted curve. Additional suggestions were made to enhance the plot by adding a title, legend, and axis labels, although the user confirmed these elements were included in the complete version of their code. Overall, the conversation emphasizes the importance of defining all variables in MATLAB for successful data visualization.
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

  • 1.png
    1.png
    16.9 KB · Views: 109
Physics news on Phys.org
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

  • interpolation.PNG
    interpolation.PNG
    5.2 KB · Views: 105
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!
 

Similar threads

Back
Top