calling variable in ODE45 solver

3 次查看(过去 30 天)
Sanjay
Sanjay 2014-2-25
I am getting a simple problem(may be). I have function like
function dy = rigid(t,y)
p
dy = zeros(3,1); % a column vector
dy(1) = y(2) * y(3);
dy(2) = -y(1) * y(3);
dy(3) = -0.51 * y(1) * y(2);
which i want solve with ode45
global p;
p=1
options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
[T,Y] = ode45(@rigid,[0 12],[0 1 1],options);
plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')
when i run ode45... it got stuck at the value of p in rigid function. Can't we define a variable which is present before ode solver but not in function.

回答(1 个)

Walter Roberson
Walter Roberson 2014-2-25
You need to declare a variable "global" in every routine that you want to use it.
But you should consider instead parameterizing your function.
function dy = rigid(t, y, p)
...
and in the script,
[T,Y] = ode45(@(t,y) rigid(t,y,p) ,[0 12], [0 1 1], options);
You would not need "global" then.

类别

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