[英]matlab ode45 how to change a parameter inside the function continiously
i am trying to solve a differential equation using ode45, I have a function in which one of the parameters has to vary by specific step, here is my function: 我正在尝试使用ode45求解微分方程,我有一个函数,其中参数之一必须按特定步骤变化,这是我的函数:
function f=RSJ(y,t,M1,P,M2,E,current)
f=(current/P)-(M1/P)*sin(y)+(M2/P)*sin(y+E);
P
, M1
, M2
& E
are numerical constants, current
is the parameter which I should resolve this differential equation for several cases, for example current=0:1:10
how can I do such a thing? P
, M1
, M2
和E
是数值常数,在某些情况下, current
是我应求解该微分方程的参数,例如current=0:1:10
我该怎么做?
Use a closure (aka anonymous or lambda function): 使用闭包(又名匿名或lambda函数):
% declare t, y, tspan and y0
% [...]
current = 6e-7 : 1e-8 : 8.5e-7;
for k=1:length(current)
f = @(y, t, M1, P, M2, E) (current(k)/P)-(M1/P)*sin(y)+(M2/P)*sin(y+E);
[t{k}, y{k}] = ode45(f, tspan, y0);
end
A quick and dirty solution. 快速而肮脏的解决方案。 Define current
as a global variable (you need to do this in both the base workspace and in the function) and use a for
loop, eg 将current
定义为全局变量(您需要在基本工作区和函数中都这样做)并使用for
循环,例如
current_vector=1e-7*(6:0.1:8.5);
global current
for k=1:length(current_vector)
current = current_vector(k);
[t{k},y{k}]=ode45(f,<tspan>,<y0>)
end
Replace <tspan>
and <y0>
by the appropriate values/variables. 将<tspan>
和<y0>
替换为适当的值/变量。 I assume the other constants are defined in the body of the function, in which case your function should look something like this: 我假设其他常量在函数主体中定义,在这种情况下,您的函数应如下所示:
function f=RSJ(t,y)
global current
M1 = ... % etc...
f=(current/P)-(M1/P)*sin(y)+(M2/P)*sin(y+E);
end
BTW, I don't see any explicit dependence on time t
in your function... 顺便说一句,我在您的函数中没有看到任何对时间t
明确依赖...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.