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:
How do I pass a value to a function called by ode45?

Subject: How do I pass a value to a function called by ode45?

From: Jeff

Date: 5 Feb, 2013 17:55:10

Message: 1 of 6

I want to pass a value (beta) to a function which I'm passing to ode45, but I can't quite fiigure out how to do it from the documentation. Here is the current function to be passed:

function udot=ODE_periodic(t,u)
...
beta=4;
...
end

I call it from another file (call it MAIN):
...
boundary='periodic';
f=str2func(['ODE'_ boundary]);
options=....whatever...
[t_steps, u1] = ode45(f, t_steps, y0, options);
...

I execute main, change the value of beta, hopefully remember to save it, execute MAIN, repeat.

But notice how there are no parameters for the function f. It's just a function handle. How do I pass the value of beta into f?
Thanks.

Subject: How do I pass a value to a function called by ode45?

From: Steven_Lord

Date: 5 Feb, 2013 18:08:51

Message: 2 of 6



"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
news:kerh1u$72o$1@newscl01ah.mathworks.com...
> I want to pass a value (beta) to a function which I'm passing to ode45,
> but I can't quite fiigure out how to do it from the documentation. Here is
> the current function to be passed:

These examples show two approaches of how to pass additional parameters into
the function called by FZERO; those same techniques work with ODE45.

http://www.mathworks.com/help/matlab/math/parameterizing-functions.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How do I pass a value to a function called by ode45?

From: Jeff

Date: 5 Feb, 2013 22:17:27

Message: 3 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <kerhrj$aeq$1@newscl01ah.mathworks.com>...
>
>
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> news:kerh1u$72o$1@newscl01ah.mathworks.com...
> > I want to pass a value (beta) to a function which I'm passing to ode45,
> > but I can't quite fiigure out how to do it from the documentation. Here is
> > the current function to be passed:
>
> These examples show two approaches of how to pass additional parameters into
> the function called by FZERO; those same techniques work with ODE45.
>
> http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Unfortunately, I cannot use nested or anonymous functions. The functions I'm calling are in separate files. I cut down the example I typed in, but basically, it uses an if..elseif block to choose a function based on other parameters.

I was able to make my sample work by adding a third parameter to the declaration of f and then using this call:
[t_steps,s1]=ode45(@(t,y) f(t,y,beta), t_steps, s0, options);

But I can't say I understand what it's doing. And I note that t and y are not given values anywhere before the @(t,y) part of that command. But the correct values are being passed into the function (at least the first time) and I'm getting the results I expect.

Thanks.

Subject: How do I pass a value to a function called by ode45?

From: Christopher Creutzig

Date: 7 Feb, 2013 08:30:49

Message: 4 of 6

On 05.02.13 23:17, Jeff wrote:

> I was able to make my sample work by adding a third parameter to the declaration of f and then using this call:
> [t_steps,s1]=ode45(@(t,y) f(t,y,beta), t_steps, s0, options);
>
> But I can't say I understand what it's doing. And I note that t and y are not given values anywhere before the @(t,y) part of that command. But the correct values are being passed into the function (at least the first time) and I'm getting the results I expect.

It defines an anonymous function that takes two values, named t and y
inside this function, and then calls f(t,y,beta). A very useful idiom.


HTH,
Christopher

Subject: How do I pass a value to a function called by ode45?

From: Steven_Lord

Date: 7 Feb, 2013 16:08:42

Message: 5 of 6



"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
news:kes0dn$arn$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <kerhrj$aeq$1@newscl01ah.mathworks.com>...
>>
>>
>> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
>> news:kerh1u$72o$1@newscl01ah.mathworks.com...
>> > I want to pass a value (beta) to a function which I'm passing to ode45,
>> > but I can't quite fiigure out how to do it from the documentation. Here
>> > is the current function to be passed:
>>
>> These examples show two approaches of how to pass additional parameters
>> into the function called by FZERO; those same techniques work with ODE45.
>>
>> http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
>>
>> --
>> Steve Lord
>> slord@mathworks.com
>> To contact Technical Support use the Contact Us link on
>> http://www.mathworks.com
>
> Unfortunately, I cannot use nested or anonymous functions. The functions
> I'm calling are in separate files. I cut down the example I typed in, but
> basically, it uses an if..elseif block to choose a function based on other
> parameters.
>
> I was able to make my sample work by adding a third parameter to the
> declaration of f and then using this call:
> [t_steps,s1]=ode45(@(t,y) f(t,y,beta), t_steps, s0, options);

This uses an anonymous function and will work as long as you've defined beta
before this ODE45 call.

> But I can't say I understand what it's doing. And I note that t and y are
> not given values anywhere before the @(t,y) part of that command. But the
> correct values are being passed into the function (at least the first
> time) and I'm getting the results I expect.

ODE45 requires the function that you pass into it as a function handle for
the first input to accept two input arguments.

Your function f requires three input arguments.

If we have a function that accepted the two input arguments from ODE45 and
passed them and another value into f, to serve as an adapter or go-between
for ODE45 and the f function, they will both be satisfied. The anonymous
function IS that adapter/go-between. The "@(t, y)" section of the anonymous
function indicates that it accepts two inputs (and so is a valid first input
for ODE45) and the "f(t, y, beta)" section calls f with three input
arguments (as it requires.)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How do I pass a value to a function called by ode45?

From: Jeff

Date: 8 Feb, 2013 03:42:07

Message: 6 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message
>
> ODE45 requires the function that you pass into it as a function handle for
> the first input to accept two input arguments.
>
> Your function f requires three input arguments.
>
> If we have a function that accepted the two input arguments from ODE45 and
> passed them and another value into f, to serve as an adapter or go-between
> for ODE45 and the f function, they will both be satisfied. The anonymous
> function IS that adapter/go-between. The "@(t, y)" section of the anonymous
> function indicates that it accepts two inputs (and so is a valid first input
> for ODE45) and the "f(t, y, beta)" section calls f with three input
> arguments (as it requires.)
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Ahhh. In other words, the anonymous function "@(t,y)" is a function with one line. That line is "f(t,y,beta)".
Pretty neat.
Thanks for explanation.

Tags for this Thread

No tags are associated with 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