Docsity

Numerical Solution to Diffusional Equation, Lecture Notes - Advanced Calculus, Study notes of Calculus

Toggle sidebar
bg1
Computer Lab 9
Numerical Solutions to the Diffusion Equation
Adrian Down
April 29, 2006
Section 1: setting up the iterative method
Part a
In this lab, we consider the partial differential equation known as the diffusion
equation. Let γrepresent the concentration of a substance along a one-
dimensional interval in the coordinate x. If the diffusion of the substance is
governed by Fick’s Law, then γsatisfies the diffusion equation,
γt(t, x) = Dγxx(t, x) (1)
Note. •Subscripts denote partial differentiation, so that γt=∂γ
∂t .
•Dis a constant that depends on the substance in which the diffusion
is taking place, and has units of length2·time−1.
Our goal is to obtain an approximation to γ(x, t). Call this approxima-
tion c(x, t). I obtain this approximation by finding a recursive formula that
determines c(x, t) at some point given the value of the approximation at
some earlier points. To find this recursive formula, I approximate the deriva-
tives in (1) with finite differences. These finite difference approximations are
obtained using Taylor expansions.
I first approximate the time derivative, holding the spatial component
fixed,
γ(t+ ∆t, x) = γ(t, x) + γt(t, x)∆t+O((∆t)2)
⇒γ(t+ ∆t, x)−γ(t, x)
∆t=γt(t, x) + O(∆t)
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Numerical Solution to Diffusional Equation, Lecture Notes - Advanced Calculus and more Study notes Calculus in PDF only on Docsity!

Computer Lab 9

Numerical Solutions to the Diffusion Equation

Adrian Down

April 29, 2006

Section 1: setting up the iterative method

Part a

In this lab, we consider the partial differential equation known as the diffusion equation. Let γ represent the concentration of a substance along a one- dimensional interval in the coordinate x. If the diffusion of the substance is governed by Fick’s Law, then γ satisfies the diffusion equation,

γt(t, x) = Dγxx(t, x) (1)

Note. • Subscripts denote partial differentiation, so that γt = ∂γ ∂t.

  • D is a constant that depends on the substance in which the diffusion is taking place, and has units of length^2 ·time−^1. Our goal is to obtain an approximation to γ(x, t). Call this approxima- tion c(x, t). I obtain this approximation by finding a recursive formula that determines c(x, t) at some point given the value of the approximation at some earlier points. To find this recursive formula, I approximate the deriva- tives in (1) with finite differences. These finite difference approximations are obtained using Taylor expansions. I first approximate the time derivative, holding the spatial component fixed,

γ(t + ∆t, x) = γ(t, x) + γt(t, x)∆t + O((∆t)^2 )

⇒

γ(t + ∆t, x) − γ(t, x) ∆t

= γt(t, x) + O(∆t)

As a first order approximation, this defines the finite difference,

γtt(t, x) ≈

c(t + ∆t, x) − c(t, x) ∆t

I now approximate the spatial derivative, holding the time component fixed,

γ(t, x + ∆x) + γ(t, x − ∆x) = 2γ(t, x) + 2 ·

γxx(t, x) 2

(∆x)^2 + O((∆x)^4 )

γ(t, x + ∆x) − 2 γ(t, x) + γ(t, x − ∆x) (∆x)^2

= γxx(t, x) + O((∆x)^2 )

As a second order approximation, this defines the finite difference,

γxx(t, x) ≈

c(t, x + ∆x) − 2 c(t, x) + c(t, x − ∆x) (∆x)^2

Substituting (2) and (3) into the diffusion equation (1) gives a recursive relationship that defines the approximation c(x, t),

c(t + ∆t, x) − c(t, x) ∆t

= D ·

c(t, x + ∆x) − 2 c(t, x) + c(t, x − ∆x) (∆x)^2

⇒ c(t + ∆t, x) = c(t, x) + D

∆t (∆x)^2

c(t, x + ∆x) − 2 c(t, x) + c(t, x − ∆x)

Part b

I now consider a particular mesh of points with fixed spacing to compute the approximation c(x, t). In particular, I consider c(m · ∆x, n · ∆t), where m and n are integers. Let m be chosen such that there are a total of M interior mesh points on the spatial interval [a, b] where the approximation is to be obtained. This determines the spacing of the mesh points,

∆x =

(b − a) M + 1

Note. The points c(0∆x, t) and c(M∆x, t) correspond to the endpoints of the spatial interval. The values of the exact solution at these points are given in the problem statement. I set the values of the approximation equal to the exact values at these endpoints, and hence my interpolation scheme deals only with the interior spatial mesh points.

Hence,

c(t + ∆t) = c(t) + D

∆t (∆x)^2

1 2 −^1

1 2

= c(t) ( 1 1 + A)

The resulting coefficient matrix A is conveniently tri-diagonal.

Sections 2 and 3: Solving the PDE

Discussion

Exact solution

Before calculating any approximations, I solve the differential equation (1) analytically, so that I can determine the accuracy of my numerical approxi- mations. In lecture, we found that exact solutions to the diffusion equation are given by,

γ(t, x) =

πDt

−∞

γ(0, x′)e−^

(x−x′)^2 4 Dt (^) dx′^ (4)

where x′^ is an arbitrary dummy variable of integration. I am given the explicit form of γ(0, x′) as an initial condition, γ(0, x′) = sin(x′). To perform the integral in (4), I write the sinusoid as part of an imaginary exponential,

1 2

πDt

−∞

sin(x′)e−^

(x−x′)^2 4 Dt (^) dx′^ = 1 2

πDt

−∞

eıx

′ e−^

(x−x′)^2 4 Dt (^) dx′

To convert the integral into a familiar Gaussian, I complete the square in the

exponent,

−

4 Dt

(x − x′)^2 + ıx′^ = −

4 Dt

x^2 +

2 Dt

xx′^ −

4 Dt

x′^2 + ıx′

4 Dt

x′^2 + x′

2 Dt

x + ı

4 Dt

x^2

4 Dt

x′^2 − (2x + 4ıDt)

4 Dt

x^2

= −

4 Dt

[x′^2 − (2x + 4ıDt)

  • (x + 2ıDt)^2 − (x + 2ıDt)^2 ] −

4 Dt

x^2

= −

4 Dt

[

(x′^ − (x + 2ıDt)) 2 (x + 2ıDt)^2

]

4 Dt

x^2

= −

4 Dt

[x′^ − (x + 2ıDt)] 2

4 Dt

x^2 +

4 Dt

(x + 2ıDt)^2

[

x′ 2

Dt

x 2

Dt

  • ı

Dt

)] 2

 

4 Dt

x^2 +

4 Dt

x (^2) + 4ıDtx − 4 D (^2) t 2

[

x′ 2

Dt

x 2

Dt

  • ı

Dt

)] 2

  • (ıx − Dt)

Returning to the desired integral,

γ(t, x) =

πDt

−∞

e−

h (^) x′ 2 √Dt −

“ (^) x 2 √Dt +ı

√ Dt

”i 2 +(ıx−Dt)dx′

The second factor in the exponential is independent of the variable of in- tegration x′, and so I remove it from the integral. The second term in the square brackets is also independent of x′. Pretending (or perhaps hoping) that the imaginary term acts in the same way as a real constant, this term acts as an effective shift in the location of the peak of the Gaussian. Since the integration is taken over all spatial coordinates, this shift should not effect the value of the integral. I remove the shift by making a change of variables,

˜x =

[

x′ 2

Dt

x 2

Dt

  • ı

Dt

)]

d˜x =

dx′ 2

Dt

0

1

2

3

0

1

0

1

Distance x

Exact solution

Time t

Figure 1: Exact solution to the diffusion equation on the interval x ∈ [0, π], t ∈ [0, 1]with D = 1 and initial condition γ(0, x) = c(0, x) = sin x.

I find that the Forward Euler method becomes unstable as the temporal step size is made sufficiently small. Taking N = 13 temporal mesh values, I find that the approximate solution begins to diverge. Taking smaller step sizes causes further divergence, and also causes the approximate solution to diverge at smaller values of t.

Approximate solution: Implicit Euler method

The definition of the Implicit Euler method is similar to that for the Forward Euler method,

xn+1 = xn + hf (xn+1)

Notice that in the case of the Implicit Euler method, however, the function f is evaluated at the point xn+1. Since we know only the value of xn, this equation must be solved for xn+1. Performing the same calculations from Section 1 part a would show that the recursion relation in the case of the Implicit Euler method takes the form,

( 11 − A) c(t + ∆t) = c(t)

In general, we have the value of the approximation c(t) at some time t and we would like to find the value of the approximation at the next time step, c(t + ∆t). Since A is tri-diagonal, the Implicit Euler method reduces to the problem of repeatedly solving a tri-diagonal system of the form Vz = b, where the matrix V and the column vector b are known, and the column vector z is to be determined. To perform the iterations in the Implicit Eu- ler Method, I use the techniques for solving tri-diagonal systems developed earlier in the course. I find that the Implicit Euler method is much more stable than the For- ward Euler method. In particular, The Implicit Euler method appears to be stable for an arbitrarily small time step, in contrast to the Forward Euler method which becomes divergent with small time steps. However, it is inter- esting that the approximate solution obtained by the Implicit Euler method does not become exact, even as the time step size is made arbitrarily small.

Code

% Created by Adrian Down, University of California, Berkeley. % Created for Prof. John Neu’s numerical analysis class, Math 128A, Spring 2006 % ----------------------------------------------------------------------------- %

c = DiffusionSolver((part-1),c0,1,T,N,M,a,b); % create numerical approximation err = c - gamma; % error in approximation

% plot results close all; surf(xmesh,tmesh,gamma); % plot exact solution colormap(cool); title(’Exact solution’); xlabel(’Distance x’); ylabel(’Time t’); axis([0 pi 0 1 0 1]) view([142.5,30]) % rotate to have t = 1 in forefront if (nargin > 3) % If save flag is provided, save plot fn = [’P’ int2str(part) ’-Ext-M-’ int2str(M) ’-N-’ int2str(N)]; % make file na fn = [fp fn]; % append file path print(ff,fn) % save file close(gcf) % close plot window end

figure; surf(xmesh,tmesh,c); % plot approximate solution colormap(spring); title([’Approximate solution, M = ’ int2str(M) ’, N = ’ int2str(N)]); xlabel(’Distance x’); ylabel(’Time t’); axis([0 pi 0 1 0 1]) view([142.5,30]) % rotate to have t = 1 in forefront if (nargin > 3) % If save flag is provided, save plot fn = [’P’ int2str(part) ’-Apx-M-’ int2str(M) ’-N-’ int2str(N)]; % make file na fn = [fp fn]; % append file path print(ff,fn) % save file close(gcf) % close plot window end

figure; surf(xmesh,tmesh,err); % plot error colormap(autumn) title([’Error, M = ’ int2str(M) ’, N = ’ int2str(N)]);

xlabel(’Distance x’); ylabel(’Time t’); axis([0 pi 0 1 min(min(err)) max(max(err))]) view([142.5,30]) % rotate to have t = 1 in forefront if (nargin > 3) % If save flag is provided, save plot fn = [’P’ int2str(part) ’-Err-M-’ int2str(M) ’-N-’ int2str(N)]; % make file na fn = [fp fn]; % append file path print(ff,fn) % save file close(gcf) % close plot window end

% ----------------------------------------------------------------------------- % Created by Adrian Down, University of California, Berkeley. % Created for Prof. John Neu’s numerical analysis class, Math 128A, Spring 2006 % ----------------------------------------------------------------------------- % function c = DiffusionSolver(type,f0,D,T,N,M,a,b) % Computes approximate solution to the diffusion equation, % df/dt = D*d^2f/dx^ % using a matrix version of the Euler method, specified by ’type’ % ’type’ = 1 uses Forward Euler; ’type’ = 2 uses Implicit Euler % % The solution is computed in the interval a < x < b, with M x-mesh points % The time interval is 0 < t < T, with N t-mesh points % f0 is a symbolic expression for the intial condition, c(x,0) % The returned matrix c: % (n+1)th row contains the approximate solution at the nth time step % 1st row contains initial condition, c(x,0) = f

x = findsym(f0); % find the sympbolic variable in f0 to substitute x-mesh for if size(x) > 1 %if f is multi-variate, there’s a problem... error(’f(0) contains more than one symbolic variable’); x = x(1); % take the symbolic variable alphabetically closest to ’x’ as the x end

ht = T / N; % compute t-mesh spacting hx = (b - a) / (M + 1); % compute x-mesh spacing xmesh = linspace(hx,M*hx,M); % construct x-mesh

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

0

1

Distance x

Approximate solution, M = 31, N = 6

Time t

(a) Approximate solution to the diffusion equation com- puted with the Forward Euler method, with a spatial mesh of M + 1 = 32 points temporal mesh of N = 6 points. The stability parameter D · (^) (∆∆xt) 2 is equal to 17.2921.

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

−0.

−0.

−0.

−0.

−0.

−0.

0

Distance x

Error, M = 31, N = 6

Time t (b) Error in approximate solution to the diffusion equa- tion computed with the Forward Euler method, calculated as c(x, t) − γ(x, t).

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

0

1

Distance x

Approximate solution, M = 31, N = 9

Time t

(c) Approximate solution to the diffusion equation com- puted with the Forward Euler method, with a spatial mesh of M + 1 = 32 points temporal mesh of N = 9 points. The stability parameter is equal to 11.5281. Divergent behavior begins to appear at large t.

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

−0.

−0.

−0.

−0.

0

Distance x

Error, M = 31, N = 9

Time t (d) Error in approximate solution to the diffusion equa- tion computed with the Forward Euler method, calculated as c(x, t) − γ(x, t). The divergent behavior is evident at large t. Note that the divergence from the exact solution is not symmetric about the center of the spatial interval.

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

0

1

Distance x

Approximate solution, M = 31, N = 20

Time t

(e) Approximate solution to the diffusion equation com- puted with the Forward Euler method, with a spatial mesh of M + 1 = 32 points temporal mesh of N = 20 points. The sta- bility parameter is equal to 6.9169. With this small temporal mesh, the approximate solution becomes severely divergent at large t.

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

−

−

−

0

2

4

6

x 10^9

Distance x

Error, M = 31, N = 20

Time t (f) Error in approximate solution to the diffusion equa- tion computed with the Forward Euler method, calculated as c(x, t) − γ(x, t). The divergent behavior that developed with N = 13 mesh points is magnified as N increases. Also, it retains the same asymmetrical shape.

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

0

1

Distance x

Approximate solution, M = 31, N = 6

Time t

(g) Approximate solution to the diffusion equation com- puted with the Implicit Euler method, with a spatial mesh of M + 1 = 32 points temporal mesh of N = 6 points. The stability parameter D · (^) (∆∆xt) 2 is equal to 17.2921.

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

0

Distance x

Error, M = 31, N = 6

Time t (h) Error in approximate solution to the diffusion equa- tion computed with the Implicit Euler method, calculated as c(x, t) − γ(x, t).

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

0

1

Distance x

Approximate solution, M = 31, N = 20

Time t

(i) Approximate solution to the diffusion equation computed with the Implicit Euler method, with a spatial mesh of M + 1 = 32 points temporal mesh of N = 20 points. The stability parameter is 6.9169. There is no observed divergent behavior in the case of the Implicit Euler method, in contrast to the Forward Euler method, which produced divergent behavior with N = 20 temporal mesh points.

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

0 1 2 3 4 5 6 7 8 9

x 10−

Distance x

Error, M = 31, N = 20

Time t (j) Error in approximate solution to the diffusion equa- tion computed with the Implicit Euler method, calculated as c(x, t) − γ(x, t). There is no evidence of divergent behavior.

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

0

1

Distance x

Approximate solution, M = 31, N = 100

Time t

(k) Approximate solution to the diffusion equation com- puted with the Implicit Euler method, with a spatial mesh of M + 1 = 32 points temporal mesh of N = 100 points. The stability parameter is equal to 1.0375. The Implicit Euler method proves to be very stable, even as the temporal mesh size is made very small.

0.5^0 1.5^1 2.5^2 3

(^0) 0. 0.4 0. 0.8 1

0

1

2

x 10−

Distance x

Error, M = 31, N = 100

Time t (l) Error in approximate solution to the diffusion equa- tion computed with the Implicit Euler method, calculated as c(x, t) − γ(x, t). With a small temporal mesh, no divergent behavior is observed.