This is my finished work buy it just shows the dots not the line, how can I display it?
x=0.5;
n=5;
i=0;
f=0;
while i<n || i==n
f= f+(x.^i)/factorial(i);
i=1+i;
f_o=exp(x);
y=(f_o-f)*100/f_o;
disp(['Term ',num2str(i), ' The error is ',num2str(y,'%.4f'),'%']);
plot(f,y,'*r','LineWidth',1)
hold on
grid on
end
Thank you

 Accepted Answer

One approach is to create two new vectors from ‘f’ and ‘y’ and plot them after the loop:
x=0.5;
n=5;
i=0;
f=0;
while i<n || i==n
f= f+(x.^i)/factorial(i);
i=1+i;
f_o=exp(x);
y=(f_o-f)*100/f_o;
disp(['Term ',num2str(i), ' The error is ',num2str(y,'%.4f'),'%']);
fv(i) = f;
yv(i) = y;
end
figure
plot(fv,yv,'-*r','LineWidth',1)
hold on
grid on
.

More Answers (1)

Star Strider's approach is the one I'd probably use if all I cared about what that the line was plotted, not necessarily when it was plotted.
But if you need each point to appear in its corresponding iteration of the loop, create an animatedline prior to the loop and addpoints to it.
x = 0:360;
y = sind(x);
h = animatedline;
axis([0 360 -1 1])
for whichPoint = 1:numel(x)
addpoints(h, x(whichPoint), y(whichPoint))
end

Categories

Tags

Community Treasure Hunt

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

Start Hunting!