Solving ODE with non-constant parameters using ode45

28 次查看(过去 30 天)
Hi everyone,
I need to solve an ODE of the general form dydt = A(t)*y; where A(t) is a function that depends on S as well as a bunch of other parameters. When I implement the solution using ode45, I found that it only works when I define all the parameters that make up A(t) within the dydt function instead of the call function. An example is as below:
function dy = dydt(t,y)
a=...;
b=...;
c=...;
A = some function that depends on a,b,c, and t;
dy = A*y;
end
However, I am trying to make it also work by defining the paramters in the call function outside of the dydt function, since A(t) technically also changes in an iterative manner so it will be a lot easier if I dont have to change the dydt function everytime I enter a new iteration.
I'd like to know if what I am asking is possible in ode45? If not, are there any other ODE solvers in MATLAB that may allow me to do this? Thanks!
  1 个评论
Torsten
Torsten 2021-12-14
编辑:Torsten 2021-12-14
You can pass A(S) to dydS as a function handle that you define in the calling program.
A = @(S) S^2;
Sspan = ...;
y0 = ...;
[S,y] = ode45(@(S,y)dydS(S,y,A),Sspan,y0);
...
function dy = dydS(S,y,A)
dy = A(S)*y;
end
or you can define your complete ODE-system without the function dydS:
A = @(S) S^2;
dydS = @(S,y) A(S)*y;
[S,y] = ode45(dydS,Sspan,y0);

请先登录,再进行评论。

回答(2 个)

John D'Errico
John D'Errico 2021-12-14
编辑:John D'Errico 2021-12-14
Trivial. In fact, there are many ways I could do this.
As an example, i'll define it in terms of three other parameters, S,u and v.
A = @(t,S,u,v) t*S + u - v^2*t;
Now the differential equation...
ODEFUNSuv = @(t,y,S,u,v) A(t,S,u,v);
And the solve. First, I'll create a function handle that knows what S, u and v are...
ODEFUN = @(t,y) ODEFUNSuv(t,y,3,4,5) % So S = 3, u = 4, v = 5
ODEFUN = function_handle with value:
@(t,y)ODEFUNSuv(t,y,3,4,5)
[tout,yout] = ode45(ODEFUN,[0,10],0); %, tspan from 0 to 10, y(0) = 0 as the IC
plot(tout,yout)
We can do an analytical solution as a comparison
syms y(t)
Ysol = dsolve(diff(y) == t*3 + 4 - 5^2*t,y(0) == 0)
Ysol = 
fplot(Ysol,[0,10])
ylim([-1200 200])
So things are the same.
I can now choose any other values for S,u,v, and re-solve the new problem.
S = 1;
u = pi;
v = 0;
ODEFUN = @(t,y) ODEFUNSuv(t,y,S,u,v);
[tout,yout] = ode45(ODEFUN,[0,10],0);
plot(tout,yout)
No issues. ODE45 works fine with the new values. And there are surely other ways I could have done this, but a function handle as I did is by far the easiest.

Steven Lord
Steven Lord 2021-12-14
See the "Pass Extra Parameters to ODE Function" example on the documentation page for the ode45 function. There's also a link to a page that discusses several techniques for parameterizing your functions in the description of the first input argument to ode45 (odefun) on that page.

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by