How to use a global variable when using ode45()?
I have a function named "my_carcontrol3" that gets passed the time "tau" in the ode45() function call. My idea was to use a global variable "i" as an index for "timesV", which is vector containing a series of time instants. I want "i" to increment by 1 when "tau" hits "timesV(i)." So in the my_carcontrol3(), for 0<t<100, i=1. Once "t" hits 100, it increments by 1. And for 100<t<460, i=2. And when "t" hits 460, it increments again and so on. But for whatever reason, the if conditional statement never got triggered in my_carcontrol3().
This is my main function:
z0 = [ 0 0 0];
t0 = 0;
tf = 1000;
timesV=[100,460,720];
global i;
i=1;
[ t1, y1 ] = ode45( @(tau, y1 ) my_carmodel3( tau, y1, @( tau ) my_carcontrol3( tau,timesV,y1(2))), [ t0 tf ], z0 );
This is my_carcontrol3():
function [ control ] = my_carcontrol3( t,timesV,v)
global i;
if t==timesV(i)
i=i+1;
end
control=1
my_carmodel3():
function dydt = my_carmodel3 (t, y, control)
m=1400;
dydt = zeros( 3, 1 );
c = control( t );
dydt( 1 ) = y( 2 );
dydt( 2 ) = c/m;I have a function named "my_carcontrol3" that gets passed the time "tau" in the ode45() function call. My idea was to use a global variable "i" as an index for "timesV", which is vector containing a series of time instants. I want "i" to increment by 1 when "tau" hits "timesV(i)." So in the my_carcontrol3(), for 0<t<100, i=1. Once "t" hits 100, it increments by 1. And for 100<t<460, i=2. And when "t" hits 460, it increments again and so on. But for whatever reason, the if conditional statement never got triggered in my_carcontrol3().
This is my main function:
z0 = [ 0 0 0];
t0 = 0;
tf = 1000;
timesV=[100,460,720];
global i;
i=1;
[ t1, y1 ] = ode45( @(tau, y1 ) my_carmodel3( tau, y1, @( tau ) my_carcontrol3( tau,timesV,y1(2))), [ t0 tf ], z0 );
This is my_carcontrol3():
function [ control ] = my_carcontrol3( t,timesV,v)
global i;
if t==timesV(i)
i=i+1;
end
control=1
my_carmodel3():
function dydt = my_carmodel3 (t, y, control)
m=1400;
dydt = zeros( 3, 1 );
c = control( t );
dydt( 1 ) = y( 2 );
dydt( 2 ) = c/m; I have a function named "my_carcontrol3" that gets passed the time "tau" in the ode45() function call. My idea was to use a global variable "i" as an index for "timesV", which is vector containing a series of time instants. I want "i" to increment by 1 when "tau" hits "timesV(i)." So in the my_carcontrol3(), for 0<t<100, i=1. Once "t" hits 100, it increments by 1. And for 100<t<460, i=2. And when "t" hits 460, it increments again and so on. But for whatever reason, the if conditional statement never got triggered in my_carcontrol3().
This is my main function:
z0 = [ 0 0 0];
t0 = 0;
tf = 1000;
timesV=[100,460,720];
global i;
i=1;
[ t1, y1 ] = ode45( @(tau, y1 ) my_carmodel3( tau, y1, @( tau ) my_carcontrol3( tau,timesV,y1(2))), [ t0 tf ], z0 );
This is my_carcontrol3():
function [ control ] = my_carcontrol3( t,timesV,v)
global i;
if t==timesV(i)
i=i+1;
end
control=1
my_carmodel3():
function dydt = my_carmodel3 (t, y, control)
m=1400;
dydt = zeros( 3, 1 );
c = control( t );
dydt( 1 ) = y( 2 );
dydt( 2 ) = c/m; ode45, control, matlab, matlab code MATLAB Answers — New Questions