# passing arguments to ode45 with 'flag'

I want to pass additional parameters to the ode45 function in MATLAB. There are many questions already on this, but I also have a 'flag' argument in my function.

function [xdot,isterminal,dircn]=spp(t,x,flag,f)


Usually, while calling ode45, something like this would work if spp did not have a flag argument.

options=odeset('RelTol',1e-13,'AbsTol',1e-14,'events','on');
[t,x,te,xe,ie]=ode45(@(t,x)spp(t,x,f),[0 2*pi/OMEG],[xin;xdin],options);


I do not exactly know how to treat 'flags' but usually when I need to pass extra variables to the ode I define them as 'global' in the main .m file and also in the file where I define the function I give to the ode.

Or you might use the varargin function .

• Yes, that did the trick. Nov 8 '14 at 16:49

Why don't you simply pass flag the way you are passing f?

[t,x,te,xe,ie]=ode45(@(t,x)spp(t,x,flag,f),[0 2*pi/OMEG],[xin;xdin],options);


(Global variables in MATLAB often cause problems and it is best to avoid them, if possible.)

Maybe I'm missing something here?

• flag is not a variable I am allowed to pass. The value of flag is decided by ode45 function at every iteration. Nov 8 '14 at 16:15
• Are you talking about "flag" in the ode output function? The output function is not the same function as the "odefun." You have not defined an output function. Nov 8 '14 at 16:20
• No, I am not using an output function. I am using flag as an argument to a user defined function, the value of which will be updated by ode45. I think it is allowed. Nov 8 '14 at 16:51