Plot multiple subintervals in one figure

Hello all. Probably quite an easy problem but I am stuck.
f=@(t,u)-u+sin(5*t)+cos(2*t);
a=0; b=5; ua=2; % begynnelsevillkor
t=linspace(a,b,15); u=linspace(-2,2,15);
riktningsfaelt(f,t,u);
N=10; h=(b-a)/N; %steglängd
T=linspace(a,b,N+1); U=zeros(size(T));
U(2)=ua; %enligt begynnelsevillkor
for n=1:N
U(n+1)=U(n)+h*f(T(n),U(n)); % Eulers metod
end
plot(T,U)
So what I want to do is plot but with different N values in the same figure. I have tried "hold on" then set a new N value but it doesn't work. Lets say I want to plot N=10 and N=100 in the same figure, how do I do that??

2 件のコメント

Rik
Rik 2017 年 11 月 9 日
You did run the plot command another time after the hold on?
Joakim Karlsson
Joakim Karlsson 2017 年 11 月 10 日
Yes I did but the script just computed the first thing again. So it didn't really change anything.

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

 採用された回答

Mischa Kim
Mischa Kim 2017 年 11 月 10 日
編集済み: Mischa Kim 2017 年 11 月 10 日

0 投票

Joakim, is this what you had in mind?
f=@(t,u)-u+sin(5*t)+cos(2*t);
a=0; b=5; ua=2; % begynnelsevillkor
t=linspace(a,b,15); u=linspace(-2,2,15);
% riktningsfaelt(f,t,u);
for N = 10:10:100
h=(b-a)/N; %steglängd
T=linspace(a,b,N+1);
U=zeros(size(T));
U(2)=ua; %enligt begynnelsevillkor
for n=1:N
U(n+1)=U(n)+h*f(T(n),U(n)); % Eulers metod
end
plot(T,U)
hold on
end

4 件のコメント

Joakim Karlsson
Joakim Karlsson 2017 年 11 月 10 日
編集済み: Joakim Karlsson 2017 年 11 月 10 日
Hmm not really. I want to set N=10,100,1000 and plot these three different with colors. Since a higher N value smoothes the curve i want to show the Difference what happens when you have a different N value
Mischa Kim
Mischa Kim 2017 年 11 月 10 日
編集済み: Mischa Kim 2017 年 11 月 10 日
Simple enough. Replace the for command by
for N = [10,100,1000]
The code in my first answer above creates essentially the same graph, just with 10 plots vs 3.
Joakim Karlsson
Joakim Karlsson 2017 年 11 月 10 日
Yeah this works very well actually. Now I'd just like to set my own colours if that is possible since its hard to look at it when one is yellow and one is orange haha.
Rik
Rik 2017 年 11 月 11 日
Save the handles to the line objects (output of plot function) and you can set the color. (or just use a cell array with the color string inside the for-loop)

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

その他の回答 (0 件)

カテゴリ

質問済み:

2017 年 11 月 9 日

コメント済み:

Rik
2017 年 11 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by