When using ODE45 (or similar functions), what is the benefit of using anonymous functions over passing additional parameters as trailing arguments?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
When I want to solve a differential equation with additional parameters
function dx = myfun(t, x, p1, p2)
...
[T, Y] = ode45(@myfun, ts, x0, [], p1, p2);
[T, Y] = ode45(@(t,x) myfun(t, x, p1, p2), ts, x0);
What is the benefit of the latter method over the former?
Accepted Answer
Jiro Doke
on 24 Feb 2011
These are some comments I received from a developer:
- Some of the newer "function functions" that were introduced since we switched to the newer methods don't support the older syntax. For example, the older BVP solver bvp4c allows the trailing parameters syntax, while the newer BVP solver bvp5c does not. Thus switching from one solver to another will work if you use the anonymous or nested function approach (well, at least back to release R14 when we introduced anonymous and nested functions) but may not if you use the trailing parameters syntax. Similarly, quad supports trailing parameters while quadgk does not. This was a deliberate decision based on the experiences we've had with the two other considerations below.
- If you use the trailing parameters syntax for a function function that receives (either directly or via the options structure) multiple functions, all those functions must accept all the trailing parameters, even if they don't use any of them. So if you call ode45 with an ODE function and an options structure that sets the OutputFcn, Jacobian, Events and Mass options to be function handles and each of those takes a distinct additional parameter, all five functions will need to accept all five additional parameters.
- If you want to call quad with a function handle and limits of integration (just the required inputs) and an additional parameter you will need to specify empty arrays for the tol and trace inputs. Failing to do so will result in MATLAB using one of your additional parameters as the tolerance or trace value and passing one too few additional parameters into your function handle. This causes a lot of confusion for users, somewhat for quad, but even more for one of the Optimization Toolbox "function functions": fmincon. fmincon has only four required inputs but the additional parameters don't start until the eleventh input. [fun, x0, A, b are required; Aeq, beq, lb, ub, nonlcon, options are optional.] Missing a [] and having fmincon error while trying to interpret your first additional parameter as an options structure happens fairly frequently.
- Personally, I like having the additional parameters included right next to (indeed, inside) the function that uses them as opposed to at the end of a (potentially long; see fmincon) function call. In my opinion, the locality helps readability.
8 Comments
Jan
on 24 Feb 2011
Thanks a lot for this exaustive answer to my question. It is very helpful.
Jan
on 25 Feb 2011
I've removed my answer about using the param-value style. It actually does not match the question.
Jiro Doke
on 25 Feb 2011
I feel kind of naughty accepting my own answer, but oh well.
Jan
on 5 Aug 2011
@Jiro: The question and the answer are useful. You've accepted the answer, which solves the problem, and this is *helpful* for the users. +1
can we solve 2nd order ODE using IMPROVED runge kutta nystrom methods for 3rd order (IRKN3)
SHIVANI TIWARI:
That is a question not related to whether to use anonymous functions or the obsolete syntax, so you should create a new Question for that.
SHIVANI TIWARI
on 21 Apr 2019
Edited: SHIVANI TIWARI
on 21 Apr 2019
Actually i just need MATLAB coding for solving 2nd order differential equations using Runge kutta method.
Actually, SHIVANA TIWARI, you need to open your own Question, not post in this unrelated topic.
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)