Why do I get different results from the numerical solution and the analytical solution with ODE15S? The numerical solution looks smoother than the analytical one
Show older comments
I want to simulate the dynamic behavior of pesticide in fruits, which is affected by daily temperature and relative humidity, by using an ode15s solver. And the result of the numerical model is then compared with the results of the analytical model.
Why do I get different results from the numerical solution and the analytical solution with ODE15S? The numerical solution looks smoother than the analytical one. And the results of ode15s solver seem to average the results to make it smoother, could somebody give me some ideas how to solve this problem? It gives me a headache. Thanks a lot!

4 Comments
Alan Stevens
on 18 Jun 2021
Looks like the analytical values are calculated on a finer mesh than the numerical ones. However, it is difficult to tell without seeing both the analytical expression and the coding used to generate the numerical solution.
Quanshun An
on 18 Jun 2021
Alan Stevens
on 18 Jun 2021
Sorry! They are far too complicated for me to investigate, given that I don't know anything about the context. You also use a large number of global variables which never helps with debugging.
Quanshun An
on 18 Jun 2021
Accepted Answer
More Answers (1)
Alan Stevens
on 18 Jun 2021
Much easier to follow! Your "analytical" solution is incorrect for "constants" that change with time. See the following
%% Read data
[num, txt, raw] = xlsread('test','Sheet1'); % Read data from document
Time_Cell=raw(2:137,1); % Extract time value
Time = cell2mat(Time_Cell); % Convert cell to double
Value_Cell=raw(2:137,2:3);
Value = cell2mat(Value_Cell);
t_measured = Time;
k1 = Value(:,1);
b1 = Value(:,2);
m_Soil_initial = 10;
% Chemical mass
%m_Soil = b1 ./ k1 .* (1 - exp(- k1 .* t_measured)) + m_Soil_initial .* exp(- k1 .* t_measured);
m_Soil = m_Soil_initial .* exp(- k1 .* t_measured);
figure(1)
plot(t_measured, m_Soil,'k','LineStyle','-','LineWidth',1);
grid
axis([0 40 0 10])
% dm/dt = -k1*m m = m0*exp(-k1*t)
% With k changing from one time to the next, you need to ingtegrate between
% the t_measured times starting from the previous time i.e. with a revised
% "initial" value of mass. See below
% Integrate from 0 to 1
% m(t) = 10*exp(-k11*t) m(1) = 10*exp(-k11)
% Integrate from 1 to 2
% m(t) = m(1)*exp(-k12*(t-1)) m(t) = 10*exp(-k11)*exp(-k12*(t-1))
% m(t) = 10*exp(-k11+k12)*exp(-k12*t)
% m(2) = 10*exp(-k11-k12)
% etc.
k = cumsum(k1);
m = [10; 10*exp(-k)];
m(end)=[];
hold on
plot(t_measured,m,'bo-')
legend('original','stepwise integration')
1 Comment
Quanshun An
on 19 Jun 2021
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!