Sample 4 - Fisher's Equation PDE

Post on 20-Dec-2015

52 views 7 download

transcript

3/26/2013

Fisher’s Equation: Analysis and numerical solutions Author:

Mandeep Singh

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 6 of 26

Results using IC = 𝑔1(π‘₯):

2D plot

3D plot

Page 7 of 26

Results using IC = 𝑔2(π‘₯):

2D plot

3D plot

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 9 of 26

Results using IC = 𝑔1(π‘₯):

2D plot

3D plot

Page 10 of 26

Results using IC = 𝑔2(π‘₯):

2D plot

3D plot

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 12 of 26

Results using IC = 𝑔1(π‘₯):

2D Plot

3D Plot

Page 13 of 26

Results using IC = 𝑔2(π‘₯):

2D Plot

3D Plot

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 15 of 26

3D Plot

Results using IC = 𝑔2(π‘₯):

2D Plot

Page 16 of 26

3D 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)

Page 26 of 26

grid on axis tight hold off

References

Salsa, Sandro. Partial Differential Equations in Action: From Modelling to Theory. 1st ed. Milan: Springer, 2008.

Debnath, Lokenath. Nonlinear Partial Differential Equations for Scientists and Engineers. 3rd ed. New York,

2012.