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 CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by