Matlab ode solver - putting condition in function

I use ode23s solver
and the results show that variables oscillate around zero plus and minus.
I want to fix the variables to zero when they first go under zero.
ode23s solver can't use odeset 'NonNegative' option,
so we should have to put some condition in the function.
Please give me help.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 12월 6일
편집: Ameer Hamza 2020년 12월 6일

0 개 추천

The easiest option might be to do this after getting the solution from ode23s. For example
[t, y] = ode23s(..)
idx = find(y<0, 1);
y(idx:end) = 0
This assumes that your ODE is first-order and y is a column vector.

댓글 수: 4

Thank you for your response.
But I should have to reflect every variables in each steps.
for example,
funtion dxdt = ode(t,x)
dxdt = zeros(4,1)
dxdt(1) = -x(1)*exp(a/x(4));
dxdt(2) = -x(2)*exp(b/x(4));
dxdt(3) = -x(3)*exp(c/x(4));
dxdt(4) = m1*dxdt(1) + m2*dxdt(2) + m3*dxdt(3);
end
(here, a,b,c,m1,m2,m3 are constant)
I want to put condition which makes 'x' go to zero at the moment it becomes under zero.
So I put condition inbetween, but it doesn't work.
funtion dxdt = ode(t,x)
dxdt = zeros(4,1)
dxdt(1) = -x(1)*exp(a/x(4));
if x(1) < 0
x(1) = 0;
end
dxdt(2) = -x(2)*exp(b/x(4));
dxdt(3) = -x(3)*exp(c/x(4));
dxdt(4) = m1*dxdt(1) + m2*dxdt(2) + m3*dxdt(3);
end
Can you show the value of variables and initial conditions used to run ode45.
kkh1897
kkh1897 2020년 12월 6일
I sent you a email. Thank you
This is the code with ode45(). However, due to some reason, the result is different as compared to dsolve(). I am not sure why.
ode = @(t, x) [x(2);
-sign(x(1)+x(2))];
IC = [1; 0];
tspan = [0 5];
ode(ode, tspan, IC)

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

추가 답변 (0개)

카테고리

질문:

2020년 12월 6일

댓글:

2020년 12월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by