How do I plot a polynomial equation and set of x-y coordinates on the same graph?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
I am trying to plot a polynmial and some x-y coordinates on the same graph. I need the coordinates to be visible and the lines to be distingushable from one another (eg. different colors).
Here is the measured data.
coefficients = [0.6252 -0.4165 0.1311 -0.0163 0.0009,-0.0000]
x = [2 5 8 11 14 20];
y = [0.20 0.29 0.44 0.50 0.66 0.56];
Accepted Answer
Edit: Your original polynomial coefficients are truncated. So, the polynomial cannot produce an accurate result.
%% data
x = [2 5 8 11 14 20];
y = [0.20 0.29 0.44 0.50 0.66 0.56];
%% polynomial coefficients
coefficients = [0.6252 -0.4165 0.1311 -0.0163 0.0009 -0.0000];
p = fliplr(coefficients)
p = 1x6
0 0.0009 -0.0163 0.1311 -0.4165 0.6252
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
format long
p = polyfit(x, y, numel(x)-1)
p = 1x6
-0.000018175582990 0.000901920438957 -0.016287722908091 0.131052812071305 -0.416499314128809 0.625240054869418
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% points generated by polynomial
xx = linspace(x(1), x(end), 181);
yy = polyval(p, xx);
%% plot results
plot(x, y, 'o', 'markersize', 10), hold on
plot(xx, yy), grid on, xlabel('x'), xlabel('y')
legend('data', 'polynomial', 'location', 'northwest')

9 Comments
On try something like this...
%% data
x = [2 5 8 11 14 20];
y = [0.20 0.29 0.44 0.50 0.66 0.56];
%% polynomial coefficients
p = polyfit(x, y, numel(x)-1)
p = 1x6
-0.0000 0.0009 -0.0163 0.1311 -0.4165 0.6252
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% points generated by polynomial
xx = linspace(x(1), x(end), 181);
yy = polyval(p, xx);
%% plot results
plot(x, y, 'o', 'markersize', 10), hold on
plot(xx, yy), grid on, xlabel('x'), xlabel('y')
legend('data', 'polynomial', 'location', 'northwest')

Deleted User
on 10 May 2024
Edited: Deleted User
on 10 May 2024
Have you done it so that the polynomial coefficients are not truncated or do I need to get the exact form of the coefficients? I will give this a try. Do you know if there is a way to get lines to come out from the axes and stop at the polynomial, so that I can interpolate?
Sam Chak
on 10 May 2024
Edited: John Kelly
on 20 May 2024
By default, MATLAB displays numeric values up to 4 decimals. The values computed by MATLAB are not truncated in the Workspace. However, when you manually copy and paste the displayed values into the 'coefficient' vector, they become truncated. By using 'format long', you can view the displayed precision up to 15 decimals. If you wish to revert back to a 4-decimal numeric display, simply enter 'format short'.
Do you know if there is a way to get lines to come out from the axes and stop at the polynomial, so that I can interpolate?
I'm not quite sure I understand what you mean by "get lines to come out from the axes and stop at the polynomial." However, based on your description, it seems like you might be referring to "Extrapolation," is that correct?
Deleted User
on 10 May 2024
Edited: Deleted User
on 10 May 2024
In the graph plotted, did you use the truncated values or untruncated values?
Is there a way I can copy and paste the values without truncating them or would I have to use format long every time?
The second part is referring to interpolation, which is bascially extrapolation within the known data points instead. I was asking whether there is a way to visually show this on the graph with lines pointing to the specific point. (The first image when you google interpolation in images.)
Sam Chak
on 10 May 2024
Edited: John Kelly
on 20 May 2024
Hi
The plotted polynomial result utilizes the polynomial coefficients computed by the 'polyfit()' command and stored in the 'p' vector (which overwrites the truncated 'p = fliplr(coefficients)').
You mentioned that you want to copy and paste the values without truncating them. Before I provide an answer, could you please let me know where you intend to paste these values after copying them from the system clipboard?
Deleted User
on 10 May 2024
Just in the same file, maybe a couple rows below. I need to use it for multiple other things.
Sam Chak
on 10 May 2024
Edited: John Kelly
on 20 May 2024
Since the polynomial coefficients will be utilized within the same script or function, you can employ the method described below without the need for copying and pasting the displayed values from the Command Window.
%% data
x = [2 5 8 11 14 20];
y = [0.20 0.29 0.44 0.50 0.66 0.56];
%% polynomial coefficients
p = polyfit(x, y, numel(x)-1);
%% recall coefficients without copy/paste the displayed values
p(1)
ans = -1.8176e-05
p(2)
ans = 9.0192e-04
p(3)
ans = -0.0163
p(4)
ans = 0.1311
p(5)
ans = -0.4165
p(6)
ans = 0.6252
Deleted User
on 11 May 2024
Thank you for that! Sorry to bother you, but I have another question. I’m trying to plot another point on the polynomial. I know the x coordinate is 17.5, but I don’t know the y coordinate. Is there a way I could plot the x coordinate on the polynomial and use the information from the graph to figure out the y coordinate? This coordinate needs to be distinguished from the points (eg. different colour).
Sam Chak
on 11 May 2024
Edited: John Kelly
on 20 May 2024
Hi
You can use the polyval() command in this case.
%% data
x = [2 5 8 11 14 20];
y = [0.20 0.29 0.44 0.50 0.66 0.56];
%% polynomial coefficients
p = polyfit(x, y, numel(x)-1)
p = 1x6
-0.0000 0.0009 -0.0163 0.1311 -0.4165 0.6252
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% points generated by polynomial
xx = linspace(x(1), x(end), 181);
yy = polyval(p, xx);
%% find y coordinate with given x coordinate
xpt = 17.5;
ypt = polyval(p, xpt)
ypt = 0.9379
%% plot results
plot(x, y, 'o', 'markersize', 10), hold on
plot(xx, yy), grid on, xlabel('x'), ylabel('y')
plot(xpt, ypt, 'p', 'markersize', 10, 'color', "#7E2F8E"), hold off
legend('data', 'polynomial', 'point', 'location', 'northwest')

More Answers (1)
KSSV
on 10 May 2024
x = [2 5 8 11 14 20];
y = [0.20 0.29 0.44 0.50 0.66 0.56];
p = polyfit(x,y,3) ;
yi = polyval(p,x) ;
figure
hold on
plot(x,y,'r')
plot(x,yi,'b')
legend('data points','polynomial fit')

Categories
Find more on Geographic Plots in Help Center and File Exchange
Products
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)