ODE graph for multiple occurrences
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
My MATLAB code as of right now will graph a ODE for one value of gamma. I would like to write the code so that it graphs 8 plots all on the same graph for varrying values of gamma. Values of gamma will be 0 through 2 with a step of .25. Here is my code currently:
tspan = [0 50];
y0 = [2 0]';
[t,y] = ode45(@hw1,tspan,y0);
plot(t,y(:,1))
function dy = hw1(t,y)
gamma = 1
dy(1)= y(2);
dy(2)= -gamma*y(2)-y(1);
dy = [dy(1);dy(2)];
title('Gamma equals .25')
end
Thank yoU!
0 件のコメント
回答 (1 件)
Star Strider
2021 年 6 月 30 日
Define the γ vector (‘gammav’ here), use a loop, and see the documentation section on Passing Extra Parameters.
tspan = [0 50];
y0 = [2 0]';
gammav = logspace(-1, 1, 5)
for k = 1:numel(gammav)
[t{k},y{k}] = ode45(@(t,y)hw1(t,y,gammav(k)),tspan,y0);
end
figure
hold on
for k = 1:numel(gammav)
plot(t{k},y{k}(:,1))
end
grid
legend(compose('$\\gamma = %.2f$',gammav), 'Location','best', 'Interpreter','latex')
function dy = hw1(t,y,gamma)
dy(1)= y(2);
dy(2)= -gamma*y(2)-y(1);
dy = [dy(1);dy(2)];
end
.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!