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

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 6 月 19 日
編集済み: Azzi Abdelmalek 2016 年 6 月 19 日
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)

1 件のコメント

Farah Nabilah Samsudin
Farah Nabilah Samsudin 2016 年 6 月 19 日
I already try it but the graph still resulted in dots. Here the attachment..

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by