Creating Plots using Matlab For Interpolation

ver_mathstats
Messages
258
Reaction score
21
Homework Statement
We are required to plot y=sqrt(x) on matlab and then interpolate y at 10 evenly spaced points using polyfit and linspace on the interval [0,4].
Relevant Equations
y=sqrt(x), polyfit, linspace
My code in Matlab for this practice question is:
(
x = linspace(0,4,10);
y = sqrt(x);
plot(x,y,'-o');

hold on

y2=polyfit(x,y,2);
plot(x,y2,'--or')
)
Is this the best way to do? My plots look nearly identical and are on top of one another but a later question asks to graph the error, so I am unsure if I messed this up because they look identical. Also I am unsure of what my n value should be for polyfit(x,y,n) function, I believe it should be n+1, but am uncertain if this is correct.
 
Physics news on Phys.org
Overplotting the curves is a bad way to judge errors, which may be on a much smaller scale than the original functions. You might calculate the error and plot it to convince yourself.
The MathWorks documentation of polyfit says that the last parameter should be the degree of the polynomial. So set it accordingly. Nothing in your problem statement indicates what polynomial order is desired.
 
  • Like
Likes ver_mathstats
10 evenly spaced points suggests a polynomial of order 9.

You wouldn't expect a polynomial to be a good fit for \sqrt{x} on [0,4] since it fails to be differentiable at the origin, and polynomials are smooth everywhere.
 
pasmith said:
10 evenly spaced points suggests a polynomial of order 9.
You can get an exact fit to the points if you allow a high-order polynomial, but it may swing wildly between points.
 
FactChecker said:
Overplotting the curves is a bad way to judge errors, which may be on a much smaller scale than the original functions. You might calculate the error and plot it to convince yourself.
The MathWorks documentation of polyfit says that the last parameter should be the degree of the polynomial. So set it accordingly. Nothing in your problem statement indicates what polynomial order is desired.
I know the question does not give what polynomial order is required. And it was a requirement to plot like this however another question asked to change the graph to 100 evenly spaced points with this I see the error much better!
 
ver_mathstats said:
I know the question does not give what polynomial order is required. And it was a requirement to plot like this however another question asked to change the graph to 100 evenly spaced points with this I see the error much better!
That puzzles me. Is that with the same degree polynomial? I don't see how the addition of more points would make the errors so much larger that they are now visible.
 
FactChecker said:
That puzzles me. Is that with the same degree polynomial? I don't see how the addition of more points would make the errors so much larger that they are now visible.
No I changed the degree, I'm probably doing something wrong, I'm still working on the problem
 
ver_mathstats said:
No I changed the degree, I'm probably doing something wrong, I'm still working on the problem
You have to be careful about higher degree polynomials. A large number of coefficients allow it to fit the given data better, but it might also swing wildly in between the given data points.
 
  • Like
Likes ver_mathstats
I think this is a very good assignment...kudos to the teacher!
ver_mathstats said:
Homework Statement:: We are required to plot y=sqrt(x) on MATLAB and then interpolate y at 10 evenly spaced points using polyfit and linspace on the interval [0,4].
Relevant Equations:: y=sqrt(x), polyfit, linspace

Also I am unsure of what my n value should be for polyfit(x,y,n) function, I believe it should be n+1, but am uncertain if this is correct.
This statement makes no sense to me how can n=n+1? Two different n's??
 
  • #10
hutchphd said:
I think this is a very good assignment...kudos to the teacher!
This statement makes no sense to me how can n=n+1? Two different n's??
I think I got it, and I wrote that incorrectly, I just needed to find what degree of polynomial I required which is n-1. Just like pasmith said. I just wrote n+1 because for MATLAB the function is polyfit(x,y,n) when I should've used a different variable. And I believed it to be n+1 because of information I found on MATLAB as I had never used polyfit before.
 
  • Like
Likes hutchphd
  • #11
ver_mathstats said:
I think I got it, and I wrote that incorrectly, I just needed to find what degree of polynomial I required which is n-1.
This confuses me. In polyfit(x,y,iPolyDegree), the third parameter is the positive integer degree of the polynomial that you desire to use. That is an independent decision, not related to the number of data points except that it must be smaller. You have given no hint as to the degree of the polynomial that you want to try. iPolyDegree can be completely different from the number of data points, so all of this n+1 and n-1 makes no sense to me.
 
Back
Top