Post on 20-Dec-2015
transcript
Page 1 of 26
Contents Introduction ............................................................................................................................................................................ 2
Stability Analysis ..................................................................................................................................................................... 2
Numerics ................................................................................................................................................................................. 3
Forward Euler Method (Results) ............................................................................................................................................. 5
Runge-Kutta Method, 4th Order (Results) ............................................................................................................................... 8
Trapezoidal Method (Results) ............................................................................................................................................... 11
Backwards Euler Method (Results) ....................................................................................................................................... 14
Forward Euler Method (MATLAB Code) ............................................................................................................................... 17
Runge-Kutta Method, 4th Order (MATLAB Code) .................................................................................................................. 18
Trapezoidal Method (MATLAB Code) ................................................................................................................................... 20
Backwards Euler Method (MATLAB Code) ............................................................................................................................ 23
References ............................................................................................................................................................................ 26
Page 2 of 26
Introduction The main equation: Fisherβs equation, also known as the FisherβKolmogorov equation and the FisherβKPP
equation
π£π = π·π£π¦π¦ + ππ£ (1 βπ£
π)
π¦ β (βπΏ, πΏ), π‘ > 0
π£π₯(Β±πΏ, π‘) = 0
π£(π¦, 0) = π(π¦)
Fisherβs Equation governs the evolution of a population of density π£, subject to diffusion and logistic growth
(given by the ππ£ (1 βπ£
π) term. Discovered in 1937 by Ronald Fisher, this model was designed to answer
whether the gene has a typical speed of propagation. This model is a semilinear equation, where diffusion is
coupled with logistic growth through the term ππ£ (1 βπ£
π). The parameter π represents a biological potential
(net birth-death rate), π is the carrying capacity of the habitat.
Non-dimensionalization:
π‘ = ππ; π₯ = βπ
π·π¦; π’ =
π£
π
π’π‘ = π’π₯π₯ + π’(1 β π’), π‘ > 0
π₯ β [βπ,π], π‘ > 0
π’π₯(Β±π, π‘) = 0
π’(π₯, 0) = π(π₯)
Stability Analysis
We start with assuming a travelling wave solution for π’(π₯, π‘), i.e.:
π’(π₯, π‘) = π’(π₯ β ππ‘); π =speed of the wave
π’π‘ = π’π₯π₯ + π’(1 β π’) β (π’(π₯ β ππ‘))π‘= (π’(π₯ β ππ‘))
π₯π₯+ π’(π₯ β ππ‘) β (π’(π₯ β ππ‘))
2
Let π₯ β ππ‘ = π§ β π’(π§)π‘ = π’(π§)π₯π₯ + π’(π§) β π’2(π§)
β βππ’β²(π§) = π’β²β²(π§) + π’(π§) β π’2(π§)
β π’β²β²(π§) + ππ’β²(π§) + π’(π§) β π’2(π§) = 0
Let
π’β² = π£ = π(π’, π£)
π£β² = βππ£ β π’ + π’2 = π(π’, π£)
Critical points: (0,0) and (1,0)
Page 3 of 26
π½(π’, π£) = (ππ’ ππ£ππ’ ππ£
) = (0 1
β1 + 2π’ βπ)
π½(0,0) = π½1 = (0 1β1 βπ
) ; π½(1,0) = π½2 = (0 11 βπ
)
π0,0 =βπ Β± βπ2 β 4
2
From above we get that the point (0,0) is a stable node for all values greater than or equal to 2 i.e. for π β₯ 2
and for π = 2, we have a degenerate node. For π2 < 4, the system is a stable spiral.
π1,0 =βπ Β± βπ2 + 4
2
From above we get that the point(1,0) is a saddle point because both eigenvalues are real but one is positive
and one is negative.
Here is the phase portrait for Fisherβs equation for π = 2:
Note that the two critical points are marked in red. It is easy to see both the separatrix and stable degenerate
node.
Numerics π’π‘ = π’π₯π₯ + π’(1 β π’)
We start with computing the approximation for the spatial derivative for π’(π₯, π‘)
Page 4 of 26
π’π₯π₯(π₯, π‘) βπ’(π₯ β Ξπ₯, π‘) β 2π’(π₯, π‘) + π’(π₯ + Ξπ₯, π‘)
(Ξπ₯)2
The purpose of this approximation is to take our nonlinear PDE and approximate it as a nonlinear, time
dependent ODE.
β΄, π’π‘ β1
(Ξπ₯)2[π’(π₯ β Ξπ₯, π‘) β 2π’(π₯, π‘) + π’(π₯ + Ξπ₯, π‘)] + π’(π₯, π‘)(1 β π’(π₯, π‘)) = π(π₯, π‘) β¦ 1
We can re-write the above equation using indices:
π’π β1
(Ξπ₯)2[π’(π₯πβ1, π‘π) β 2π’(π₯π, π‘π) + π’(π₯π+1, π‘π)] + π’(π₯π, π‘π)(1 β π’(π₯π, π‘π)) = π(π₯π , π‘π)β¦ 2
Note the subscript on π’ changes from π‘ to π. As π is our time index, the above equation is at the time step
π‘ = πβ, where β = Ξπ‘. In other words, for the given space point, π₯π, we calculate the time evolution by
keeping the space index,π, fixed and going to the next time index, π + 1. We perform this calculation using
nested loops.
Also, in order to account for the no-flux boundary conditions π’π₯(Β±π, π‘) = 0, the space points at π = 1 and π =
π need to be treated differently. These points are approximated by the following equations:
π’π β(β2π’(π₯1, π‘π) + 2π’(π₯2, π‘π))
(Ξπ₯)2+ π’(π₯1, π‘π)(1 β π’(π₯1, π‘π))β¦ 3
The above equation is at π₯ = βπ (left boundary point), and as before, the equation represents the time
evolution of this boundary point at index π = 1.
π’π β(β2π’(π₯π , π‘π) + 2π’(π₯πβ1, π‘π))
(Ξπ₯)2+ π’(π₯π , π‘π)(1 β π’(π₯π , π‘π))β¦ 4
i.e.:
π’π = π(π₯π, π‘π) β
{
(β2π’(π₯1, π‘π) + 2π’(π₯2, π‘π))
(Ξπ₯)2+ π’(π₯1, π‘π)(1 β π’(π₯1, π‘π)); π = 1
1
(Ξπ₯)2[π’(π₯πβ1, π‘π) β 2π’(π₯π , π‘π) + π’(π₯π+1, π‘π)] + π’(π₯π , π‘π)(1 β π’(π₯π, π‘π)); π = 2,β¦ ,π β 1
(β2π’(π₯π , π‘π) + 2π’(π₯πβ1, π‘π))
(Ξπ₯)2+ π’(π₯π , π‘π)(1 β π’(π₯π , π‘π)); π = π
The above equation is at π₯ = βπ (left boundary point), and as before, the equation represents the time
evolution of this boundary point at index π = π.
The solution technique employed in this assignment is as follows: we approximate the spatial derivative with a
G1 type grid, we assign the number of nodes to the said grid (N), and the solution to the time derivative is
obtained by simply βmarchingβ technique i.e. we just approximate the integral for the time using different
methods (namely forward Euler, backwards Euler, RK4, and Trapezoidal). The following schematic shows a
pictorial description on this summary.
Page 5 of 26
Each column vector (red), is a time snapshot of the entire spatial domain, and each row vector (green) is the
evolution of the solution at that given point π₯π for the entire time from 0 to π‘π.
In order to apply different numerical techniques to the Fisherβs equation, we take the spatial approximation
from equation 2 , i.e. π(π₯π, π‘π), and apply whatever numerical method we desire.
Lastly, here are the two initial conditions given for the assignment:
π1(π₯) = {0.990
|π₯| β€ 15|π₯| > 15
π2(π₯) = {1
4cos2
ππ₯
100
|π₯| β€ 5|π₯| > 5
Also, in order to apply the implicit methods, we will need to compute the Jacobian matrix for the given
system.
The Jacobian for Fisherβs Equation problem is given as:
π½(π) =ππ
ποΏ½ββοΏ½=
{
β
2
(Ξπ₯)2+ 1 β 2π’π; ππ π = π
1
(Ξπ₯)2; ππ |π β π| = 1
0; ππ‘βπππ€ππ π
Forward Euler Method (Results)
The definition of the method is given as:
π¦π+1 = π¦π + βπ(π¦π, π‘π)
β΄,we solve: π’(π₯π, π‘π+1) = π’(π₯π, π‘π) + βπ(π₯π, π‘π)
Page 8 of 26
Runge-Kutta Method, 4th Order (Results)
The definition of the method is given as:
π’π+1 = π’π +β
6(π1 + 2π2 + 2π3 + π4)
π1 = π(π‘π, π’π)
π2 = π (π‘π +β
2, π’π +
βπ12)
π3 = π (π‘π +β
2, π’π +
βπ22)
π4 = π(π‘π + β, π’π + βπ3)
As our discretized system is βtime independentβ, the π values simply become:
π1 = π(π’π) = π’π
π2 = π (π’π +βπ12) = π’π +
βπ12
π3 = π (π’π +βπ22) = π’π +
βπ22
π4 = π(π’π + βπ3) = π’π + βπ3
And since:
π’π = π(π₯π, π‘π) β
{
(β2π’(π₯1, π‘π) + 2π’(π₯2, π‘π))
(Ξπ₯)2+ π’(π₯1, π‘π)(1 β π’(π₯1, π‘π)); π = 1
1
(Ξπ₯)2[π’(π₯πβ1, π‘π) β 2π’(π₯π , π‘π) + π’(π₯π+1, π‘π)] + π’(π₯π , π‘π)(1 β π’(π₯π, π‘π)); π = 2,β¦ ,π β 1
(β2π’(π₯π , π‘π) + 2π’(π₯πβ1, π‘π))
(Ξπ₯)2+ π’(π₯π , π‘π)(1 β π’(π₯π , π‘π)); π = π
We are numerically solving the following system: π’(π₯π, π‘π+1 ) = π’(π₯π, π‘π) +β
6(π1 + 2π2 + 2π3 + π4)
Page 11 of 26
Trapezoidal Method (Results) The definition of the method is given as:
π¦π+1 = π¦π +1
2β[π(π¦π, π‘π) + π(π¦π+1, π‘π+1)]
However, since we have a nonlinear system, we need to solve for π’π+1 using Newtonβs Method. For the n-
dimensional case, the Newtonβs Method is given as:
οΏ½ΜοΏ½π+1 = π’π β π½β1(π(π’π))π(π’π)
The Jacobian is defined as:
π½(π) =ππ
ποΏ½ββοΏ½=
{
β
2
(Ξπ₯)2+ 1 β 2π’π; ππ π = π
1
(Ξπ₯)2; ππ |π β π| = 1
0; ππ‘βπππ€ππ π
The computational challenge with such an approach is that we cannot choose β and Ξπ₯ with the same degree
of freedom as we were allowed in the explicit methods. This is due to the fact that the Jacobian matrix must
be a square matrix in order for the inverse to exist. Therefore, the grid size for both π‘ and π₯ has to be the
same.
The following results were obtained, using Newtonβs method with a tolerance of 10β4. The general shape of
the result matches that of the two explicit methods discussed above.
The approximation, οΏ½ΜοΏ½π+1, obtained from Newtonβs Method is implemented in the following manner:
π’π+1 = π’π +1
2β[π’π + οΏ½ΜοΏ½π+1]
Page 14 of 26
Backwards Euler Method (Results) The definition of the method is given as:
π¦π+1 = π¦π + βπ(π¦π+1, π‘π+1)
The same discussion of nonlinearity and accuracy from Trapezoidal method applies to Backwards Euler
method as well. The only different here is the manner of implementation of the approximation, οΏ½ΜοΏ½π+1. For
backwards Euler Method, the results are given as:
π’π+1 = π’π + βοΏ½ΜοΏ½π+1
Results using IC = π1(π₯):
2D Plot
Page 17 of 26
Forward Euler Method (MATLAB Code) %Code for Forward Euler Method
clear
clc
%Initializing constants, parameters, and data structures
N = 200; %Number of space points
M = 50; %Space endpoint
t0 = 0; %Initial time
tf = 20; %Final time
h = (tf-t0)/(N-1); %Delta t
t = t0:h:tf; %Time vector
dx = (2*M)/(N-1); %Delta x
x = -M:dx:M; %Space vector
g1 = zeros(length(x),1); %IC 1 Vector
g2 = zeros(length(x),1); %IC 2 Vector
u = zeros(N,length(t)); %Solution Matrix
%Create IC g1
for i = 1:N
if abs(x(i))<= 15
g1(i,1) = 0.99;
end
end
%Create IC g2
for i = 1:N
if abs(x(i))<= 5
g2(i,1) = 0.25*(cos((pi*x(i))/10))^2;
end
end
%---------------Solving using Forward Euler and IC 1----------------------%
u(:,1) = g1(:,1);
%i = 1
for n = 1:length(t)-1
fn = (1/dx^2)*(-2*u(1,n) + 2*u(2,n))+u(1,n)*(1-u(1,n));
u(1,n+1)=u(1,n)+h*(fn);
end
%i = 2,3,...,N-1
for n = 1:length(t)-1
fn = (1/dx^2).*(u(1:N-2,n)-2.*u(2:N-1,n)+u(3:N,n))+u(2:N-1,n).*(1-u(2:N-1,n));
u(2:N-1,n+1)=u(2:N-1,n)+h*(fn);
end
%i = N
for n = 1:length(t)-1
fn = (1/dx^2)*(2*u(N-1,n)-2*u(N,n))+u(N,n)*(1-u(N,n));
u(N,n+1)=u(N,n)+h*(fn);
end
%Plot solutions using IC g1
figure1 = figure;
plot(x,u(:,1),'black','linewidth',2)
hold on
%This part produces profile for each second starting at 1 to 20
for i = 2:length(t)
if rem(t(i),1) == 0
plot(x,u(:,i),'r')
end
end
legend('t = 0','t > 0','location','northeastoutside')
title('Solution using IC = g_1, plotted every 1 second','fontsize',16)
xlabel('x','fontsize',16)
Page 18 of 26
ylabel('u(x,t_{fixed})','fontsize',16)
grid on
axis tight
hold off
%------------Solving using Forward Euler and IC 2-------------------------%
u(:,1) = g2(:,1);
%i = 1
for n = 1:length(t)-1
fn = (1/dx^2)*(-2*u(1,n) + 2*u(2,n))+u(1,n)*(1-u(1,n));
u(1,n+1)=u(1,n)+h*(fn);
end
%i = 2,3,...,N-1
for n = 1:length(t)-1
fn = (1/dx^2).*(u(1:N-2,n)-2.*u(2:N-1,n)+u(3:N,n))+u(2:N-1,n).*(1-u(2:N-1,n));
u(2:N-1,n+1)=u(2:N-1,n)+h*(fn);
end
%i = N
for n = 1:length(t)-1
fn = (1/dx^2)*(2*u(N-1,n)-2*u(N,n))+u(N,n)*(1-u(N,n));
u(N,n+1)=u(N,n)+h*(fn);
end
%Plot solutions using IC g2
figure2 = figure;
plot(x,u(:,1),'black','linewidth',2)
hold on
%This part produces profile for each second starting at 1 to 20
for i = 2:length(t)
if rem(t(i),1) == 0
plot(x,u(:,i),'r')
end
end
legend('t = 0','t > 0','location','northeastoutside')
title('Solution using IC = g_2, plotted every 1 second','fontsize',16)
xlabel('x','fontsize',16)
ylabel('u(x,t_{fixed})','fontsize',16)
grid on
axis tight
hold off
Runge-Kutta Method, 4th Order (MATLAB Code) %Code for RK4 Method
clear
clc
%Initializing constants, parameters, and data structures
N = 200; %Number of space points
M = 50; %Space endpoint
t0 = 0; %Initial time
tf = 20; %Final time
h = (tf-t0)/(N-1); %Delta t
t = t0:h:tf; %Time vector
dx = (2*M)/(N-1); %Delta x
x = -M:dx:M; %Space vector
g1 = zeros(length(x),1); %IC 1 Vector
g2 = zeros(length(x),1); %IC 2 Vector
u = zeros(N,length(t)); %Solution Matrix
%Create IC g1
for i = 1:N
if abs(x(i))<= 15
Page 19 of 26
g1(i,1) = 0.99;
end
end
%Create IC g2
for i = 1:N
if abs(x(i))<= 5
g2(i,1) = 0.25*(cos((pi*x(i))/10))^2;
end
end
%----------------------Solving using RK4 and IC 2-------------------------%
u(:,1) = g1(:,1);
%i = 1
for n = 1:length(t)-1
k1 = (1/dx^2)*(-2*u(1,n) + 2*u(2,n)) + u(1,n)*(1-u(1,n));
k2 = k1 + 0.5*h.*k1;
k3 = k2 + 0.5*h.*k2;
k4 = k3 + h.*k3;
u(1,n+1)=u(1,n) + h/6*(k1 + 2.*k2 + 2.*k3 + k4);
end
%i = 2,3,...,N-1
for n = 1:length(t)-1
for i = 2:N-1
k1 = (1/dx^2)*(u(i-1,n)-2*u(i,n)+u(i+1,n)) + u(i,n)*(1-u(i,n));
k2 = k1 + 0.5*h.*k1;
k3 = k2 + 0.5*h.*k2;
k4 = k3 + h.*k3;
u(i,n+1)=u(i,n) + h/6*(k1 + 2.*k2 + 2.*k3 + k4);
end
end
%i = N
for n = 1:length(t)-1
k1 = (1/dx^2)*(2*u(N-1,n)-2*u(N,n)) + u(N,n)*(1-u(N,n));
k2 = k1 + 0.5*h.*k1;
k3 = k2 + 0.5*h.*k2;
k4 = k3 + h.*k3;
u(N,n+1)=u(N,n) + h/6*(k1 + 2.*k2 + 2.*k3 + k4);
end
%Plot solutions using IC g1
figure1 = figure;
plot(x,u(:,1),'black','linewidth',2)
hold on
%This part produces profile for each second starting at 1 to 20
for i = 2:length(t)
if rem(t(i),1) == 0
plot(x,u(:,i),'r')
end
end
legend('t = 0','t > 0','location','northeastoutside')
title('Solution using IC = g_1, plotted every 1 second','fontsize',16)
xlabel('x','fontsize',16)
ylabel('u(x,t_{fixed})','fontsize',16)
grid on
axis tight
hold off
k1 = 0; k2 = 0; k3 = 0; k4 = 0;
u = zeros(N,length(t));
%----------------------Solving using RK4 and IC 2-------------------------%
u(:,1) = g2(:,1);
Page 20 of 26
%i = 1
for n = 1:length(t)-1
k1 = (1/dx^2)*(-2*u(1,n) + 2*u(2,n)) + u(1,n)*(1-u(1,n));
k2 = k1 + 0.5*h.*k1;
k3 = k2 + 0.5*h.*k2;
k4 = k3 + h.*k3;
u(1,n+1)=u(1,n) + h/6*(k1 + 2.*k2 + 2.*k3 + k4);
end
%i = 2,3,...,N-1
for n = 1:length(t)-1
for i = 2:N-1
k1 = (1/dx^2)*(u(i-1,n)-2*u(i,n)+u(i+1,n)) + u(i,n)*(1-u(i,n));
k2 = k1 + 0.5*h.*k1;
k3 = k2 + 0.5*h.*k2;
k4 = k3 + h.*k3;
u(i,n+1)=u(i,n) + h/6*(k1 + 2.*k2 + 2.*k3 + k4);
end
end
%i = N
for n = 1:length(t)-1
k1 = (1/dx^2)*(2*u(N-1,n)-2*u(N,n)) + u(N,n)*(1-u(N,n));
k2 = k1 + 0.5*h.*k1;
k3 = k2 + 0.5*h.*k2;
k4 = k3 + h.*k3;
u(N,n+1)=u(N,n) + h/6*(k1 + 2.*k2 + 2.*k3 + k4);
end
%Plot solutions using IC g2
figure2 = figure;
plot(x,u(:,1),'black','linewidth',2)
hold on
%This part produces profile for each second starting at 1 to 20
for i = 2:length(t)
if rem(t(i),1) == 0
plot(x,u(:,i),'r')
end
end
legend('t = 0','t > 0','location','northeastoutside')
title('Solution using IC = g_2, plotted every 1 second','fontsize',16)
xlabel('x','fontsize',16)
ylabel('u(x,t_{fixed})','fontsize',16)
grid on
axis tight
hold off
Trapezoidal Method (MATLAB Code) %Code for Trapezoidal Method clear clc
%Initializing constants, parameters, and data structures N = 200; %Number of space points M = 50; %Space endpoint t0 = 0; %Initial time tf = 20; %Final time tol = 10^-4; %Tolerance for Newton's Method h = (tf-t0)/(N-1); %Delta t t = t0:h:tf; %Time vector dx = (2*M)/(N-1); %Delta x x = -M:dx:M; %Space vector g1 = zeros(length(x),1); %IC 1 Vector g2 = zeros(length(x),1); %IC 2 Vector u = zeros(N,length(t)); %Solution Matrix J = zeros(N,length(t)); %Jacobian Matrix
Page 21 of 26
u1 = 0; u2 = 0;
%Create IC g1 for i = 1:N if abs(x(i))<= 15 g1(i,1) = 0.99; end end
%Create IC g2 for i = 1:N if abs(x(i))<= 5 g2(i,1) = 0.25*(cos((pi*x(i))/10))^2; end end
%-----------------Solving using Trapaziodal and IC 1----------------------% u(:,1) = g1(:,1);
%Deriving the Jacobian for n1 = 1:N for k1 = 1:length(t) if n1 == k1 J(n1,k1) = 1/dx^2 + 1 - 2*u(n1,k1); else if abs(k1 - n1) == 1 J(n1,k1) = 1/dx^2; else J(n1,k1) = 0; end end end end
Ji = inv(J);
%i = 1 for n = 1:length(t)-1 fn = (1/dx^2)*(-2*u(1,n) + 2*u(2,n))+u(1,n)*(1-u(1,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(2,n+1)*fn; u1 = u2; end u(1,n+1)=u(1,n)+(h/2)*(fn + u1); end
%i = 2,3,...,N-1 for n = 1:length(t)-1 fn = (1/dx^2).*(u(1:N-2,n)-2.*u(2:N-1,n)+u(3:N,n))+u(2:N-1,n).*(1-u(2:N-1,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(3:N,n+1).*fn; u1 = u2; end u(2:N-1,n+1)=u(2:N-1,n)+(h/2)*(fn + u1); end
%i = N for n = 1:length(t)-1 fn = (1/dx^2)*(2*u(N-1,n)-2*u(N,n))+u(N,n)*(1-u(N,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(N,n+1)*fn; u1 = u2; end u(N,n+1)=u(N,n)+(h/2)*(fn + u1);
Page 22 of 26
end
%Plot solutions using IC g1 figure1 = figure; plot(x,u(:,1),'black','linewidth',2) hold on %This part produces profile for each second starting at 1 to 20 for i = 2:length(t) if rem(i,10) == 0 plot(x,u(:,i),'r') end end legend('t = 0','t > 0','location','northeastoutside') title('Solution using IC = g_1, plotted every 1 second','fontsize',16) xlabel('x','fontsize',16) ylabel('u(x,t_{fixed})','fontsize',16) grid on axis tight hold off
%-----------------Solving using Trapezoidal and IC 2----------------------% u(:,1) = g2(:,1);
%Deriving the Jacobian for n1 = 1:N for k1 = 1:length(t) if n1 == k1 J(n1,k1) = 1/dx^2 + 1 - 2*u(n1,k1); else if abs(k1 - n1) == 1 J(n1,k1) = 1/dx^2; else J(n1,k1) = 0; end end end end
Ji = inv(J);
%i = 1 for n = 1:length(t)-1 fn = (1/dx^2)*(-2*u(1,n) + 2*u(2,n))+u(1,n)*(1-u(1,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(1,n+1)*fn; u1 = u2; end u(1,n+1)=u(1,n)+(h/2)*(fn + u1); end
%i = 2,3,...,N-1 for n = 1:length(t)-1 fn = (1/dx^2).*(u(1:N-2,n)-2.*u(2:N-1,n)+u(3:N,n))+u(2:N-1,n).*(1-u(2:N-1,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(2:N-1,n+1).*fn; u1 = u2; end u(2:N-1,n+1)=u(2:N-1,n)+(h/2)*(fn + u1); end
%i = N for n = 1:length(t)-1 fn = (1/dx^2)*(2*u(N-1,n)-2*u(N,n))+u(N,n)*(1-u(N,n)); u1 = fn;
Page 23 of 26
while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(1,n+1)*fn; u1 = u2; end u(N,n+1)=u(N,n)+(h/2)*(fn + u1); end
%Plot solutions using IC g2 figure1 = figure; plot(x,u(:,1),'black','linewidth',2) hold on %This part produces profile for each second starting at 1 to 20 for i = 2:length(t) if rem(i,10) == 0 plot(x,u(:,i),'r') end end legend('t = 0','t > 0','location','northeastoutside') title('Solution using IC = g_2, plotted every 1 second','fontsize',16) xlabel('x','fontsize',16) ylabel('u(x,t_{fixed})','fontsize',16) grid on axis tight hold off
Backwards Euler Method (MATLAB Code) %Code for Backwards Euler Method clear clc
%Initializing constants, parameters, and data structures N = 200; %Number of space points M = 50; %Space endpoint t0 = 0; %Initial time tf = 20; %Final time tol = 10^-4; %Tolerance for Newton's Method h = (tf-t0)/(N-1); %Delta t t = t0:h:tf; %Time vector dx = (2*M)/(N-1); %Delta x x = -M:dx:M; %Space vector g1 = zeros(length(x),1); %IC 1 Vector g2 = zeros(length(x),1); %IC 2 Vector u = zeros(N,length(t)); %Solution Matrix J = zeros(N,length(t)); %Jacobian Matrix u1 = 0; u2 = 0;
%Create IC g1 for i = 1:N if abs(x(i))<= 15 g1(i,1) = 0.99; end end
%Create IC g2 for i = 1:N if abs(x(i))<= 5 g2(i,1) = 0.25*(cos((pi*x(i))/10))^2; end end
%------------Solving using backward Euler and IC 1------------------------% u(:,1) = g1(:,1);
%Deriving the Jacobian for n1 = 1:N
Page 24 of 26
for k1 = 1:length(t) if n1 == k1 J(n1,k1) = 1/dx^2 + 1 - 2*u(n1,k1); else if abs(k1 - n1) == 1 J(n1,k1) = 1/dx^2; else J(n1,k1) = 0; end end end end
Ji = inv(J);
%i = 1 for n = 1:length(t)-1 fn = (1/dx^2)*(-2*u(1,n) + 2*u(2,n))+u(1,n)*(1-u(1,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(2,n+1)*fn; u1 = u2; end u(1,n+1)=u(1,n)+(h)*(u1); end
%i = 2,3,...,N-1 for n = 1:length(t)-1 fn = (1/dx^2).*(u(1:N-2,n)-2.*u(2:N-1,n)+u(3:N,n))+u(2:N-1,n).*(1-u(2:N-1,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(3:N,n+1).*fn; u1 = u2; end u(2:N-1,n+1)=u(2:N-1,n)+(h)*(u1); end
%i = N for n = 1:length(t)-1 fn = (1/dx^2)*(2*u(N-1,n)-2*u(N,n))+u(N,n)*(1-u(N,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(N,n+1)*fn; u1 = u2; end u(N,n+1)=u(N,n)+(h)*(u1); end
%Plot solutions using IC g1 figure1 = figure; plot(x,u(:,1),'black','linewidth',2) hold on %This part produces profile for each second starting at 1 to 20 for i = 2:length(t) if rem(i,10) == 0 plot(x,u(:,i),'r') end end legend('t = 0','t > 0','location','northeastoutside') title('Solution using IC = g_1, plotted every 1 second','fontsize',16) xlabel('x','fontsize',16) ylabel('u(x,t_{fixed})','fontsize',16) grid on axis tight hold off
Page 25 of 26
%------------Solving using backward Euler and IC 2------------------------% u(:,1) = g2(:,1);
%Deriving the Jacobian for n1 = 1:N for k1 = 1:length(t) if n1 == k1 J(n1,k1) = 1/dx^2 + 1 - 2*u(n1,k1); else if abs(k1 - n1) == 1 J(n1,k1) = 1/dx^2; else J(n1,k1) = 0; end end end end
Ji = inv(J);
%i = 1 for n = 1:length(t)-1 fn = (1/dx^2)*(-2*u(1,n) + 2*u(2,n))+u(1,n)*(1-u(1,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(1,n+1)*fn; u1 = u2; end u(1,n+1)=u(1,n)+(h)*(u1); end
%i = 2,3,...,N-1 for n = 1:length(t)-1 fn = (1/dx^2).*(u(1:N-2,n)-2.*u(2:N-1,n)+u(3:N,n))+u(2:N-1,n).*(1-u(2:N-1,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(2:N-1,n+1).*fn; u1 = u2; end u(2:N-1,n+1)=u(2:N-1,n)+(h)*(u1); end
%i = N for n = 1:length(t)-1 fn = (1/dx^2)*(2*u(N-1,n)-2*u(N,n))+u(N,n)*(1-u(N,n)); u1 = fn; while(abs(norm(u1)-norm(u2))) >= tol u2 = u1 - Ji(1,n+1)*fn; u1 = u2; end u(N,n+1)=u(N,n)+(h)*(u1); end
%Plot solutions using IC g2 figure1 = figure; plot(x,u(:,1),'black','linewidth',2) hold on %This part produces profile for each second starting at 1 to 20 for i = 2:length(t) if rem(i,10) == 0 plot(x,u(:,i),'r') end end legend('t = 0','t > 0','location','northeastoutside') title('Solution using IC = g_2, plotted every 1 second','fontsize',16) xlabel('x','fontsize',16) ylabel('u(x,t_{fixed})','fontsize',16)