Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Using IF condition with ODE
조회 수: 2 (최근 30일)
이전 댓글 표시
I would need some help to check on the proper utilization of conditions inside ODE equations.
Below can be found the code. Graphes were managed to be plotted but they were not as I expected them to be.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
%Constants
Iapp=40*60;
K(1)=0.001;
K(2)=10e-4;
K(3)=1;
K(4)=5;
K(5)=0.8;
% Initial Conditions
y0 = [3.949270942 7e-4 0.000281838];
% Time inverval
t_exp = [0 30 60 120 180 240 300];
% Solver;
options = odeset('AbsTol',1e-6);
[t,y] = ode15s(@manu,t_exp,y0,options,Iapp,K);
% Graphics:
figure(1)
%Figure 1-Ca
subplot(311)
plot(t,y(:,1),'r')
legend('Ca2+ theo')
title('Ca2+')
xlabel('treatment time / min')
ylabel('Concentration / M')
hold on
grid
%Figure 2-CO3
subplot(312)
plot(t,y(:,2),'r')
legend('CO32- theo')
title('CO32-')
xlabel('treatment time / min')
ylabel('Concentration / M')
hold on
grid
%Figure 3-OH
subplot(313)
plot(t,y(:,3),'r')
legend('OH- theo')
title('OH-')
xlabel('treatment time / min')
ylabel('Concentration / M')
hold on
grid
function dydt = manu(t,y,Iapp,K)
if y(2)>=K(2)
Ca = -K(1)*y(1);
CO3 = -K(4)*y(2);
OH = 0;
elseif y(2)<=K(2)
Ca = 0;
CO3 = K(3);
OH = Iapp/96500*K(5);
end
dydt = [Ca; CO3; OH];
end
댓글 수: 9
Torsten
2020년 3월 30일
In this case Walter's suggestion to use Matlab's event option is the way to go.
Or - if the equations remain that simple - you can integrate using pencil and paper.
답변 (2개)
Walter Roberson
2020년 3월 30일
You need to use Events in the options to terminate the ode integration, and then restart from that time. None of the ode* functions can deal with discontinuities in any of the derivatives (and not in two more derivatives beyond either.) The ode* functions do not always notice the discontinuities, but their mathematical models rely upon there not being any discontinuities, so even if they do not complain then the results are invalid.
댓글 수: 4
darova
2020년 4월 1일
Maybe you don't need event function for this case. I just add persistent variable to your ode function
function main
clear functions
% your main code
end
function dydt = manu(t,y,Iapp,K)
persistent flag
if isempty(flag)
flag = false;
end
if y(2)>=K(2) || flag
% variables
flag = true;
elseif y(2)<=K(2)
Ca = 0;
CO3 = K(3);
OH = Iapp/96500*K(5);
end
dydt = [Ca; CO3; OH];
end
result
댓글 수: 1
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!