# Plot multiple subintervals in one figure

조회 수: 4 (최근 30일)
Joakim Karlsson 2017년 11월 9일
댓글: Rik 2017년 11월 11일
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 2017년 11월 9일
You did run the plot command another time after the hold on?
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 2017년 11월 10일
편집: Mischa Kim 2017년 11월 10일
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이전 댓글 2개 표시이전 댓글 2개 숨기기
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 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)

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by