Question

I am learning about ode45 in my computational physics class. One thing I am a little confused about is the anonymous function. What is it? I have been searching around, but I can not seem to find anything.

Was it helpful?

Solution

Note: This is not a technical answer, this comes from my experience using ode45. Hopefully you find it useful and it answers your question. If anyone wants to correct any mistakes or details I have wrong, please do.

When using Matlab ode solvers, you have to specify the derivative function, which takes as inputs the independent variable (e.g., time) and the dependent variable(s) (e.g., position, speed, etc.). There are several ways to do this.

You can make a function function dydt=derivative(t,y) which evaluates the derivative at y and t, which you can call from other scripts. This is useful if you will be solving the same ode from a number of different m-files.

On the other hand, if you are only going to solve you ode from on m-file, there is no need to create a whole new function just for one ode, so you can use an anonymous function. You have two choices here. You can create a function like dydt=@(t,y) x+t and then when you call ode45 use ode45(dydt,tspan,y0) or define the ode in the function call: ode45(@(t,y) dydt,tspan,y0).

If solving small one-off problems, I generally define my ode as an anonymous function like dydt=@(t,y) .... If I am working on a bigger project I will write a function for the derivative.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top