Why does using MATLAB's ODE result in undefined behavior for this simulation?
Show older comments
While trying to switch from a closed-source FORTRAN-based differential equation solver to MATLAB, we encountered rather unusual behavior, when applying MATLAB’s ODEs to our simulations. The recommended ode45 puts out NaN after a certain x-value, which is different on all tested machines and MATLAB versions.
The following scripts can be run without MATLAB throwing an error (on our machines) and represent two different models, which we are not able to simulate with MATLAB at the moment. Both models differ exclusively in the fixed variables for the simulation. For simplicity’s sake, the variables have been refactored.
function output = model1
I1 = 0.0833;
I2 = 1.317;
I3 = 1.013*(10^5);
I4 = 2.823*(10^-5);
I5 = 1.9*(10^-7);
I6 = 8.314*(10^-3);
y0(1)=1;
y0(2)=0;
y0(3)=0;
tspan = 0:1:800;
[t,y] = ode23s(@(t,y) diff_eqs(t,y,I1,I2,I3,I4,I5,I6),tspan,y0);
output = [t,y];
end
function dydt = diff_eqs(t, y, I1, I2, I3, I4, I5, I6)
A1 = y(1);
A2 = y(2);
A3 = y(3);
A4 = 1.0-A1-A2;
U1 = (10^13)*exp(-134/(I6*t));
U2 = (10^13)*exp(-96/(I6*t));
U3 = (10^13)*exp(-46/(I6*t));
U4 = 2888.45;
U5 = (I3*I5)/(I6*1000.0*273.15);
dydt(1,1)=(1/I1*(2*(U2*A4*A2-U1*A1*A1)));
dydt(2,1)=(1/I1)*(((A3*I3*A4*U4)/(I2+A3))-U3*A2+U1*A1*A1-U2*A4*A2);
%dydt(3,1)=(1/I1)*(-A3+(-U4*A3*I3*A4/(A3+I2)+U3*A2))*((I2+A3)^2/(U5*I2));
dydt(3,1)=(1/I1)*(-A3+(-U4*A3*I3*A4/(A3+I2)+U3*A2))/(U5/I4*((1/(I2+A3)-A3/(I2+A3)^2)));
end
And for the second model:
function output = model2
I1 = 0.0833;
I2 = 1.317;
I3 = 1.013*(10^5);
I4 = 2.823*(10^-5);
I5 = 1.9*(10^-7);
I6 = 8.314*(10^-3);
y0(1)=1;
y0(2)=0;
y0(3)=0;
tspan = 0:1:800;
[t,y] = ode23s(@(t,y) diff_eqs(t,y,I1,I2,I3,I4,I5,I6),tspan,y0);
output = [t,y];
end
function dydt = diff_eqs(t, y, I1, I2, I3, I4, I5, I6)
A1 = y(1);
A2 = y(2);
A3 = y(3);
A4 = 1.0-A1-A2;
U1 = (1.32E9)*exp(-155/(I6*t));
U2 = (4.29E9)*exp(-28.5/(I6*t));
U3 = (1.87E14)*exp(-43.1/(I6*t));
U4 = 24.983;
U5 = (I3*I5)/(I6*1000.0*273.15);
dydt(1,1)=(1/I1*(2*(U2*A4*A2-U1*A1*A1)));
dydt(2,1)=(1/I1)*(((A3*I3*A4*U4)/(I2+A3))-U3*A2+U1*A1*A1-U2*A4*A2);
%dydt(3,1)=(1/I1)*(-A3+(-U4*A3*I3*A4/(A3+I2)+U3*A2))*((I2+A3)^2/(U5*I2));
dydt(3,1)=(1/I1)*(-A3+(-U4*A3*I3*A4/(A3+I2)+U3*A2))/(U5/I4*((1/(I2+A3)-A3/(I2+A3)^2)));
end
I have included the graphs of the simulations, as they should ideally look like (from the software, that is about to be deprecated) and the graphs that MATLAB puts out. The important values are the results of equation 3 (the red one in the MATLAB plots). We have tried every ODE but still struggle to obtain the desired output. We would be very happy if someone with more experience with MATLAB’s ODEs could point us in the right direction, as to what might be the problem here.
MATLAB output model 1:

MATLAB output model 2:

Both models from our old software:

All outputs are from a 64-bit system and a fairly new MATLAB version (R2017a or at most 2 versions earlier).
Accepted Answer
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!