27.1 The Solution Can Be Assumed To Be T E: B A B A
27.1 The Solution Can Be Assumed To Be T E: B A B A
CHAPTER 27
27.1 The solution can be assumed to be T = ex. This, along with the second derivative T” = 2ex,
can be substituted into the differential equation to give
2e x 0.15e x 0
2 0.15 0
0.15
T Ae 0.15 x
Be 0.15 x
The constants can be evaluated by substituting each of the boundary conditions to generate
two equations with two unknowns,
240 A B
150 48.08563A 0.020796B
which can be solved for A = 3.016944 and B = 236.9831. The final solution is, therefore,
T 3.016944e 0.15 x
236.9831e 0.15 x
x T
0 240
1 165.329
2 115.7689
3 83.79237
4 64.54254
5 55.09572
6 54.01709
7 61.1428
8 77.55515
9 105.7469
10 150
2
27.3 A centered finite difference can be substituted for the second derivative to give,
Ti 1 2Ti Ti 1
0.15Ti 0
h2
or for h = 1,
Ti 1 2.15Ti Ti 1 0
2.15T1 T2 240
T9 2.15T10 150
The tridiagonal system can be solved with the Thomas algorithm or Gauss-Seidel for (the
analytical solution is also included)
x T Analytical
0 240 240
1 165.7573 165.3290
2 116.3782 115.7689
3 84.4558 83.7924
4 65.2018 64.5425
5 55.7281 55.0957
6 54.6136 54.0171
7 61.6911 61.1428
8 78.0223 77.5552
9 106.0569 105.7469
10 150 150
The following plot of the results (with the analytical shown as filled circles) indicates close
agreement.
3
27.5 Centered finite differences can be substituted for the second and first derivatives to give,
y i 1 2 y i y i 1 y i 1 y i 1
7 2 y i xi 0
x 2
2x
This equation can be written for each node and solved with methods such as the Tridiagonal
solver, the Gauss-Seidel method or LU Decomposition. The following solution was computed
using Excel’s Minverse and Mmult functions:
x y
0 5
2 4.199592
4 4.518531
6 5.507445
8 6.893447
10 8.503007
12 10.20262
14 11.82402
16 13.00176
18 12.7231
20 8
4
d 2T
1 10 7 (Tb 273) 4 4 10 7 (Tb 273) 3 (T Tb ) 4(150 T ) 0
dx 2
d 2T
34.27479T 1939.659 0
dx 2
We used the Gauss-Seidel method to solve these equations. The results for a few selected
points are:
A graph of the entire solution along with the nonlinear result from Prob. 27.7 is shown below:
250
200
150 Linear
100
50 Nonlinear
0
0 0.1 0.2 0.3 0.4 0.5
27.9 For 5 interior points (h = 3/6 = 0.5), the result is Eq. (27.19) with 2 0.25p2 on the diagonal.
Dividing by 0.25 gives,
8 p 2 4
4 8 p2 4
4 8 p2 4 0
4 8 p 2
4
4 8 p2
The determinant can be expanded (e.g., with Fadeev-Leverrier or the MATLAB poly
function) to give
The roots of this polynomial can be determined as (e.g., with Bairstow’s methods or the
MATLAB roots function) p2 = 1.072, 4, 8, 12, 14.94. The square root of these roots yields p =
1.035, 2, 2.828, 3.464, and 3.864.
First iteration:
>> x=a*x
x =
20
17
22
>> e=max(x)
e =
22
>> x=x/e
x =
0.9091
0.7727
1.0000
Second iteration:
>> x=a*x
x =
18.0000
15.3636
19.9545
>> e=max(x)
e =
19.9545
>> x=x/e
x =
0.9021
0.7699
1.0000
Third iteration:
>> x=a*x
x =
17.9636
15.2961
19.8702
>> e=max(x)
e =
6
19.8702
>> x=x/e
x =
0.9040
0.7698
1.0000
Fourth iteration:
>> x=a*x
x =
17.9665
15.3116
19.8895
>> e=max(x)
e =
19.8895
>> x=x/e
x =
0.9033
0.7698
1.0000
Thus, after four iterations, the result is converging on a highest eigenvalue of 19.8842 with a
corresponding eigenvector of [0.9035 0.7698 1].
Option Explicit
Sub Shoot()
Dim n As Integer, m As Integer, i As Integer, j As Integer
Dim x0 As Double, xf As Double
Dim x As Double, y(2) As Double, h As Double, dx As Double, xend As Double
Dim xp(200) As Double, yp(2, 200) As Double, xout As Double
Dim z01 As Double, z02 As Double, T01 As Double, T02 As Double
Dim T0 As Double, Tf As Double
Dim Tf1 As Double, Tf2 As Double
'set parameters
n = 2
x0 = 0
T0 = 40
xf = 10
Tf = 200
dx = 2
xend = xf
xout = 2
'first shot
x = x0
y(1) = T0
y(2) = 10
Call RKsystems(x, y, n, dx, xf, xout, xp, yp, m)
z01 = yp(2, 0)
Tf1 = yp(1, m)
'second shot
x = x0
y(1) = T0
y(2) = 20
Call RKsystems(x, y, n, dx, xf, xout, xp, yp, m)
z02 = yp(2, 0)
Tf2 = yp(1, m)
'last shot
7
x = x0
y(1) = T0
'linear interpolation
y(2) = z01 + (z02 - z01) / (Tf2 - Tf1) * (Tf - Tf1)
Call RKsystems(x, y, n, dx, xf, xout, xp, yp, m)
'output results
Range("A4:C1004").ClearContents
Range("A4").Select
For j = 0 To m
ActiveCell.Value = xp(j)
For i = 1 To n
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = yp(i, j)
Next i
ActiveCell.Offset(1, -n).Select
Next j
Range("A4").Select
End Sub
Sub RK4(x, y, n, h)
Dim i
Dim ynew, dydx(10), ym(10), ye(10)
Dim k1(10), k2(10), k3(10), k4(10)
Dim slope(10)
Call Derivs(x, y, k1)
For i = 1 To n
ym(i) = y(i) + k1(i) * h / 2
Next i
Call Derivs(x + h / 2, ym, k2)
For i = 1 To n
ym(i) = y(i) + k2(i) * h / 2
Next i
Call Derivs(x + h / 2, ym, k3)
For i = 1 To n
ye(i) = y(i) + k3(i) * h
Next i
Call Derivs(x + h, ye, k4)
For i = 1 To n
8
27.15 A general formulation that describes Example 27.3 as well as Probs. 27.3 and 27.5 is
d2y dy
a 2
b cy f ( x) 0
dx dx
y i 1 2 y i y i 1 y y i 1
a b i 1 cyi f ( xi ) 0
x 2
2x
Collecting terms
a 0.5bx y i 1 2a cx 2 y i a 0.5bx y i 1 f ( x i )x 2
Dividing by x2,
a / x 2 0.5b / x y i 1 2a / x 2 c y i a / x 2 0.5b / x y i 1 f ( xi )
For Example 27.3, a = 1, b = 0, c = h and f(x) = hTa. The following VBA code implements
Example 27.3.
Public hp As Double
Option Explicit
Sub FDBoundaryValue()
Dim ns As Integer, i As Integer
Dim a As Double, b As Double, c As Double
9
Sub Tridiag(e, f, g, r, n, x)
Dim k As Integer
For k = 2 To n
e(k) = e(k) / f(k - 1)
f(k) = f(k) - e(k) * g(k - 1)
Next k
For k = 2 To n
10
Function ff(x)
ff = hp * 20
End Function
27.17 The following two codes can be used to solve this problem. The first is written in
VBA/Excel. The second is an M-file implemented in MATLAB.
VBA/Excel:
Option Explicit
Sub Power()
Dim n As Integer, i As Integer, iter As Integer
Dim aa As Double, bb As Double
Dim a(10, 10) As Double, c(10) As Double
Dim lam As Double, lamold As Double, v(10) As Double
Dim es As Double, ea As Double
es = 0.001
n = 3
aa = 2 / 0.5625
bb = -1 / 0.5625
a(1, 1) = aa
a(1, 2) = bb
For i = 2 To n - 1
a(i, i - 1) = bb
a(i, i) = aa
a(i, i + 1) = bb
Next i
a(i, i - 1) = bb
a(i, i) = aa
lam = 1
For i = 1 To n
v(i) = lam
Next i
Sheets("sheet1").Select
Range("a3:b1000").ClearContents
Range("a3").Select
Do
iter = iter + 1
Call Mmult(a, (v), v, n, n, 1)
11
lam = Abs(v(1))
For i = 2 To n
If Abs(v(i)) > lam Then lam = Abs(v(i))
Next i
ActiveCell.Value = "iteration: "
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = iter
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "eigenvalue: "
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = lam
ActiveCell.Offset(1, -1).Select
For i = 1 To n
v(i) = v(i) / lam
Next i
ActiveCell.Value = "eigenvector:"
ActiveCell.Offset(0, 1).Select
For i = 1 To n
ActiveCell.Value = v(i)
ActiveCell.Offset(1, 0).Select
Next i
ActiveCell.Offset(1, -1).Select
ea = Abs((lam - lamold) / lam) * 100
lamold = lam
If ea <= es Then Exit Do
Loop
End Sub
Sub Mmult(a, b, c, m, n, l)
Dim i As Integer, j As Integer, k As Integer
Dim sum As Double
For i = 1 To n
sum = 0
For k = 1 To m
sum = sum + a(i, k) * b(k)
Next k
c(i) = sum
Next i
End Sub
12
MATLAB:
es = 0.0001;
maxit = 100;
n = size(A);
for i=1:n
v(i)=1;
end
v = v';
e = 1;
iter = 0;
while (1)
13
eold = e;
x = A*v;
[e,i] = max(abs(x));
e = sign(x(i))*e;
v = x/e;
iter = iter + 1;
ea = abs((e - eold)/e) * 100;
if ea <= es | iter >= maxit, break, end
end
27.19 This problem can be solved by recognizing that the solution corresponds to driving the
differential equation to zero. To do this, a finite difference approximation can be substituted
for the second derivative to give
Ti 1 2Ti Ti 1
R 2
1 10 7 (Ti 273) 4 4(150 Ti )
(x)
where R = the residual, which is equal to zero when the equation is satisfied. Next, a
spreadsheet can be set up as below. Guesses for T can be entered in cells B11:B14. Then, the
residual equation can be written in cells C11:C14 and referenced to the temperatures in
column B. The square of the R’s can then be entered in column D and summed (D17).
= sum(D11:D14)
=(B10-2*B11+B12)/$B$7^2-$B$2*(B11+273)^4+$B$3*($B$4-B11)
Solver can then be invoked to drive cell D17 to zero by varying B11:B14.
14
27.21 (a) First, the 2nd-order ODE can be reexpressed as the following system of 1st-order
ODE’s
dx
z
dt
dz
8 z 1200 x
dt
function dx=spring(t,y)
dx=[y(2);-8*y(2)-1200*y(1)]
[t,y]=ode45('spring',[0 .4],[0.5;0]);
plot(t,y(:,1));
(b) The eigenvalues and eigenvectors can be determined with the following commands:
1. x-spacing
at x = 0, i = l; and at x = 2, i = n
20
x
n 1
d 2u du
2
6 u 2
dx dx
u i 1 2u i u i 1 u i 1 u i 1
6 ui 2
x 2
2x
Coefficients:
ai = 1 – 3x bi = –2 – x2 ci = 1 + 3x di = 2x2
i = 2:
[1 3(x)]10 [2 x 2 ]u 2 [1 3(x)]u 3 2x 2
Coefficients:
a2 = 0 b2 = –2 – x2 c2 = 1 + 3x d2 = 2x2 – 10(1 – 3(x))
i = n – 1:
[1 3(x)]u n 2 [2 x 2 ]u n 1 [1 3(x )]1 2x 2
Coefficients:
a2 = 1 – 3x b2 = –2 – x2 c2 = 0 d2 = 2x2 – (1 – 3(x))
n=41; xspan=2.0;
% Constants
dx=xspan/(n-1);
dx2=dx*dx;
% Sizing matrices
u=zeros(1,n); x=zeros(1,n);
a=zeros(1,n); b=zeros(1,n); c=zeros(1,n); d=zeros(1,n);
ba=zeros(1,n); ga=zeros(1,n);
ba(2)=b(2);
ga(2)=d(2)/b(2);
for i=3:n-1
ba(i)=b(i)-a(i)*c(i-1)/ba(i-1);
ga(i)=(d(i)-a(i)*ga(i-1))/ba(i);
end
% back substitution
u(n-1)=ga(n-1);
for i=n-2:-1:2
u(i)=ga(i)-c(i)*u(i+1)/ba(i);
end
% Plot
plot(x,u)
title('u[xx]+6u[x]-u=2; u(x=0)=10, u(x=2)=1')
xlabel('x-Independent Variable Range 0 to 2');ylabel('u-Dependent
Variable')
grid
27.25 By summing forces on each mass and equating that to the mass times acceleration, the
resu1ting differential equations can be written
k k2 k
x1 1 x1 2 x 2 0
m1 m1
k k k3 k
x2 2 x1 2 x 2 3 x3 0
m2 m2 m2
k k k4
x3 3 x 2 3 x3 0
m3 m3
In matrix form
18
k1 k 2 k2
0
m1 m1
x1 k 3 x1 0
k2 k2 k3
x2 x 2 0
m m m
x3 2 2 2
x3 0
0 k k k 4
3 3
m3 m3
k 33.33333 23.33333 0
m 23.33333 46.66667 23.33333
0 23.33333 33.33333
>> k1=15;k4=15;k2=35;k3=35;
>> m1=1.5;m2=1.5;m3=1.5;
>> a=[(k1+k2)/m1 -k2/m1 0;
-k2/m2 (k2+k3)/m2 -k3/m2;
0 -k3/m3 (k3+k4)/m3]
a =
33.3333 -23.3333 0
-23.3333 46.6667 -23.3333
0 -23.3333 33.3333
>> w2=eig(a)
w2 =
6.3350
33.3333
73.6650
>> w=sqrt(w2)
w =
2.5169
5.7735
8.5828
y Ae 5t t 2 0.4t 0.08
y t 2 0.4t 0.08
19
Note that even though the choice of the initial condition removes the positive exponential
terms, it still lurks in the background. Very tiny round off errors in the numerical solutions
bring it to the fore. Hence all of the following solutions eventually diverge from the analytical
solution.
(b) 4th order RK. The plot shows the numerical solution (bold line) along with the exact
solution (fine line).
15
10
-5
0 1 2 3 4
-10
(c)
function yp=dy(t,y)
yp=5*(y-t^2);
>> tspan=[0,5];
>> y0=0.08;
>> [t,y]=ode45('dy1',tspan,y0);
(d)
>> [t,y]=ode23S('dy1',tspan,y0);
(e)
>> [t,y]=ode23TB('dy1',tspan,y0);
27.29 First, the 2nd-order ODE can be reexpressed as the following system of 1st-order ODE’s
20
dT
z
dx
dz
(0.12 x 3 2.4 x 2 12 x)
dx
(a) Shooting method: These can be solved for two guesses for the initial condition of z. For
our cases we used –1 and 0.5. We solved the ODEs with the 4th-order RK method using a
step size of 0.125. The results are
z(0) 1 0.5
T(10) 570 565
These values can then be used to derive the correct initial condition,
0 .5 1
z ( 0) 1 ( 200 ( 570)) 76
565 (570)
(b) Finite difference: Centered finite differences can be substituted for the second and first
derivatives to give,
Ti 1 2Ti Ti 1
2
0.12 xi3 2.4 xi2 12 xi 0
x
This equation can be written for each node with the result
21
2 1 0 0 T1 101.44
1 2 1 0 T2 69.12
0 1 2 1 T3 46.08
0 0 1 2 T 215.36
4
These equations can be solved with methods such as the tridiagonal solver, the Gauss-Seidel
method or LU Decomposition. The following solution was computed using Excel’s Minverse
and Mmult functions: