Filter löschen
Filter löschen

Changing a parameter in a differential equation?

1 Ansicht (letzte 30 Tage)
Sean Thrasher
Sean Thrasher am 27 Jul. 2020
Bearbeitet: David Goodmanson am 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!

Akzeptierte Antwort

David Goodmanson
David Goodmanson am 27 Jul. 2020
Bearbeitet: David Goodmanson am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by