I've been trying to create a biological model with weekly drug injections that causes few of the immune cells types to react only 7-14 days .The model consists of a for loop with index of the number of weeks, and lags array for the delays.
However, if I change delay of 7 days to 14 days I get the same solution.
Is there any problem with my code?
would really appreciate if you have any idea how to fix it.
%paramaters and delay
a=6;
b=10;
c=9;
lags = [7 ];
y0 = [ 200 9.5*5*10^3 0.5*5*10^3 10^3 100 100];
tspan =[0,6/24];
n_weeks =6;
opt = odeset('AbsTol',1e-9,'RelTol',1e-6);
%Modeling 3 injections
for i=1:3
sol =dde23(#(t,y,Z)ddefun(t,y,Z,a,b,c),lags,y0,tspan,opt);
semilogy(sol.x,sol.y(1,:),'r',sol.x,sol.y(2,:),'k',sol.x,sol.y(3,:),'b',sol.x,sol.y(4,:),'m',sol.x,sol.y(5,:),'g',sol.x,sol.y(6,:),'y')
legend('1','2','3','4','5','6')
xlabel('Time t');
ylabel('Solution y');
hold on
y0=sol.y(:,end);
tspan = [7*(i-1)+6/24,7*(i-1)+22/24];
m1=0;
sol =dde23(#(t,y,Z)ddefun(t,y,Z,a,b,c),lags,y0,tspan,opt);
semilogy(sol.x,sol.y(1,:),'r',sol.x,sol.y(2,:),'k',sol.x,sol.y(3,:),'b',sol.x,sol.y(4,:),'m',sol.x,sol.y(5,:),'g',sol.x,sol.y(6,:),'y')
legend('1','2','3','4','5','6')
xlabel('Time t');
ylabel('Solution y');
hold on
y0=sol.y(:,end);
mu1=0;
m1=0;
tspan = [7*(i-1)+22/24,7*i];
sol =dde23(#(t,y,Z)ddefun(t,y,Z,a,b,c),lags,y0,tspan,opt);
semilogy(sol.x,sol.y(1,:),'r',sol.x,sol.y(2,:),'k',sol.x,sol.y(3,:),'b',sol.x,sol.y(4,:),'m',sol.x,sol.y(5,:),'g',sol.x,sol.y(6,:),'y')
legend('1','2','3','4','5','6')
xlabel('Time t');
ylabel('Solution y');
hold on
y0=sol.y(:,end);
tspan = [7*i,7*i+6/24];
end
hold off
%-------------------------------------------
function dydt = ddefun(t,y,Z,a,b,c) %equation being solved
ylag1 = Z(:,1);
dydt = [ a*y(1);
b*ylag1(2)*y(1);
c*y(1)+a*ylag1(3);%
y(4);
ylag1(5)*ylag1(6);
y(6)];%
end
Related
I am trying to create a figure showing the solution I obtained through Jacobi iteration along with the true solution, as well as the error of the Jacobi solution.
The figure I'm trying to create should consist of two plots.
I used the subplot command, to split the figure into
an upper and lower axes and I wrote the for loop that calculates the Jacobi iterations and the error. The loop is going to iterate 400 times using x0 as the initial guess. Before this, I calculated the true solution to the system Ax = b.
N = 30;
iter = 400;
A = toeplitz([-2 1 zeros(1, N-2)], [-2 1 zeros(1, N-2)]);
bk = ones(N,1);
for jj = 1:N
bk(jj) = cos(5*jj) + (1/2)*sin(7*jj);
end
x = A\bk;
D = diag(diag(A));
T = A - D;
x0 = zeros(N,1);
error = zeros(iter,1);
M = -D\T;
g = D\bk;
for nn = 1:iter
x0 = M*x0 + g;
error(nn) = norm(x - x0,2);
end
subplot(2,1,1)
plot(x0(1:N,1),'ro');
ylabel('Solution','FontSize',22);
title('Solution by Jacobi Iteration','FontSize',22);
xlim([0 pi]);
ylim([-5 5]);
xticks(0:0.5:3);
yticks(-5:5:5);
subplot(2,1,2)
plot(error(1:N),'ro')
ylabel('Error','FontSize',22);
xlabel('t','FontSize',22);
xlim([0 pi]);
ylim([0 0.1]);
xticks(0:0.5:3);
yticks(0:0.05:0.1);
The upper window should show the true solution in red circles connected by solid lines. The lower window show show the error as red
circles connected by dotted lines. When I ran my code, only 3 red circles appeared in the upper window and nothing was plotted in the lower window. I'm still bad at plotting iterations of a loop. Can someone help me with plotting the solutions and errors I calculated?
The xlim and ylim statements are not representative of the data.
x0 and x have N elements (30 here), and the elements of x and x0 span -2 to 2 in this setup.
error has iter elements (400 here), and the elements of error go from 4 to about 0.01.
For these plots, the element index maps to the horizontal x-axis, and their values to the y-axis. I think this plot setup should give you the result you desire (I probably changed more than actually needed):
subplot(2,1,1);
plot(1:N, x0(1:N,1), 'ro', 1:N, x,'k+');
title('Solution by Jacobi Iteration','FontSize',22);
ylabel('Solution','FontSize',22);
xlim([1, N]);
ylim([-3, 3]);
xticks(1:N);
yticks(-3:0.5:3);
subplot(2,1,2)
semilogy(1:iter, error(1:iter),'ro')
ylabel('Error','FontSize',22);
xlabel('t','FontSize',22);
xlim([1 iter]);
ylim([0 4]);
xticks(0:25:400);
Entire Code
clear;clc
d1= 0.34;
d2= 0.15;
d3=0.27;
deltat = 1;
W = 1000.0;
X = 0.0;
Y = 0.0;
Z=0.00;
for t = 1:30
W(t+1) = W(t) - deltat*d1*W(t);
X(t+1) = X(t) - deltat*d2*X(t);
X(t+1) = X(t+1) + deltat*d1*W(t);
Y(t+1) = Y(t)- deltat*d3*Y(t);
Y(t+1) = Y(t+1) + deltat*d2*X(t);
Z(t+1)=Z(t);
Z(t+1)=Z(t+1) + deltat*d3*Y(t);
end
figure(1)
plot(W(t),X(t),Y(t),Z(t))
title('Radioactive Decay','FontSize',14)
legend('W','X','Y','Z');
xlabel('Time (days)');
ylabel('Kilograms');
W(30)
X(30)
Y(30)
Z(30)
Part I am having trouble with
plot(W(t),X(t),Y(t),Z(t))
Issue
I was trying to plot the W, X, Y and Z graphs together on one plot to show the change over the 30 days period I have assigned, however it seems like the formatting I put is wrong because the resultant plot ends up being empty.
The problem occurs because the plot-function is only for two-dimensional line plots. Documentation: https://se.mathworks.com/help/matlab/ref/plot.html
The way you have written it is plotting W(t) on one axis, X(t) on another axis and so forth summing up to four dimensions.
Use
plot(W); hold on;
plot(X); hold on;
plot(Y); hold on;
plot(Z);
I want to make a code for a model of competing three species with diffusion to get plots ( 2d plot u, v, w against time ) My model is
u'=d1u_xx+(r1-a1u-b1v-c1w)u
v'=d2v_xx+(r2-a2u-b2v-c2w)v
w'=d3w_xx+(r3-a3u-b3v-c3w)w
where d1=d2=d3=1;
r1=1.5; r2=2.65; r3=3.45;
a1=0.1; b1=0.3; c1=0.01; b2=0.2;
a2=0.3; c2=0.2; c3=0.2; a3=0.01; b3=0.1.
I did the code for this model without diffusion as follow
% Define diff. equations, time span and initial conditions, e.g.
tspan = [0 20];
y0 = [0.2 0.2 0.2];
r1=1.5; r2=2.65; r3=3.45;
a1=0.1; a2=0.3; a3=0.01;
b1=0.3; b2=0.2; b3=0.1;
c1=0.01; c2=0.2; c3=0.2;
dy = #(t,y) [
(r1-a1*y(1)-b1*y(2)-c1*y(3))*y(1);
(r2-a2*y(1)-b2*y(2)-c2*y(3))*y(2);
(r3-a3*y(1)-b3*y(2)-c3*y(3))*y(3)];
% Solve differential equations
[t,y] = ode45(dy, tspan, y0);
% Plot all species against time
figure(1)
plot(t,y)
Resulting in following figure:
how to do it with diffusion like in this paper figure 10u, v, w
I'm trying to simulate the time behavior for a physical process governed by a system of ODEs. When I switch the width of the input pulse from 20 to 19, there is no depletion of the y(1) state, which doesn't make sense physically. What am I doing wrong? Am I using ode45 incorrectly?
function test
width = 20;
center = 100;
tspan = 0:0.1:center+50*(width/2);
[t,y] = ode45(#ODEsystem,tspan,[1 0 0 0]);
plot(t,y(:,1),'k*',t,y(:,2),'k:',t,y(:,3),'k--',t,y(:,4),'k');
hold on;
axis([center-3*(width/2) center+50*(width/2) -0.1 1.1])
xlabel('Time')
ylabel('Relative values')
legend({'y1','y2','y3','y4'});
function dy = ODEsystem(t,y)
k1 = 0.1;
k2 = 0.000333;
k3 = 0.1;
dy = zeros(size(y));
% rectangular pulse
I = rectpuls(t-center,width);
% ODE system
dy(1) = -k1*I*y(1);
dy(2) = k1*I*y(1) - k2*y(2);
dy(3) = k2*y(2) - k3*I*y(3);
dy(4) = k3*I*y(3);
end
end
You are changing the parameters of your your ODEs discontinuously in time. This results in a very stiff system and less accurate, or even completely wrong, results. In this case, because the your ODE is so simple when I = 0, an adaptive solver like ode45 will take very large steps. Thus, there's a high probability that it will step right over the region where you inject the impulse and never see it. See my answer here if you're confused as to why the code in your question misses the pulse even though you've specified tspan to have (output) steps of just 0.1.
In general it is a bad idea to have any discontinuities (if statements, abs, min/max, functions like rectpuls, etc.) in your integration function. Instead, you need to break up the integration and calculate your results piecewise in time. Here's a modified version of your code that implements this:
function test_fixed
width = 19;
center = 100;
t = 0:0.1:center+50*(width/2);
I = rectpuls(t-center,width); % Removed from ODE function, kept if wanted for plotting
% Before pulse
tspan = t(t<=center-width/2);
y0 = [1 0 0 0];
[~,out] = ode45(#(t,y)ODEsystem(t,y,0),tspan,y0); % t pre-calculated, no need to return
y = out;
% Pulse
tspan = t(t>=center-width/2&t<=center+width/2);
y0 = out(end,:); % Initial conditions same as last stage from previous integration
[~,out] = ode45(#(t,y)ODEsystem(t,y,1),tspan,y0);
y = [y;out(2:end,:)]; % Append new data removing identical initial condition
% After pulse
tspan = t(t>=center+width/2);
y0 = out(end,:);
[~,out] = ode45(#(t,y)ODEsystem(t,y,0),tspan,y0);
y = [y;out(2:end,:)];
plot(t,y(:,1),'k*',t,y(:,2),'k:',t,y(:,3),'k--',t,y(:,4),'k');
hold on;
axis([center-3*(width/2) center+50*(width/2) -0.1 1.1])
xlabel('Time')
ylabel('Relative values')
legend({'y1','y2','y3','y4'});
function dy = ODEsystem(t,y,I)
k1 = 0.1;
k2 = 0.000333;
k3 = 0.1;
dy = zeros(size(y));
% ODE system
dy(1) = -k1*I*y(1);
dy(2) = k1*I*y(1) - k2*y(2);
dy(3) = k2*y(2) - k3*I*y(3);
dy(4) = k3*I*y(3);
end
end
See also my answer to a similar question.
I have a 1D heat diffusion code in Matlab which I was using on a timescale of 10s of years and I am now trying to use the same code to work on a scale of millions of years. Obviously if I keep my timestep the same this will take ages to calculate but if I increase my timestep I encounter numerical stability issues.
My questions are:
How should I approach this problem? What affects the maximum stable timestep? And how do I calculate this?
Many thanks,
Alex
close all
clear all
dx = 4; % discretization step in m
dt = 0.0000001; % timestep in Myrs
h=1000; % height of box in m
nx=h/dx+1;
model_lenth=1; %length of model in Myrs
nt=ceil(model_lenth/dt)+1; % number of tsteps to reach end of model
kappa = 1e-6; % thermal diffusivity
x=0:dx:0+h; % finite difference mesh
T=38+0.05.*x; % initial T=Tm everywhere ...
time=zeros(1,nt);
t=0;
Tnew = zeros(1,nx);
%Lower sill
sill_1_thickness=18;
Sill_1_top_position=590;
Sill_1_top=ceil(Sill_1_top_position/dx);
Sill_1_bottom=ceil((Sill_1_top_position+sill_1_thickness)/dx);
%Upper sill
sill_2_thickness=10;
Sill_2_top_position=260;
Sill_2_top=ceil(Sill_2_top_position/dx);
Sill_2_bottom=ceil((Sill_2_top_position+sill_2_thickness)/dx);
%Temperature of dolerite intrusions
Tm=1300;
T(Sill_1_top:Sill_1_bottom)=Tm; %Apply temperature to intrusion 1
% unit conversion to SI:
secinmyr=24*3600*365*1000000; % dt in sec
dt=dt*secinmyr;
%Plot initial conditions
figure(1), clf
f1 = figure(1); %Make full screen
set(f1,'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
plot (T,x,'LineWidth',2)
xlabel('T [^oC]')
ylabel('x[m]')
axis([0 1310 0 1000])
title(' Initial Conditions')
set(gca,'YDir','reverse');
%Main calculation
for it=1:nt
%Apply temperature to upper intrusion
if it==10;
T(Sill_2_top:Sill_2_bottom)=Tm;
end
for i = 2:nx-1
Tnew(i) = T(i) + kappa*dt*(T(i+1) - 2*T(i) + T(i-1))/dx/dx;
end
Tnew(1) = T(1);
Tnew(nx) = T(nx);
time(it) = t;
T = Tnew; %Set old Temp to = new temp for next loop
tmyears=(t/secinmyr);
%Plot a figure which updates in the loop of temperature against depth
figure(2), clf
plot (T,x,'LineWidth',2)
xlabel('T [^oC]')
ylabel('x[m]')
title([' Temperature against Depth after ',num2str(tmyears),' Myrs'])
axis([0 1300 0 1000])
set(gca,'YDir','reverse');%Reverse y axis
%Make full screen
f2 = figure(2);
set(f2,'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
drawnow
t=t+dt;
end
The stability condition for an explicit scheme like FTCS is governed by $r = K dt/dx^2 < 1/2$ or $dt < dx^2/(2K)$ where K is your coefficient of diffusion. This is required in order to make the sign of the 4th order derivative leading truncation error term be negative.
If you do not want to be limited by timestep I suggest using an implicit scheme (albeit at a higher of computational cost than an explicit scheme). This can be achieved simply by using backward Euler for the diffusion term instead of forward Euler. Another option is Crank-Nicholson which is also implicit.
#Isopycnal Oscillation is totally correct in that the maximum stable step is limited in an explicit scheme. Just for reference this is usually referred to as the discrete Fourier number or just Fourier number and can be looked up for different boundary conditions.
also the following may help you for the derivation of the Implicit or Crank-Nicholson scheme and mentions stability Finite-Difference Approximations
to the Heat Equation by Gerald W. Recktenwald.
Sorry I don't have the rep yet to add comments