I tried to solve a delay differential equation using dde23 but it seems I didn't understand it correctly so the function I wrote has error and I couldn't correct it to run to see if the output is correct.
I want to solve this system:
I couldn't understand how to add the last five equation to the program. I have to solve this system using table 1 parameter to gain an output such as fig 1 in image.
This is the code I wrote:
clear all;
clc;
lags=1;
sol=dde23(#eq24,lags,#eqh,[0 80]);
plot(sol.x,sol.y)
function v=eqh(t)
v=zeros(6,1);
function v=eq24(t,s,Ia,Is,R,N)
Alfa=0.1;
beta1=0.09;
beta2=0.1;
sigma1=0.3;
sigma2=0.4;
mu=0.01;
alfa=0.2;
rho=0.4;
r1=0.4;
r2=0.2;
d1=0.2;
d2=0.15;
k=0.1;
p=0.8;
tau=1;
T=4;
dsdt=Alfa-beta1*((s*Ia)/(1+sigma1*s))-beta2*((s*Is)/(1+sigma2*s))-mu*s+alfa*R;
dIadt=rho.*exp(-mu*tau).*s(((beta1.*Ia)/(1+sigma1.*s))+((beta2.*Is)/(1+sigma2.*s)))-(r1+d1+mu).*Ia;
dIsdt=(1-rho).*exp(-mu.*tau).*s(((beta1.*Ia)/(1+sigma1.*s))+((beta2.*Is)/(1+sigma2.*s)))+(1-k).*r1.*Ia-(r2+d2+mu).*Is;
dRdt=k.*r1.*Ia+r2.*Is-mu.*R-alfa.*R;
dNdt=Alfa-mu.*N-d1.*Ia-d2.*Is;
I have managed to come up with a running code. But I am still confused about t⁺ part of your question. The results of my code are almost the same as the ones you have presented. I hope this helps you with whatever you are doing.
function sol = eq242
clf
global lambda beta1 beta2 sigma1 sigma2 mu alpha rho r1 r2 d1 d2 k tau
lambda=0.1;beta1=0.09;beta2=0.1;sigma1=0.3;sigma2=0.4;mu=0.01;alpha=0.2;
rho=0.4;r1=0.4;r2=0.2;d1=0.2;d2=0.15;k=0.1;
opts = ddeset('RelTol',1e-5,'AbsTol',1e-8);
sol = dde23(#eq24,tau,[1, 1, 1, 1, 1],[0, 50],opts);
figure(1)
plot(sol(1).x,sol(1).y(1,:),sol(1).x,sol(1).y(2,:),sol(1).x,sol(1).y(3,:),sol(1).x,sol(1).y(4,:),sol(1).x,sol(1).y(5,:))
function dydt=eq24(t,y,Z)
global lambda beta1 beta2 sigma1 sigma2 mu alpha rho r1 r2 d1 d2 k tau
s = y(1);
Ia = y(2);
Is = y(3);
R = y(4);
N = y(5);
slag1 = Z(1,1);
ialag2 = Z(2,1);
islag3 = Z(3,1);
dsdt=lambda-beta1*((s*Ia)/(1+sigma1*s))-beta2*((s*Is)/(1+sigma2*s))-mu*s+alpha*R;
dIadt=rho*exp(-mu*tau)*slag1*(((beta1*ialag2)/(1+sigma1*slag1))+((beta2*islag3)/(1+sigma2*slag1)))-(r1+d1+mu)*Ia;
dIsdt=(1-rho)*exp(-mu*tau)*slag1*(((beta1*ialag2)/(1+sigma1*slag1))+((beta2*islag3)/(1+sigma2*slag1)))+(1-k)*r1*Ia-(r2+d2+mu)*Is;
dRdt=k*r1*Ia+r2*Is-mu*R-alpha*R;
dNdt=lambda-mu*N-d1*Ia-d2*Is;
dydt= [dsdt; dIadt; dIsdt; dRdt; dNdt];
Related
The aim is to solve the below equations and plot m with time, i.e. dm/dt
k is unknown and needs to be estimated. For the parameter estimation, the below values for m versus t can be used. This is a dummy dataset, as I would like to get an idea if this problem is solvable. If needed, an initial guesstimate for k can be provided 6e-5 m2/bar.hr.
I have added an initial idea for the code, but not sure if my implementation of the A(H) and P(H) functions in the odes is correct. Also I need a way to estimate for k whilst fitting to the provided m vs t data.
function dydt = diffunTAR(~,y,k, h, A_H, P_H, rho_h, rho_o, wo)
dydt = zeros (2,1);
D = 2.64/1000 %mm/1000 to convert to m
r=D/2
rho_o = 1320 %kg/m3
rho_h = 1000 % kg/m3
wo=648/1000000 %weight component 1 in mg converted to kg
h= 1.5/1000 %1.5mm expressed in m
function A_H= pi*(D/2)^2 + (2/r)*(1+(rho_o/rho_h*y(2)))*(wo/rho_o);
end
function P_H = 5004.6*y(2) + 150.39;
end
%dV/dt
dydt(1)= (k/h)* function A_H* function P_H;
%dH/dt
dydt(2)= (rho_h*k/wo*h)*function A_H*function P_H;
end
tspan = linspace(0,21*24); %time in hrs for 21 days
y0 = [0 6.4800e-04];
[t,y]=ode45(#(t,y) diffunTAR(t,y, k, h, A_H0, rho_h, rho_o, wo), tspan, y0);
updated code after help
function dydt = diffunTAR(~,y,k,h,rho_h,rho_o,wo,L,r)
dydt = zeros (2,1);
%dV/dt
dydt(1)= k/h* AH * PH;
%dH/dt
dydt(2)= (rho_h*k/wo*h) * AH * PH;
AH
PH
function PH
(5004.6*y(2))+150.3;
end
function AH
pi*(r^2)+(2/r)*(1+(rho_o/rho_h*y(2)))*(wo/rho_o);
setInitialConditions (AH, 2*pi*(r)*L+2*pi*(r)^2);
end
end
D = 2.64/1000 %mm/1000 to convert to m
r=D/2
L = 10.6/100 %cm/100 to convert to m
wo=648/1000000 %weight osmotic tablets in mg converted to kg
k=6e-5 %guess for permeability
h= 1.5/1000 %1.5mm expressed in m
rho_o = 1320 %kg/m3
rho_h = 1000 % kg/m3
tspan = linspace(0,21*24); %time in hrs for 21 days
y0 = [0 6.4800e-04];
[t,y]=ode45(#(t,y) diffunTAR(t,y,k,h,rho_h,rho_o,wo,L,r), tspan, y0);
i'm trying to evaluate a G11 symfun in the code below but it fails and keep showing me the variable t that I choose to be set as specified in the code, I even tried to use the 'subs' command but it failed also. I define the necessary symbols and variables but the t variable does not evaluated in just 'G11' symfun, there are another similar symfun such G12 or K12 but the t variable evaluated in them
here is my code
% Defining the variables as symbols.
clear all, close all
clc
syms x xi q_1 q_2 q_3 q_4 q_01 q_02 q_03 q_04 EI Mr M m L t Omega S1 S2 S3
...
S4 S5 S6 S7 S8 w L_dot U U_dot g L_ddot M11 M22
% Defining the general coordinates and the trial function.
L_ddot=0;
g=9.8;
Mr=M/(M+m);
PHI(x,t)=[sqrt(2).* sin(pi.*(xi)) sqrt(2).* sin(2*pi.*(xi)) sqrt(2).*
sin(3.*pi.*(xi)) sqrt(2).* sin(4.*pi.*(xi))];
q_1(t)= S1*exp(sqrt(-1)*w*t);
q_01(t)= S5*exp(sqrt(-1)*w*t);
q_2(t)= S2*exp(sqrt(-1)*w*t);
q_02(t)= S6*exp(sqrt(-1)*w*t);
q_3(t)= S3*exp(sqrt(-1)*w*t);
q_03(t)= S7*exp(sqrt(-1)*w*t);
q_4(t)= S4*exp(sqrt(-1)*w*t);
q_04(t)= S8*exp(sqrt(-1)*w*t);
Q_v(t) =[q_1;q_2;q_3;q_4];
Q_w(t) =[q_01;q_02;q_03;q_04];
V(x,t) = PHI*Q_v(t);
W(x,t) = PHI*Q_w(t);
% Defining the coeficients of the ODE.
U(x,t)=U;
L(x,t)=1+0.1*t;
L_dot(x,t)=diff(L,'t',1);
L_ddot=0;
M11=int(PHI'*PHI,'xi',[0 1]);
M22=M11;
G11=(2*(L_dot/L))* int((2-xi)*PHI'*diff(PHI,'xi',1),'xi',[0 1])+2*Mr*
(U/L)*int(PHI'*diff(PHI,'xi',1),'xi',[0 1]);
G12=-2*Omega*int(PHI'*PHI,'xi',[0 1]);
G21=-G12;
G22=G11;
K11=(EI/((M+m)*L^4))*int(PHI'*diff(PHI,'xi',4),'xi',[0 1])+ (L_dot/L)^2
*int((2-xi)^2 *...
PHI'*diff(PHI,'xi',2),'xi',[0 1])+ ((L_ddot*L-2*L_dot^2)/L^2)*int((1-
xi)*PHI'*diff(PHI,'xi',1),'xi',[0 1]) ...
+ 2*Mr*(L_dot*U/L^2)*int((2-xi)*PHI'*diff(PHI,'xi',2),'xi',[0 1])+ Mr*
(U/L)^2 *int(PHI'*diff(PHI,'xi',2),'xi',[0 1])-...
(g-((L_ddot-2*L_dot^2)/L))*int((1-xi)*PHI'*diff(PHI,'xi',2),'xi',[0 1])+
(g/L)*int(PHI'*diff(PHI,'xi',1),'xi',[0 1])-...
Omega^2 * int(PHI'*PHI,'xi',[0 1]);
K12= -2*Omega*(L_dot/L)*int((2-xi)*PHI'*diff(PHI,'xi',1),'xi',[0 1])-2*Mr*
((U*Omega)/L)*int(PHI'*diff(PHI,'xi',1),'xi',[0 1]);
K21=-K12;
K22=K11;
% evaluating the Coefficient matrices for the time history 1 to 80 seconds
by the stepping of 0.1 .
t=0:0.1:80;
m=8;
M=2;
Mr=0.2;
x=1;
U=2; % the flow velocity
Omega=90;
EI=8.9782;
FUNM11 = matlabFunction(M11);
Mmatrix11 = feval(FUNM11);
FUNG11 = matlabFunction(G11);
Gmatrix11 = feval(FUNG11,t,U,Mr,L_dot,L);
FUNG12 = matlabFunction(G12);
Gmatrix12 = feval(FUNG12, t,x,Omega);
FUNK11 = matlabFunction(K11);
Kmatrix11 = feval(FUNK11, t,M,x,Omega,m,U,EI);
FUNK12 = matlabFunction(K12);
Kmatrix12 = feval(FUNK12, t,M,x,U,m,Omega);
Mmatrix22=Mmatrix11;
Gmatrix21=-Gmatrix12;
Gmatrix22=Gmatrix11;
Kmatrix21=-Kmatrix12;
Kmatrix22=Kmatrix11;
% Assembling the Coeficient matrices
Q=[Q_v;Q_w];
Mmatrix=[Mmatrix11 ,zeros(size(Mmatrix11)); zeros(size(Mmatrix11))
Mmatrix22];
Gmatrix=[Gmatrix11 Gmatrix12; Gmatrix21 Gmatrix22];
Kmatrix=[Kmatrix11 Kmatrix12; Kmatrix21 Kmatrix22];
After assembling my coefficient matrices I try to solve this algebric equation for w:
eqn=det(-w^2.*Mmatrix+i*w.*Gmatrix+Kmatrix)==0;
which w is the frequency of the system.
I was wondering if there was some problem with my Matlab code for solving a 2nd order differential equation. The equation is y"+cy'+12.5y=2.5cos(wt). This is the code I was using:
function [ dydt ] = order2( t,y )
dydt = zeros(size(y));
c=2.5;
%c=0.25;
%c=0.025;
w=sqrt(12.5-(c^2/4));
a = 2.5;
b = 12.5;
r = 2.5*cos(w*t);
dydt(1) = y(2);
dydt(2) = r -a*y(2) - b*y(1);
end
Code inputted into command window:
>> tspan = [0 40];
y0 = [1,2];
[t,y]=ode45(#order2,tspan,y0);
plot(t,y(:,1))
The problem is, this code does seem to work (it outputs a graph), but when I use each of those c values, the graph it produces looks almost the same. I thought that there would be a significant difference between the graphs. Does it make sense for the results to be almost the same or is there something off with my code?
If you implement the equation correctly, so that indeed a=c and the numerical equation actually represents the resonance case, then you also get 3 different graphs. Below they are shown in one diagram. One can see how the saturation amplitude depends on the friction coefficient, low friction large amplitude and vv.
b = 12.5;
def derivs(y,t,c):
w = (b-c**2/4)**0.5
return [ y[1], 2.5*np.cos(w*t) - c*y[1] - b*y[0] ]
tspan = np.linspace(0,20,501)
cs = [2.5, 0.25, 0.025 ]
sols = [ odeint(lambda y,t: derivs(y,t,c), [1.,2.], tspan) for c in cs]
for c,sol in zip(cs, sols): plt.plot(tspan, sol[:,0], label="c=%6f"%c)
plt.legend(loc="best");plt.show()
My friends and I have been struggling to generate a 2-D plot in MATLAB with
$\eta_1$ and $\eta_2$ both varying in $0:0.01:1$ and the z-axis given by color.
We have a system of 8 differential equations, with HIVinf representing the total new HIV infections in a population over 1 year (HIVinf is obtained by integrating a function of $\eta_1, \eta_2$).
We are looping through $\eta_1$ and $\eta_2$ (two 'for' loops) with the ode45 solver within the 'for' loops.
Based on our prior numerical results, we should be getting much color variation in the 2D-plot. There should be patterns of darkness (high concentration of HIVinfections) along the edges of the plot, and lightness along the diagonals (low concentrations).
However, the following snippet does not produce what we want (I have attached the figure below).
[X,Y] = meshgrid(eta_11,eta_22);
figure;
pcolor(X,Y,AA);
shading interp;
I have attached the code below, as concisely as possible. The function ydot works fine (it is required to run ode45).
We would greatly appreciate if you could help us fix the snippet.
function All()
global Lambda mu mu_A mu_T beta tau eta_1 eta_2 lambda_T rho_1 rho_2 gamma
alpha = 20;
TIME = 365;
eta_11 = zeros(1,alpha);
eta_22 = zeros(1,alpha);
AA = zeros(1,alpha);
BB = zeros(1,alpha);
CC = zeros(1,alpha);
for n = 1:1:alpha
for m = 1:1:alpha
Lambda = 531062;
mu = 1/25550;
mu_A = 1/1460;
mu_T = 1/1825;
beta = 187/365000;
tau = 4/365;
lambda_T = 1/10;
rho_1 = 1/180;
rho_2 = 1/90;
gamma = 1/1000;
eta_1 = (n-1)./(alpha-1);
eta_11(m) = (m-1)./(alpha-1);
eta_2 = (m-1)./(alpha-1);
eta_22(m) = (m-1)./(alpha-1);
y0 = [191564208, 131533276, 2405629, 1805024, 1000000, 1000000, 500000, 500000];
[t,y] = ode45('SimplifiedEqns',[0:1:TIME],y0);
N = y(:,1)+y(:,2)+y(:,3)+y(:,4)+y(:,5)+y(;,6)+y(:,7)+y(:,8);
HIVinf1=[0:1:TIME];
HIVinf2=[beta.*(S+T).*(C1+C2)./N];
HIVinf=trapz(HIVinf1,HIVinf2);
AA(n,m) = HIVinf;
end
end
[X,Y] = meshgrid(eta_11,eta_22);
figure;
pcolor(X,Y,AA);
shading interp;
function ydot = SimplifiedEqns(t,y)
global Lambda mu mu_A mu_T beta tau eta_1 eta_2 lambda_T rho_1 rho_2 gamma
S = y(1);
T = y(2);
H = y(3);
C = y(4);
C1 = y(5);
C2 = y(6);
CM1 = y(7);
CM2 = y(8);
N = S + T + H + C + C1 + C2 + CM1 + CM2;
ydot = zeros(8,1);
ydot(1)=Lambda-mu.*S-beta.*(H+C+C1+C2).*(S./N)-tau.*(T+C).*(S./N);
ydot(2)=tau.*(T+C).*(S./N)-beta.*(H+C+C1+C2).*(T./N)-(mu+mu_T).*T;
ydot(3)=beta.*(H+C+C1+C2).*(S./N)-tau.*(T+C).*(H./N)-(mu+mu_A).*H;
ydot(4)=beta.*(H+C+C1+C2).*(T./N)+tau.*(T+C).*(H./N)- (mu+mu_A+mu_T+lambda_T).*C;
ydot(5)=lambda_T.*C-(mu+mu_A+rho_1+eta_1).*C1;
ydot(6)=rho_1.*C1-(mu+mu_A+rho_2+eta_2).*C2;
ydot(7)=eta_1.*C1-(mu+rho_1+gamma).*CM1;
ydot(8)=eta_2.*C2-(mu+rho_2+gamma.*(rho_1)./(rho_1+rho_2)).*CM2+(rho_1).*CM1;
end
end
Ok, I don't really know much about how the plot should look like, but your eta_11 and eta_22 are variables which are indexed only on the inner loop. That means that when n=1, m=1,2,3,...,alpha your eta_11/eta_22 will be a vector whose elements 1,2,3,...,alpha will be overwritten for every n. Since your meshgrid is outside of the loop, that could be a problem. Usually if you are plotting functions of two variables and you have said variables in 2 nested loops you just ignore the meshgrid. Like this
Option 1:
x=[0:0.01:1];
[x1,x2]=meshgrid(x,x);
y=x1+cos(x2);
contour(x,x,y,30);
Option 2
x=[0:0.01:1];
for i=1:101 %length(x)
for j=1:101
y(i,j)=x1(i)+cos(x2(j)); % It is important to index y with both
% loop variables
end
end
contour(x,x,y,30)
I want to program forward euler for a system om 4 differential equation. These are:
x'(t)=u
y'(t)=v
u'(t)=-kx*u*V
v'(t)=-g-ky(v*V)
where kx,ky and g are constants and V=sqrt(u^2+v^2)
The first thing that I have dones is that I have called:
w_1'=x'
w_2'=y'
w_3'=u'
w_4'=v'
so I can express the 4 system of DE in terms of w:
w_3
w_4
-kx*w_3+sqrt((w_3)^2+(w_4)^2)
-g-ky*w_4*(sqrt((w_3)^2+(w_4)^2)
Now to the MATLAB code:
x0 = 0;
N = 16000;
h = 2./N;
kx=0.020;
ky=0.065;
g=9.81;
x_i = [0 ; 1.5; 19*cos(45); 19*sin(45)]; % initialconditons
for i = 1:N;
x_n = x0 + (i-1).*h;
diff=[x_n;x_n;-kx*(x_n*sqrt((x_n).^2+(x_n).^2));-g-ky*x_n*sqrt((x_n).^2+(x_n).^2)];
e= x_i + h.*diff; %euluerforward
end
is this correct? or should i replace the x_n with the intialconditions? like x_i(3) etc
I think you'll find great help in having a look at the euler.m code published on the following URL:
Initial Value Problems, Numerical Analysis