Results 1 to 6 of 6

Math Help - Ode45

  1. #1
    Super Member
    Joined
    Dec 2008
    From
    Scotland
    Posts
    901

    Ode45

    Hello there.

    Okay, let's say I have a system of 1st order ODEs to solve. So I create a function file which describes these functions called, say:

    Code:
     function dy = myfunction(t,y)
    .

    Now, to solve these ODEs using ODE45, I'd simply write a script file which says:

    Code:
    [t,y] = ode45(@myfunction, [t0 tf], [y10 y20 y30 y40...yn0])
    , and ODE45 will perform the integration and spit out the solution in the vector y.

    Now let's say I want to do the same thing again, but rather than use ODE45, I'd like to use my own integration scheme. So I begin to write my function file to perform the integration using, say, a modified Euler method. How would I write the file in such a way that it takes the same inputs as ode45, and produces the same outputs? How to I write my integration scheme to use myfunction(t,y)? I'm rather confused about it, and scanning over the ODE45 code to see how ODE45 does it is a bit beyond my scope.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    I think you need to look into "function_handles and "anonymous functions" if I understand your question correctly?

    Example:
    Code:
    EDU>> f = @(x)magic(x)
    
    f = 
    
        @(x)magic(x)
    
    EDU>> f(2)
    
    ans =
    
         1     3
         4     2
    
    EDU>> f(5)
    
    ans =
    
        17    24     1     8    15
        23     5     7    14    16
         4     6    13    20    22
        10    12    19    21     3
        11    18    25     2     9
    
    EDU>>
    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Super Member
    Joined
    Dec 2008
    From
    Scotland
    Posts
    901
    Blah!
    Last edited by Mush; October 28th 2009 at 07:24 PM.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    When you pass your function into ode45, you are passing it in as a function_handle. This allowes ode45 to assign its own name to some instance of your function, which in my example I called f. If you already know how to do the integration and want to create a function like ode45, you will need to use function_handles.

    This might help:
    Function Handles (Programming and Data Types)

    You are essentially createing a function_handle when you write
    Code:
    @mydifferentialequation
    Maybe I am still misreading the question? This is the method I used when I wrote my own implementation of newtons method, I let the user pass a function_handle for f(x) and a second function_handle for df/dx.

    Elbarto
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Super Member
    Joined
    Dec 2008
    From
    Scotland
    Posts
    901
    Quote Originally Posted by elbarto View Post
    When you pass your function into ode45, you are passing it in as a function_handle. This allowes ode45 to assign its own name to some instance of your function, which in my example I called f. If you already know how to do the integration and want to create a function like ode45, you will need to use function_handles.

    This might help:
    Function Handles (Programming and Data Types)

    You are essentially createing a function_handle when you write
    Code:
    @mydifferentialequation
    Maybe I am still misreading the question? This is the method I used when I wrote my own implementation of newtons method, I let the user pass a function_handle for f(x) and a second function_handle for df/dx.

    Elbarto
    Would it be too much to request an excerpt of example code?
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Super Member
    Joined
    Dec 2008
    From
    Scotland
    Posts
    901
    Got it!
    Last edited by Mush; October 28th 2009 at 07:24 PM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. ode45 help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 12th 2010, 11:53 PM
  2. Matlab ode45
    Posted in the Math Software Forum
    Replies: 8
    Last Post: October 24th 2009, 12:22 PM
  3. Using ODE45
    Posted in the Math Software Forum
    Replies: 10
    Last Post: March 29th 2009, 02:14 PM
  4. ode45
    Posted in the Differential Equations Forum
    Replies: 3
    Last Post: March 19th 2009, 05:58 AM
  5. ode45 MATLAB
    Posted in the Math Software Forum
    Replies: 2
    Last Post: September 24th 2008, 05:02 AM

Search Tags


/mathhelpforum @mathhelpforum