Thread Subject: Passing argument to a function handle (@...)

Subject: Passing argument to a function handle (@...)

From: Merciadri Luca

Date: 17 Feb, 2010 17:09:38

Message: 1 of 4

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

I have a function which defines an ODE system, say, defineODESystem, which
currently, only takes t (the time, as it is the variable) and C (a
vector containing the functions which will be referred to as
derivatives in the future). These variables must be there (and are
currently not important), and I need, to compute the first members of
the eqs., many variables, which should be taken as arguments to
defineODESystem. However, defineODESystem is then called by
[t,y] = ode45(@defineODESystem, ...) to solve the ODE system using Runge-Kutta.

Where can I put the other arguments (those which will help me
computing the first members of the ODE) of defineODESystem in this
call (i.e. for the handle)?

For example, I have

==
function dC = defineODESystem(t, C)

dC(1) = something which involves a, b, c, d, e, f, ...
dC(2) = something which involes a, b, c, d, e, f, ...
.
.
.
==

and defineODESystem needs to compute the second members `something
which involves a, b, c, ...' Consequently, I would have to modify
defineODESystem's signature into

==
function dC = defineODESystem(t, C, a, b, ...)
==

but how would I pass these arguments when executing ode45 on it, via a
handle? The

==
[t,y] = ode45(@defineODESystem(a, b, ...), ...)
==

does not work.

Thanks.
- --
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- --

If you want breakfast in bed, sleep in the kitchen.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAkt8ItIACgkQM0LLzLt8Mhy46ACfQ3qVQ0bE5lYkZcXgopCRDEAo
0EIAnikKGYZojTgupeL9ZvJjuIW6eyp7
=SLVI
-----END PGP SIGNATURE-----

Subject: Passing argument to a function handle (@...)

From: Steven Lord

Date: 17 Feb, 2010 17:53:46

Message: 2 of 4


"Merciadri Luca" <Luca.Merciadri@student.ulg.ac.be> wrote in message
news:873a0zdc19.fsf@merciadriluca-station.MERCIADRILUCA...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello,
>
> I have a function which defines an ODE system, say, defineODESystem, which
> currently, only takes t (the time, as it is the variable) and C (a
> vector containing the functions which will be referred to as
> derivatives in the future). These variables must be there (and are
> currently not important), and I need, to compute the first members of
> the eqs., many variables, which should be taken as arguments to
> defineODESystem. However, defineODESystem is then called by
> [t,y] = ode45(@defineODESystem, ...) to solve the ODE system using
> Runge-Kutta.
>
> Where can I put the other arguments (those which will help me
> computing the first members of the ODE) of defineODESystem in this
> call (i.e. for the handle)?

Use an anonymous function.

% Remember to define C and additionalParameters first
[t, y] = ode45(@(t, y) defineODESystem(t, y, C, additionalParameters), ...)

If you're using a version of MATLAB that predates anonymous functions, look
in the help for the syntax that includes the parameters P1, P2, etc.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Passing argument to a function handle (@...)

From: Merciadri Luca

Date: 17 Feb, 2010 20:25:38

Message: 3 of 4

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Steven Lord" <slord@mathworks.com> writes:

> "Merciadri Luca" <Luca.Merciadri@student.ulg.ac.be> wrote in message
> news:873a0zdc19.fsf@merciadriluca-station.MERCIADRILUCA...
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Hello,
>>
>> I have a function which defines an ODE system, say, defineODESystem, which
>> currently, only takes t (the time, as it is the variable) and C (a
>> vector containing the functions which will be referred to as
>> derivatives in the future). These variables must be there (and are
>> currently not important), and I need, to compute the first members of
>> the eqs., many variables, which should be taken as arguments to
>> defineODESystem. However, defineODESystem is then called by
>> [t,y] = ode45(@defineODESystem, ...) to solve the ODE system using
>> Runge-Kutta.
>>
>> Where can I put the other arguments (those which will help me
>> computing the first members of the ODE) of defineODESystem in this
>> call (i.e. for the handle)?
>
> Use an anonymous function.
>
> % Remember to define C and additionalParameters first
> [t, y] = ode45(@(t, y) defineODESystem(t, y, C, additionalParameters), ...)
Thanks. Actually, I do not need the `y' as my C seems to contain as
much info as needed.

I am now using

==
[t, C]=ode23(@(t,C) defineODESystem(t, C, 20, 100, 100, 1, 1), [0
150000], [1 2 3 4 5])
==

and, seeing the outputs at the MATLAB prompt, defineODESystem seems to
be repeated until the end of the universe. Why? I just want
defineODESystem to be launched once!

Thanks.

- --
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- --

If you want to judge a man's character, give him power.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAkt8UMIACgkQM0LLzLt8MhzZIgCfSdlipRlXbmUmBye7Kvd390Qt
5UkAnjX/SNUCqfmlrOrJ6wkfv7B5oM5l
=pSGu
-----END PGP SIGNATURE-----

Subject: Passing argument to a function handle (@...)

From: Steven Lord

Date: 19 Feb, 2010 22:45:15

Message: 4 of 4


"Merciadri Luca" <Luca.Merciadri@student.ulg.ac.be> wrote in message
news:87aav7wqwt.fsf@merciadriluca-station.MERCIADRILUCA...

*snip*

> I am now using
>
> ==
> [t, C]=ode23(@(t,C) defineODESystem(t, C, 20, 100, 100, 1, 1), [0
> 150000], [1 2 3 4 5])
> ==
>
> and, seeing the outputs at the MATLAB prompt, defineODESystem seems to
> be repeated until the end of the universe. Why? I just want
> defineODESystem to be launched once!

Where is that line of code located? Is it located inside the
defineODESystem function itself? If so, then defineODESystem calls ODE23,
which calls defineODESystem, which calls ODE23, which calls defineODESystem,
which calls ODE23, which calls defineODESystem, which calls ODE23, which
calls defineODESystem, which calls ODE23, ... you get the picture.

Don't call any of the "function functions" (like the ODE solvers) with the
input being a handle to the function that's calling the solver, or you'll
either get an error about the recursion limit being exceeded or (if the
limit is set too high) you could crash MATLAB due to the recursion
exhausting the stack. Move the ODE23 call outside defineODESystem and/or
have ODE23 call a different function that evaluates the right-hand side of
the system of ODEs.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com