calling variable in ODE45 solver

1 view (last 30 days)
Sanjay
Sanjay on 25 Feb 2014
Answered: Walter Roberson on 25 Feb 2014
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.

Answers (1)

Walter Roberson
Walter Roberson on 25 Feb 2014
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.

Community Treasure Hunt

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

Start Hunting!