How to plot for different values of parameters on one run?

조회 수: 33 (최근 30일)
parag gupta
parag gupta 2019년 7월 30일
댓글: parag gupta 2019년 8월 2일
How to get the plots for different values of aplha = 2,3,4,5 on one run and how to merge all those graphs ?
function [t,y] = call_dstate()
tspan = [0 9]; % set time interval
y0 = 10; % set initial condition
% dstate evaluates r.h.s. of the ode
[t,y] = ode45( @dstate ,tspan ,y0);
plot(t,y)
disp([t,y]) % displays t and y(t)
function dydt = dstate (t,y)
alpha=2; gamma=0.0001;
dydt = alpha* y-gamma *y^2;
end
end

채택된 답변

David K.
David K. 2019년 7월 30일
Matlab actually has a page on this called Parameterizing Functions.
Using one of the options for that I believe this will do it for you. This works because the nested function can use variables created in the outer function.
function [t,y] = call_dstate()
tspan = [0 9]; % set time interval
y0 = 10; % set initial condition
% dstate evaluates r.h.s. of the ode
gamma=0.0001;
for alpha = 2:5
[t,y] = ode45( @dstate ,tspan ,y0);
plot(t,y); hold on;
end
disp([t,y]) % displays t and y(t)
function dydt = dstate (t,y)
dydt = alpha* y-gamma *y^2;
end
end
  댓글 수: 3
David K.
David K. 2019년 8월 2일
I may be wrong but it looks like you chose a color for the plot to use. So first I would suggest letting it choose a different color for each. Or if you want to do it manually you can create an array to set them yourself like this
color = {'r','m','b','c'}
for alpha = 2:5
[t,y] = ode45( @dstate ,tspan ,y0);
plot(t,y,color{alpha-1}); hold on;
end
Then you can add a legend after they are all plotted like this
for alpha = 2:5
...
end
legend('a=2','a=3','a=4','a=5')
And a legend will show up indicating which color plot corresponds to each alpha
parag gupta
parag gupta 2019년 8월 2일
Thanks david ?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by