필터 지우기
필터 지우기

How can I put condition in ode solver

조회 수: 9 (최근 30일)
kkh1897
kkh1897 2020년 12월 31일
댓글: kkh1897 2020년 12월 31일
I'm trying to put if condition in ode solver.
but it seems that I can't control variables in ode function. (can control just derivative of variables)
for example
function dcdt = exo(t,c)
dcdt = zeros(3,1)
dcdt(1) = -c(1)*exp(-1/c(3));
if c(1) < 0
c(1) = 0; ----->>> this doesn't work!!!
end
dcdt(2) = -c(2)*exp(-1/c(3));
dcdt(3) = - dcdt(1) + dcdt(2);
end
How can I change this function to make c(1) = 0 if c(1) < 0

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 31일
The above solution is really only appropriate if the reason that c(1) becomes negative is due to round-off or overshooting (that is, a smaller step size would not have gone negative.)
If you are trying to do the equivalent of bounding off a floor, then it is not appropriate to use this solution, and instead you should use an ode event function to terminate the integration, then change the boundary condition, and then restart the integration.

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 12월 31일
Use odeset to create an options structure with the NonNegative option set to the elements of the solution that need to remain non-negative and pass that options structure into the ODE solver.
  댓글 수: 1
kkh1897
kkh1897 2020년 12월 31일
I try to use ode23 solver. Sorry I didn't mention the solver.

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

카테고리

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