Creating Plots using Matlab For Interpolation

Click For Summary

Homework Help Overview

The discussion revolves around creating plots in MATLAB for the function y=sqrt(x) and using polynomial interpolation with the polyfit function. Participants are exploring the implications of polynomial degree selection and the visualization of errors in the context of interpolation.

Discussion Character

  • Exploratory, Assumption checking, Conceptual clarification

Approaches and Questions Raised

  • Participants discuss the appropriateness of using a high-order polynomial for fitting the square root function and question the implications of overplotting for error assessment. There is uncertainty regarding the correct polynomial degree to use with polyfit, with some participants suggesting it should relate to the number of data points.

Discussion Status

The discussion is active, with participants raising questions about polynomial degree and its relationship to the number of data points. Some guidance has been offered regarding the interpretation of the polyfit function and the potential issues with high-degree polynomials. There is no explicit consensus on the best approach yet.

Contextual Notes

Participants note that the homework requires plotting y=sqrt(x) and interpolating at 10 evenly spaced points, but there is confusion about the degree of the polynomial to use, as the problem statement does not specify this. Additionally, some participants mention a subsequent question involving 100 evenly spaced points, which raises further questions about error visibility.

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   Reactions: 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   Reactions: 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   Reactions: 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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
15K
  • · Replies 12 ·
Replies
12
Views
7K