Matlab plot not showing correct values

8 visualizaciones (últimos 30 días)
Takura Nyatsuro
Takura Nyatsuro el 20 de Oct. de 2022
Editada: dpb el 20 de Oct. de 2022
Hi there,
Im trying to plot a graph using a for loop but the correct values are not being plotted. It is currently outputting the
same value 5 times. Any help would be appreciated.
clc;
clear;
hold on;
x=1:5;
grid on;
for i = 1:5
v=0.1553567*(i.^6)-2.0416*(i.^5)+9.1837*(i.^4)-14.829*(i.^3)-1.3703*(i.^2)+32.821*(i)-1.3155
end
plot(x,v,".")

Respuestas (2)

Torsten
Torsten el 20 de Oct. de 2022
v(i)=0.1553567*(i.^6)-2.0416*(i.^5)+9.1837*(i.^4)-14.829*(i.^3)-1.3703*(i.^2)+32.821*(i)-1.3155
instead of
v=0.1553567*(i.^6)-2.0416*(i.^5)+9.1837*(i.^4)-14.829*(i.^3)-1.3703*(i.^2)+32.821*(i)-1.3155

dpb
dpb el 20 de Oct. de 2022
Editada: dpb el 20 de Oct. de 2022
No need for and don't use loops here...
p=[0.1553567,-2.0416,9.1837,-14.829,-1.3703,32.821,-1.3155]; % store poly coefficients as vector
x=1:5; % x range desired
y=polyval(p,x); % calculate y(p(x)) at x
plot(x,y,'*')
grid on;
Your problem above is you're calculating on value at a time in the loop, but not saving in an array and not plotting until after the loop has finished. Hence, the value is always the last iteration through the loop; you've written over the previous loop value each time the loop executes.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by