How to plot two functions by using looping values in the same graph using Matlab?

I try to plot two function on the same graph from looping but all I get are just dots on the graph. I want it to be a smooth line graph. So how can I connect the dots? Please help.. this is my coding:
r=@(t) -0.5*(t.^4)+4*(t.^3)-10*(t.^2)+8.5*t+1;
f=@(x) -2*(x.^3)+12*(x.^2)-20*x+8.5; %Diff of r%
h=0.5;
x0=0;
y0=1;
l=0;
fprintf (' x Ytrue YEuler True Error Percent Relative Error\n')
fprintf ('_______________________________________________________________________________________________________________\n')
fprintf ('\n')
for j=0.0:+0.5:4.0;
m=r(j);
yx=y0+h*l;
l=f(j);
et=m-yx;
er=((m-yx)/m)*100;
y0=yx;
fprintf (' %5.4f %4.4f %4.4f %4.4f %12.4f\n', [j,m,yx,et,er]')
plot(j,m,'-')
hold on
plot(j,yx,'-')
end

Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 19 Jun. 2016
Bearbeitet: Azzi Abdelmalek am 19 Jun. 2016
instead plot, use scatter function
scatter(j,m)
Or store all your values in one array called m with same size as j, and use outside the loop
plot(j,m)

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by