Adding conditions / additional input to ode45 function

조회 수: 25 (최근 30일)
yuval . 2017년 5월 2일
편집: yuval . 2017년 5월 5일
Hello, I'm using ode45 and I want to be able to add an input ('i') which will determine a parameter in the function I put in the ode45:
[T,V] = ode45(@myfun,[0 20],vec_0);
and the function (shortened) is:
function dy = myfun(t,y)
if i==1
I_inj = 20;
elseif i==2
I_inj = 40;
% etc...
dy(1) = ...
dy(2) = ...
dy(3) = ...
dy(4) = ... + I_inj;
I've tried to add 'i' in the inputs of the function: function dy = hh(t,y,i) but I couldn't understand how to input 'i' in the ode45 function ( [T,V] = ode45(@myfun,[0 20],vec_0); ) I'll be happy to know how to do that or if there are other ways to set conditions to a function for ode45. Thanks a lot!

채택된 답변

Steven Lord
Steven Lord 2017년 5월 2일
While this documentation page uses fzero in its examples, the same techniques work for passing additional parameters into the ODE functions that you specify as the first input when you call ode45.

추가 답변 (1개)

yuval 2017년 5월 5일
편집: yuval 님. 2017년 5월 5일
For future reference, besides the solutions presented in the documentation page referenced by Steven Lord, I found another and somewhat simpler option, which is not mentioned in the help section of ode45. It is possible to add more parameters in this method:
[T,V] = ode45(@my_fun,[0 20],[2 0],options,extra_parameter);
It only seemed to work if I defined 'options' (odeset), although I had no intention to, but otherwise it didn't take my extra parameter. That is, the following does not work:
[T,V] = ode45(@my_fun,[0 20],[2 0],extra_parameter);
Also, this is possible:
[T,V] = ode45(@(t,v) my_fun(t,v,extra_parameter),[0 20],[20 0]);


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