필터 지우기
필터 지우기

ODE solver with variable derivative

조회 수: 1 (최근 30일)
cameron morrison
cameron morrison 2020년 4월 22일
댓글: Bjorn Gustavsson 2020년 4월 22일
This question relates to the function handle to determine the derivative vector for use in ODE45.
My 'dx' changes during each iteration of my code (my dx is dependent on applied forces), as such I need a way of updating the force within the function. A simple example is shown below.
function dx = functionhandle(t,x_current)
dx(1) = x_current(1)*F_x
end
and then I would call this function handle in my main script to determine the new statespace
[t,x_new] = ODE45(@functionhandle,[0 dt/2 dt],x_current)
where x_new is my updated statespace. My question can be boiled down to this: How do I retreive F_x within the function handle to tabulate dx.

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2020년 4월 22일
It is rather easy once you've wrapped your head around it:
function dx = forcefunction(t,x_current,Pars4function)
% Example with sinusiodaly varying F:
A = Pars4function(1);
w = Pars4function(2);
F_x = A*sin(w*t);
dx(1) = x_current(1)*F_x;
end
Then you can call your ode-integrator like such:
x0 = 0.1; % Initial condition
t0 = 0; % Start-time
tend = 50; % end-time
A = 2; % amplitude of force
w = 1/2/pi; % angular frequency
t_span = linspace(t0,tend,5001);
[t,x_all] = ode45(@(x,t) forcefunction(t,x,[A,w]),t_span,x0);
That way you can send whatever parameters you need to the ode-defining function.
HTH
  댓글 수: 2
cameron morrison
cameron morrison 2020년 4월 22일
absolutely perfect - I wish I could buy you a pint
Bjorn Gustavsson
Bjorn Gustavsson 2020년 4월 22일
My pleasure!
(In my hometown we don't realy buy others pints, it's each man for himself)

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

추가 답변 (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