Changing a parameter in a differential equation?
4 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!