Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
passing variables to ode and OutputFcn

Subject: passing variables to ode and OutputFcn

From: Mateusz Gos

Date: 21 Jan, 2009 12:08:01

Message: 1 of 5

I have to pass a number of variables (around 20) to an ODE solver. The syntax I used is as follows (I am not using an anonymous function since my integrated function is too complicated):

ydot = myfun(t,y,v1,v2...vn)
--------
ode45(@myfun,tspan,y0,options,v1,v2...vn)

This would probably (or not?) work, but I am also specifying the OutputFcn using the options of ode45, and once I introduce the above procedure I get the error saying there are too many input parameters to my OutputFcn (?!). It is true I only defined three inputs to OutputFcn which are (t,y,flag), but this should suffice as there are no other outputs from the solver to be passed to OutputFcn. My understanding is that for some reason it tries to read the variables also to the OutputFcn, but I see no justification for this.

Can someone tell me how to solve this problem?

Thanks in advance,
Mateusz

Subject: passing variables to ode and OutputFcn

From: Loren Shure

Date: 21 Jan, 2009 12:32:18

Message: 2 of 5

In article <gl7371$qhi$1@fred.mathworks.com>, webmaster24@wp.pl says...
> I have to pass a number of variables (around 20) to an ODE solver. The syntax I used is as follows (I am not using an anonymous function since my integrated function is too complicated):
>
> ydot = myfun(t,y,v1,v2...vn)
> --------
> ode45(@myfun,tspan,y0,options,v1,v2...vn)
>
> This would probably (or not?) work, but I am also specifying the OutputFcn using the options of ode45, and once I introduce the above procedure I get the error saying there are too many input parameters to my OutputFcn (?!). It is true I only defined three inputs to OutputFcn which are (t,y,flag), but this should suffice as there are no other outputs from the solver to be passed to OutputFcn. My understanding is that for some reason it tries to read the variables also
to the OutputFcn, but I see no justification for this.
>
> Can someone tell me how to solve this problem?
>
> Thanks in advance,
> Mateusz
>
Instead of passing a simple function handle, make an anonymous function
where you use the parameter values myfun needs. Then your output
function won't be burdened with extra inputs.

--
Loren
http://blogs.mathworks.com/loren

Subject: passing variables to ode and OutputFcn

From: Mateusz Gos

Date: 21 Jan, 2009 14:07:02

Message: 3 of 5

Loren Shure <loren@mathworks.com> wrote in message <MPG.23e0dfe199479e1498992d@news.mathworks.com>...
> In article <gl7371$qhi$1@fred.mathworks.com>, webmaster24@wp.pl says...

> Instead of passing a simple function handle, make an anonymous function
> where you use the parameter values myfun needs. Then your output
> function won't be burdened with extra inputs.
>
> --
> Loren
> http://blogs.mathworks.com/loren

Thanks a lot for quick answer, however as I said - the function I am integrating is a long, column vector that is derived in ca. 1,5 thousand lines separate mfile 'myfun'. From the examples I saw anonymous function can be used for far more simple cases. The best solution I can think of is defining these variables as global, but I am not sure how efficient that method would be.

Or perhaps I am missing something?

Subject: passing variables to ode and OutputFcn

From: Steven Lord

Date: 21 Jan, 2009 15:07:42

Message: 4 of 5


"Mateusz Gos" <webmaster24@wp.pl> wrote in message
news:gl7371$qhi$1@fred.mathworks.com...
>I have to pass a number of variables (around 20) to an ODE solver. The
>syntax I used is as follows (I am not using an anonymous function since my
>integrated function is too complicated):
>
> ydot = myfun(t,y,v1,v2...vn)
> --------
> ode45(@myfun,tspan,y0,options,v1,v2...vn)
>
> This would probably (or not?) work, but I am also specifying the OutputFcn
> using the options of ode45, and once I introduce the above procedure I get
> the error saying there are too many input parameters to my OutputFcn (?!).
> It is true I only defined three inputs to OutputFcn which are (t,y,flag),
> but this should suffice as there are no other outputs from the solver to
> be passed to OutputFcn. My understanding is that for some reason it tries
> to read the variables also to the OutputFcn, but I see no justification
> for this.

That's the way that syntax has always worked. If you specify additional
parameters after the options structure in a call to any of the ODE solvers,
those additional parameters are passed into the ODE function (in your case
myfun) as well as any functions defined in the options structure (OutputFcn,
Events, etc.)

In order to solve this, you _could_ modify your OutputFcn to accept these
additional inputs. Instead, though, I'd use Loren's suggestion to create an
anonymous function "adapter" to provide each of ODE45 and your ODE function
the interface they expect.

The myfun function is defined as:

function ydot = myfun(t, y, v1, v2, ... vn)

It expects n+2 input arguments.

The ODE45 function is documented to call its function (in the most basic
syntax) with:

ydot = myfun(t, y)

So, write your anonymous function so it accepts the two inputs that ODE45 is
going to pass it and calls myfun with n+2 input arguments:


% You need to define the v_i's before creating the anonymous function
v1 = 1;
v2 = 2;
v3 = 3;

ode45(@(t, y) myfun(t, y, v1, v2, v3), tspan, y0, options);


Alternately, I'd combine the n additional input arguments into either a
vector, a cell array, or a struct array. It makes it easier to add or
remove a parameter later on; you don't need to change the function to accept
23 input arguments, just change it so it accepts a 23-element vector rather
than a 22-element vector, or a struct with a new field.


additionalParameters = [v1, v2, v3, v4, v5, ...];
ode45(@(t, y) myfun(t, y, additionalParameters), ...

additionalParamsStruct = struct('speed', v1, 'distance', v2, ...)
ode45(@(t, y) myfun(t, y, additionalParamsStruct), ...


--
Steve Lord
slord@mathworks.com

Subject: passing variables to ode and OutputFcn

From: Mateusz Gos

Date: 21 Jan, 2009 15:31:02

Message: 5 of 5

Thank you very much to both of you who have helped me.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us