0
$\begingroup$

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 have searched around but cannot find the solution. Please help me out.

$\endgroup$
0

2 Answers 2

0
$\begingroup$

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 .

$\endgroup$
1
  • $\begingroup$ Yes, that did the trick. $\endgroup$
    – zynga
    Nov 8, 2014 at 16:49
1
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ flag is not a variable I am allowed to pass. The value of flag is decided by ode45 function at every iteration. $\endgroup$
    – zynga
    Nov 8, 2014 at 16:15
  • $\begingroup$ 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. $\endgroup$ Nov 8, 2014 at 16:20
  • $\begingroup$ 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. $\endgroup$
    – zynga
    Nov 8, 2014 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.