Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old October 27th, 2009, 03:36 PM
Super Member
 
Join Date: Dec 2008
Location: Scotland
Posts: 863
Country:
Thanks: 6
Thanked 341 Times in 319 Posts
Mush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the rough
Default 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.
Reply With Quote
Advertisement
 
  #2  
Old October 27th, 2009, 05:02 PM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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
Reply With Quote
The following users thank elbarto for this useful post:
Donate to MHF
  #3  
Old October 28th, 2009, 05:26 PM
Super Member
 
Join Date: Dec 2008
Location: Scotland
Posts: 863
Country:
Thanks: 6
Thanked 341 Times in 319 Posts
Mush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the rough
Default

Blah!

Last edited by Mush; October 28th, 2009 at 08:24 PM.
Reply With Quote
  #4  
Old October 28th, 2009, 05:38 PM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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
Reply With Quote
The following users thank elbarto for this useful post:
Donate to MHF
  #5  
Old October 28th, 2009, 07:08 PM
Super Member
 
Join Date: Dec 2008
Location: Scotland
Posts: 863
Country:
Thanks: 6
Thanked 341 Times in 319 Posts
Mush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the rough
Default

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?
Reply With Quote
  #6  
Old October 28th, 2009, 07:40 PM
Super Member
 
Join Date: Dec 2008
Location: Scotland
Posts: 863
Country:
Thanks: 6
Thanked 341 Times in 319 Posts
Mush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the rough
Default

Got it!

Last edited by Mush; October 28th, 2009 at 08:24 PM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 04:36 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.