The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How do I pass a value to a function called by ode45?
Replies: 5   Last Post: Feb 7, 2013 10:42 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 18,038
Registered: 12/7/04
Re: How do I pass a value to a function called by ode45?
Posted: Feb 7, 2013 11:08 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Jeff " <> wrote in message
> "Steven_Lord" <> wrote in message
> <kerhrj$aeq$>...

>> "Jeff " <> wrote in message
>> news:kerh1u$72o$

>> > 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.
>> --
>> Steve Lord
>> To contact Technical Support use the Contact Us link on

> 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
To contact Technical Support use the Contact Us link on

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.