Changing a parameter in a differential equation?
3 views (last 30 days)
Show older comments
Sean Thrasher
on 27 Jul 2020
Edited: David Goodmanson
on 27 Jul 2020
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 Comments
Accepted Answer
David Goodmanson
on 27 Jul 2020
Edited: David Goodmanson
on 27 Jul 2020
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 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!