How do I plot a polynomial equation and set of x-y coordinates on the same graph?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Deleted User
el 10 de Mayo de 2024
Editada: John Kelly
el 20 de Mayo de 2024
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];
0 comentarios
Respuesta aceptada
Sam Chak
el 10 de Mayo de 2024
Editada: Sam Chak
el 10 de Mayo de 2024
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)
format long
p = polyfit(x, y, numel(x)-1)
%% 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 comentarios
Sam Chak
el 11 de Mayo de 2024
Editada: John Kelly
el 20 de Mayo de 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)
%% 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)
%% 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')
Más respuestas (1)
KSSV
el 10 de Mayo de 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')
0 comentarios
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!