smart function to switch functions

조회 수: 1 (최근 30일)
feynman feynman
feynman feynman 2024년 4월 28일
댓글: feynman feynman 2024년 4월 30일
Suppose I want to tune different ode solvers to run:
ode23(@fun,...)
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?

채택된 답변

Stephen23
Stephen23 2024년 4월 28일
편집: Stephen23 2024년 4월 28일
You could use STR2FUNC:
str = 'ode23';
odesolver = str2func(str);
odesolver(@fun,..)
or simply use FEVAL:
feval(str,@fun,..)

추가 답변 (2개)

Anton Kogios
Anton Kogios 2024년 4월 28일
You could set up a function with a switch statement, something like:
function odesolver(@fun,odesolverStr)
switch odesolverStr
case 'ode23'
ode23(@fun)
case 'ode45'
ode45(@fun)
otherwise
error('Incorrect ode solver specified.')
end
end
  댓글 수: 1
feynman feynman
feynman feynman 2024년 4월 28일
Thank you very much. But I accepted the answer that is shorter

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2024년 4월 29일
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.
  댓글 수: 3
Steven Lord
Steven Lord 2024년 4월 29일
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.
feynman feynman
feynman feynman 2024년 4월 30일
thank you

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by