Creating Plots using Matlab For Interpolation

In summary, the conversation is discussing the best way to plot y=sqrt(x) on MATLAB and how to interpolate the data at 10 evenly spaced points using polyfit and linspace. The individual is unsure of what the n value should be for the polyfit function and is asking for clarification. They also mention changing the number of data points and the effect it has on the errors. Ultimately, they are trying to find the appropriate degree of polynomial for their problem.
  • #1
ver_mathstats
260
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
  • #2
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
  • #3
10 evenly spaced points suggests a polynomial of order 9.

You wouldn't expect a polynomial to be a good fit for [itex]\sqrt{x}[/itex] on [0,4] since it fails to be differentiable at the origin, and polynomials are smooth everywhere.
 
  • #4
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.
 
  • #5
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!
 
  • #6
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.
 
  • #7
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
 
  • #8
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
  • #9
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.
 

FAQ: Creating Plots using Matlab For Interpolation

How do I create a plot using Matlab for interpolation?

To create a plot using Matlab for interpolation, you will need to first import your data into the program. Then, use the "plot" function to plot your data points. Next, use the "interp1" function to interpolate between the data points and plot the interpolated curve. Finally, use the "legend" function to label your plot with a title and axis labels.

What is interpolation and why is it important in creating plots?

Interpolation is the process of estimating values between known data points. It is important in creating plots because it allows us to visualize the relationship between data points and to fill in gaps in our data. This can help us make more accurate predictions and understand the behavior of our data.

What are the different types of interpolation methods available in Matlab?

There are several interpolation methods available in Matlab, including linear, cubic, spline, and nearest neighbor. Each method has its own advantages and is suitable for different types of data. It is important to choose the appropriate method for your data to ensure accurate results.

Can I customize the appearance of my plot in Matlab?

Yes, you can customize the appearance of your plot in Matlab by using various functions such as "title", "xlabel", "ylabel", and "grid". You can also change the color, style, and size of the plot lines by specifying parameters in the "plot" function.

How can I save my plot as an image file in Matlab?

To save your plot as an image file in Matlab, you can use the "saveas" function. This function allows you to specify the file format (such as PNG, JPEG, or PDF) and the name of the file. You can also use the "print" function to save your plot as an image file or print it directly from Matlab.

Similar threads

Replies
5
Views
1K
Replies
1
Views
1K
Replies
0
Views
109
Replies
2
Views
3K
Replies
4
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
2
Views
11K
Back
Top