Error while passing arguments to events function

712 Views Asked by At

I have a function called initial which takes an argument funname and performs some integration. I wanted to pass multiple arguments to events function. So I did this:

odeopt=odeset('RelTol',1e-5,'AbsTol',1e-5,'Events',@(t,y) events(t,y,prm,funname));
fun=str2func(funname);
[t,y]=ode15s(fun,[0 3600],z,odeopt,prm);

prm is a structure and funname is a string.

This is the events function:

function [value,isterminal,direction] = events(t,y,prm,funname)

isterminal=1;
direction=0;
v=feval(funname,1,y,prm);
value=~all(v<1e-10);

funname is basically the ode function.

It still says too many input arguments.:

??? Error using ==> initial>@(t,y)events(t,y,prm,funname)
Too many input arguments.

Error in ==> odeevents at 29
  eventValue = feval(eventFcn,t0,y0,eventArgs{:});

Error in ==> ode15s at 263
[haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout] = ...

Error in ==> initial at 10
[t,y]=ode15s(fun,[0 3600],z,odeopt,prm);

Can't be a problem of version (was using 7.6) because this post which addresses this issue was on 2006.

Any Idea?

1

There are 1 best solutions below

0
On BEST ANSWER

Since prm is passed as the last argument in ode15s, it will result in the eventArgs cell passing the value to the event handle. In other words, the line

eventValue = feval(eventFcn,t0,y0,eventArgs{:});

is really doing

eventValue = eventFcn(t,y,prm);

So if prm is needed as an extra parameter in the ODE system, just make prm an input in the event handle:

odeopt=odeset(..., @(t,y,prm) events(t,y,prm,funname));

Also, since the events function is design to locate solutions passing through zeros, the value should be a double such that MATLAB's sign function works properly.