Clear Filters
Clear Filters

smart function to switch functions

2 views (last 30 days)
Suppose I want to tune different ode solvers to run:
I want to use 'odesolver' so that
odesolver(@fun,...) actually runs ode23(@fun,...) when specifying odesolver=='ode23' and runs ode45(@fun,...) when specifying odesolver=='ode45'. Is there a smart way to do it?

Accepted Answer

Stephen23 on 28 Apr 2024
Edited: Stephen23 on 28 Apr 2024
You could use STR2FUNC:
str = 'ode23';
odesolver = str2func(str);
or simply use FEVAL:

More Answers (2)

Anton Kogios
Anton Kogios on 28 Apr 2024
You could set up a function with a switch statement, something like:
function odesolver(@fun,odesolverStr)
switch odesolverStr
case 'ode23'
case 'ode45'
error('Incorrect ode solver specified.')
  1 Comment
feynman feynman
feynman feynman on 28 Apr 2024
Thank you very much. But I accepted the answer that is shorter

Sign in to comment.

Steven Lord
Steven Lord on 29 Apr 2024
If you're using a sufficiently recent release (release R2023b or later) use the ode object and specify the Solver property to tell the object which ODE solver to use.
Steven Lord
Steven Lord on 29 Apr 2024
The ode object was introduced in release R2023b. It doesn't exist in earlier releases. There isn't a way to "automatically have MATLAB decide which ODE solver to call" in earlier releases.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!