How to calculate first and second derivative for concentration?
Show older comments
Hi, I have a code that show the concentration. I want to find the first and second derivative. How to calculate that? This is my code
clc;clear;
%input parameter
delta=50;
gamma=75;
K1=10^-4;
K2=5*10^-4;
K3=10^-3;
K4=5*10^-3;
K5=10^-2;
K6=5*10^-2;
Ko=0.1;
n=6;
Oa=10;
Pa=100;
mu_1=10^-3;
mu_2=10^-3;
mu_3=10^-3;
mu_4=10^-3;
mu_5=10^-3;
mu_6=10^-3;
mu_o=10^-4;
mu_p= 10^-5;
%input initial condition
M1(1)=10;
M2(1)=0;
M3(1)=0;
M4(1)=0;
M5(1)=0;
M6(1)=0;
O(1)=0;
P(1)=0;
%input for time
t(1)=0;
dt=0.01; %time interval
t=0:dt:360; %time span
%input empty array
T=zeros(length(t)+1,1); %empty array for t
M1=zeros(length(t)+1,1); %empty array for M1
M2=zeros(length(t)+1,1); %empty array for M2
M3=zeros(length(t)+1,1); %empty array for M3
M4=zeros(length(t)+1,1); %empty array for M3
M5=zeros(length(t)+1,1); %empty array for M3
M6=zeros(length(t)+1,1); %empty array for M3
O=zeros(length(t)+1,1);
P=zeros(length(t)+1,1);
sumter=K2*M2+K3*M3+K4*M4+K5*M5;
%first and second derivative
df=diff(M1(j+1),t);
d2f=diff(df,t);
for j = 1:length(t)
T(j+1)=T(j)+dt;
M1(j+1)=M1(j)+1./(1+exp(-T(j)));
M1(j+1) = M1(j)+(dt*(delta*M1(j+1)*(1-(M1(j+1)/gamma))-2*K1*M1(j+1)*M1(j+1)-M1(j+1)*sumter(j+1))-((Oa-n)*K6*M1(j+1)*M6(j+1))-((Pa-Oa)*Ko*M1(j+1)*O(j+1))-(mu_1*M1(j+1)));
M2(j+1) = M2(j)+(dt*(K1*M1(j)*M1(j)-K2*M1(j)*M2(j))-(mu_2*M2(j+1)));
M3(j+1) = M3(j)+(dt*(K2*M1(j)*M2(j)-K3*M1(j)*M3(j))-mu_3*M3(j));
M4(j+1) = M4(j)+(dt*(K3*M1(j)*M3(j)-K4*M1(j)*M4(j))-mu_4*M4(j));
M5(j+1) = M5(j)+(dt*(K4*M1(j)*M4(j)-K5*M1(j)*M5(j))-mu_5*M5(j));
M6(j+1) = M6(j)+(dt*(K5*M1(j)*M5(j)-K6*M1(j)*M6(j))-mu_6*M6(j));
O(j+1) = O(j)+(dt*(K6*M1(j)*M6(j)-Ko*M1(j)*O(j)-mu_o*O(j)));
P(j+1) = P(j)+(dt*(Ko*M1(j)*O(j)-mu_p*P(j)));
end
subplot (2,2,1)
%figure
%hold on
plot(T,M1,'r','Linewidth',2)
xlabel('Time (days)')
ylabel('Concentration (gr/ml)')
title ('M1')
subplot (2,2,2)
%figure
%hold on
plot(T,M2,'g','Linewidth',2)
xlabel('Time (days)')
ylabel('Concentration (gr/ml)')
title ('M2')
subplot (2,2,3)
%figure
%hold on
plot(T,M3,'b','Linewidth',2)
xlabel('Time (days)')
ylabel('Concentration (gr/ml)')
title ('M3')
subplot (2,2,4)
%figure
%hold on
plot(T,M4,'m','Linewidth',2)
xlabel('Time (days)')
ylabel('Concentration (gr/ml)')
title ('M4')
subplot (2,2,1)
%figure
%hold on
plot(T,M5,'r','Linewidth',2)
xlabel('Time (days)')
ylabel('Concentration (gr/ml)')
title ('M5')
subplot (2,2,2)
%hold on
plot(T,M6,'g','Linewidth',2)
xlabel('Time (days)')
ylabel('Concentration (gr/ml)')
title ('M6')
subplot (2,2,3)
%figure
%hold on
plot(T,O,'b','Linewidth',2)
xlabel('Time (days)')
ylabel('Concentration (gr/ml)')
title ('O')
subplot (2,2,4)
%figure
%hold on
plot(T,P,'m','Linewidth',3)
xlabel('Time (days)')
ylabel('Concentration (gr/ml)')
title ('P')
Accepted Answer
More Answers (1)
It seems the concentrations are solutions of differential equations, thus something like dy/dt = f(y,t).
So you can see that after you have solved the differential equations, you get the first derivative simply as dy/dt = f(y,t).
For the second derivative, you get
d^2y/dt^2 = d/dt (f(y,t)) = df/dy * dy/dt + df/dt = df/dy * f(t,y) + df/dt.
Further, there seems to be an error in your equations:
M2(j+1) = M2(j)+(dt*(K1*M1(j)*M1(j)-K2*M1(j)*M2(j))-(mu_2*M2(j+1)));
M2(j+1) appears on both sides of your equation. Since you initialized M2 to 0, the last term will always be 0 and so the equation is the same as if you had written
M2(j+1) = M2(j)+(dt*(K1*M1(j)*M1(j)-K2*M1(j)*M2(j)));
Other incorrect settings are the use of O(j+1) and sumter(j+1) in the equation for M1(j+1).
Categories
Find more on Mathematics 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!