I'm solving two differential equations and plotting the results of the second differential equation. I want to find the values at the max point in the plot. However, maybe because I'm using sym
, I'm having trouble figuring how to do it. I know that in math, you would just find the first derivative and set it to zero.
% Constants
k1 = 1/10;
k2 = 1/1.3;
k3 = 1/1.3;
k4 = 1/(1/48);
k5 = 1/3.9;
t = 1:5;
% Initial concentration
A0 = 5891.694;
B0 = 0;
%%% Mass balance equation 1
syms A(t) t
eqn = diff(A,t) == -k1*A -k2*A -k3*A -k4*A;
cond = A(0) == A0;
A(t) = dsolve(eqn,cond);
% A(t) = exp(-k1*t);
%%% Mass balance equation 2
syms B(t) t
eqn = diff(B,t) == k1*A +k2*A +k3*A +k4*A - k5*B;
cond = B(0) == B0;
B(t) = dsolve(eqn,cond);
%%% Plot
figure('visible','on');
t = 0:20;
plot(t,B(t))
So for the picture above, I'd like to find the max point. Sometimes, it isn't at exact time steps so I want MATLAB to just show the exact values at the max point.
You can use findpeaks
to find maxima in your data. To do so, first you need to convert your ode solution (algebraic expression) into a data list for a specific interval of study.
B(t) = dsolve(eqn,cond,'t') ;
t = 0:0.1:20;
z=eval(B(t))
Now using findpeaks
[pks, locs] = findpeaks(z)
pks =
5.7310e+03
locs =
2