Changing a parameter in a differential equation?

4 ビュー (過去 30 日間)
Sean Thrasher
Sean Thrasher 2020 年 7 月 27 日
編集済み: David Goodmanson 2020 年 7 月 27 日
My code is below:
Y0=[0;0;1];
tRange=[0,10000];
[tSol,YSol]=ode45(@quantumsystem,tRange,Y0);
x=YSol(:,1);
y=YSol(:,2);
z=YSol(:,3);
plot(tSol,z,'k')
function dYdt = quantumsystem(t,Y);
x=Y(1);
y=Y(2);
z=Y(3);
gamma=1/2;
epsilon=0.1;
c=1;
dtheta=gamma*c/(t^2 + c^2);
dxdt=(-1/epsilon)*y - dtheta*z;
dydt=(1/epsilon)*x;
dzdt=dtheta*x;
dYdt=[dxdt;dydt;dzdt];
end
---
I wish to create a family of graphs, where the parameter "epsilon" is varied. The only way I know how to do this would be to keep redefining the function dYdt, changing epsilon each time.
I was wondering if there was a quicker way to do this. Ideally, something like
Y0=[0;0;1];
tRange=[0,10000];
[tSol,YSol]=ode45(@quantumsystem,tRange,Y0);
x=YSol(:,1);
y=YSol(:,2);
z=YSol(:,3);
epsilon=0.1
plot(tSol,z,'k')
hold on
epsilon=0.01
function dYdt = quantumsystem(t,Y);
x=Y(1);
y=Y(2);
z=Y(3);
gamma=1/2;
c=1;
dtheta=gamma*c/(t^2 + c^2);
dxdt=(-1/epsilon)*y - dtheta*z;
dydt=(1/epsilon)*x;
dzdt=dtheta*x;
dYdt=[dxdt;dydt;dzdt];
end
except that that gives me an error.
I hope my question is clear.
Any help would be much appreciated. Thank you!

採用された回答

David Goodmanson
David Goodmanson 2020 年 7 月 27 日
編集済み: David Goodmanson 2020 年 7 月 27 日
Hi Sean,
you can pass the value of epsilon into the ode function with
[tSol,YSol]=ode45(@(t,Y) quantumsystem(t,Y,epsilon),tRange,Y0);
and
function dYdt = quantumsystem(t,Y,epsilon);
x=Y(1);
y=Y(2);
z=Y(3);
gamma=1/2;
c=1;
dtheta=gamma*c/(t^2 + c^2);
dxdt=(-1/epsilon)*y - dtheta*z;
dydt=(1/epsilon)*x;
dzdt=dtheta*x;
dYdt=[dxdt;dydt;dzdt];
end
Since ode45 picks the time increments, different values of epsilon may lead to different lengths of tSol and YSol, but the information is there. One of the easier ways to deal with that issue is to set up a for loop with different values of epsilion and use
plot(tSol,z)
hold on
inside the loop as you are doing, only without the color specification.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by