Professional Documents
Culture Documents
Dedication:
I Thank You
For being an inspiration to many including mine.
For believing in my talents and capacities.
For helping me rise above humble beginnings.
For without you, I can only dream the impossible.
Page 1
PREFACE
This book presents the compilation of Numerical Methods in a simplified engineering approach.
Gone are those days when students have to deliberately find scarce materials in curve fittings,
interpolation, and finding roots for complicated polynomials and trigonometric functions.
This book presents topics in numerical methods in a way comprehensible by engineering
students. Each topic is presented with brief discussions, theoretical background, program flows,
the codes or programs and the sample simulation results.
The concepts presented in this book are the same as that in the major reference material in
Numerical Methods. The only difference is the integration of a software design to interpret the
data more comprehensively. The codes or programs are designed to operate in MATLAB
software to aid in the presentation of the data as well as its accuracy in curve fittings and
interpolation.
-AUTHOR
ACKNOWLEDGEMENT
This book has come to fruition with the efforts and contribution of the following people: Dr.
Nicanor Buenconsejo for imparting his knowledge to the students in Numerical Methods. This
book is an output of his perseverance and dedication in teaching this subject to the students.
To my classmates whose bond extends outside class. Special thanks to my mother whose
support is overwhelming, for the support and the lots of coffee breaks at night. To those who
have helped this book in small and big ways, I owe the success in this book.
All glory and praise to the Almighty Father for giving me good health and strength in writing this
book. I may have sleepless nights yet the will to finish this book continues,
PERSONAL MESSAGE
Dr. Nick,
I thank you for generously giving your time and intellect in this subject. I may have had difficulty
in comprehending the subjects/topics in the first three meetings yet I found better ways of
studying the topics in Numerical Methods well. My new found ways of learning are used in other
subjects such as the Advance Engineering Mathematics and Signals and Systems. More
importantly, I found the concepts of Newton- Raphson Method useful in the implementation of
my thesis.
Page 2
-Author
Table of Contents
Chapter 1. Part 1: Nonlinear Equations (Single Equation) ..................................................................... 8
...................................................................................................................................................................... 8
FIXED POINT ITERATION ................................................................................................................................. 9
UNDERLYING PRINCIPLES AND THEOREMS ............................................................................................ 9
CONVERGENCE....................................................................................................................................... 10
PROGRAM FLOW ..................................................................................................................................... 11
MATLAB CODES SOURCE FILE................................................................................................................ 12
Bisection Method ........................................................................................................................................ 15
EXAMPLE PROBLEM: ................................................................................................................................ 16
PROGRAM FLOW ..................................................................................................................................... 18
MATLAB CODES SOURCE FILE................................................................................................................ 19
SAMPLE RUN/SIMULATION ...................................................................................................................... 20
Regula Falsi Method ................................................................................................................................... 23
CONVERGENCE OF FALSE POSITION METHOD .................................................................................. 24
MATLAB CODES SOURCE FILE................................................................................................................ 24
SAMPLE RUN/SIMULATION ...................................................................................................................... 25
Newton -Raphson Method ........................................................................................................................ 28
PROGRAM FLOW.................................................................................................................................... 29
MATLAB SOURCE CODE ......................................................................................................................... 30
SAMPLE RUN/SIMULATION ...................................................................................................................... 32
Secant Method ............................................................................................................................................ 34
PROGRAM FLOW ..................................................................................................................................... 35
MATLAB SOURCE CODE ......................................................................................................................... 36
SAMPLE RUN/SIMULATION ...................................................................................................................... 38
Chapter 2, Part 2: Nonlinear Equations (System of Nonlinear Equations) .......................................... 39
NEWTON-RAPHSON METHOD IN 2D .......................................................................................................... 40
PROGRAM FLOW ..................................................................................................................................... 41
MATLAB SOURCE CODE ......................................................................................................................... 42
Page 3
Page 4
Page 5
Page 6
Page 7
Bisection Method
Secant Method
Page 8
pn n0
lim pn1
n
P,
Assume that
a xb
g Ca, b .
a yb
for all
then g has a fixed point in [a , b]. Furthermore, suppose that g(x) is defined over
then g
Theorem 1.3.
Assume that g(x) and g(x) are continuous on a balanced interval (a,b)=(P) that
contains the unique fixed point P and that the starting point value p 0 is chosen in this interval.
If
Page 9
CONVERGENCE
There are many ways to change the equation f(x) to the fixed point form x = g(x) using
simple algebraic manipulation. But the crucial question is does it converge to the fixed
point? How can we find a fixed point form that reliably and rapidly converges to a
solution.
The answers of the above questions are based on Theorem 1.3.
For example the equation x3 + 4x2 10 = 0 has a unique root in the interval [1 , 2].
Thus, the function can manipulate in a form x = g(x) as follows:
1.
x g ( x)
2.
x g ( x)
10
4x
x
1
10 x 3 -------------------------------- converge
2
Evaluating for convergence of the given fixed point form in the interval [1 , 2].
1.
10
4x
x
x g ( x)
g ' ( x)
1
10
2 4
10
x
2
4x
x
For x = 1, then
x g ( x)
1
10 x 3
2
g ' ( x)
3x 2
4 10 x 3
For x = 1, then
Page 10
PROGRAM FLOW
Page 11
p0 =
tol =
m_iter =
P(1)=p0;
J(1)=0;
for j=1:m_iter
P (j+1)=feval(g,p0);
err=abs(P(1+j)-p0);
J (1+j)=j;
p0=P(j+1);
if (err<tol),break; end
end
if J(1+j)==100
disp('*****DIVERGING*****')
else
disp('*****CONVERGING****')
disp(''),disp('The fixed point is g(p)=p='),disp(p0)
disp('The absolute error is '),disp(err)
end
R=[J;P];
disp(''),disp(''),disp('List of Ite rations')
disp(''),disp('
p(k)')
disp(''), disp(R')
a=round(p0)-2;
b=round(p0)+2;
Page 12
h=(b-a)/150;
c=1;
for x=a:h:b;
Y(c)=feval(g,x);
X(c)=x;
end
plot(X,Y, '-b','linewidth',2.5), hold on
plot(X,0, '-g','linewidth',2.5),
plot(p0,p0,'.r','markersize',30),
title('The line y = x and the curve y = g(x).');
hold off
grid on
Page 13
Page 14
Bisection Method
This lesson introduce another types of numerical method for solving a single nonlinear equation
to determine the roots of the equation f(x) = 0 Bisection Method.
Bisection method falls under the category of bracketing methods, since the method is based
on finding the root between two points.
Since the root is bracketed between two points, xl (lower x value) and xu (upper x value), one
can find the midpoint, xm between xl and xu . This gives new interval
1. xl and xm , or
2. xm and xu .
If f(xl) and f(xm) have opposite signs, a root lies in [xl, xm].
Also called as Bolzanos method, the idea of bisection method is very simple. We assume that
we are given two values x = a and x = b in which a < b. The function f(x) must be continuous in
this range of values and is positive at one and negative at the other. In other words, [a, b] must
be a change-of-sign interval.
Intuitively, if we divide a change-of-sign interval in half, one or the other half must end up being
another change-of-sign interval. We keep dividing in half until the change-of-sign interval is so
small that any point in it is within the specified tolerance.
More precisely, consider the point x = (a + b)/2. If f(x) = 0, we are done. Otherwise, depending
on the sign of f(x), we know that the root lies in [a, x] or [x, b]. In any case, our change-of-sign is
now half as large as before. Repeat this process with the new interval until the interval is
sufficiently small. We are then guaranteed to converge, which makes bisection a robust.
Page 15
If f(xm) and f(xu) have opposite signs, a root lies in [xm, xu].
If f(xm) = 0, we found at x = xm.
EXAMPLE PROBLEM:
The bisection Method can be derived using the given illustration of the polynomial. At first, the
polynomial has to be graphed f(x)= x^3-0.165x^2+0.00003993. It shows that theres one root in
the interval [0 , 0.11].
Step 2: Solving for first xm
xm = (xl + xu) / 2. = (0 + 0.11) / 2 = 0.055
Step 3: Finding the new interval: xl and xu
Solving for f(xl), f(xu), and f(xm)
f(xl) = (xl)3 0.165(xl)2 + 0.0003993
f(xu) = (xu)3 0.165(xu)2 + 0.0003993
f(xm) = (xm)3 0.165(xm)2 + 0.0003993
Check if the function changes sign
- If f(xl) f(xm) < 0; then xl = xl , xu = xm.
- If f(xm) f(xu) < 0; then, xl = xm ; xu = xu.
- If f(xm) = 0; then, root = xm, then End.
Step 4: Solving for new midpoint and computed Error(absolute error).
xm = ( xl + xu ) / 2.
|Ec| =computed error = ( |(xmnew - xmold) / xmnew| ) * 100
Step 5: Check if |Ec| <= Es(set error) and number of iterations is equal to number of
iterations allowed:
If true; End
Else; Goto step 3
Page 16
a0 , b0
a1 , b1 a0 , c0 ........ or c0 , b0
a2 , b2 a2 , c2 ........or c2 , b2
an , bn an , cn ........ or cn , bn
a0 b0
2
a b1
c1 1
2
a b2
c2 2
2
c0
cn
an bn
2
The program codes also utilized the MATLAB software. It prompts the user to input the desired
equation for mathematical operation. It also shows the the program flow for easier
understanding of the topic.
Page 17
PROGRAM FLOW
Page 18
Page 19
disp('')
disp(''),disp('Iterations for the bisection method')
disp(''), disp(' a
b'), disp(R')
SAMPLE RUN/SIMULATION
Page 20
0.5
1.5
2.5
3.5
f ( x) sin( x ^ 2) * exp( x) with the left end point limit of 1 and right end point limit of 4. The
simulated tolerance is just up to 0.0001.
The simulation has approximated the root to be equal to 1.77247619, with an error estimate of
4.5776367187e-05.
This shows that the simulation has insofar been effective in finding for the roots of the equation.
Page 21
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
1.5
2.5
3.5
4.5
Page 22
f ( xu ) f ( xl ) 0 f ( xu )
xu xl
xm xu
This method is similar to the secant method, in the sense that it uses the formula
=
()()
()
to approximate the root of the equation. However, the regula falsi method must also be similar
with the bisection method because the initial interval [a, b], and all intervals thereafter, must all
be change-of-sign intervals. Knowing these rules for the method, convergence is guaranteed.
The method approximates the curve of f(x) on [a, b] by a straight line connecting the
Solving for xm,
x m xu
f xu xu xl
f xu f xl
xmn xun
Page 23
two points (a, f (a)) and (b, f (b)). It guesses that the solution may be the point at which the
straight line crosses the x axis.
');
');
Page 24
disp('')
disp('')
disp(''), disp('Iterations for the Regula-Falsi/False Position Method')
disp(''), disp('
b'), disp(P)
SAMPLE RUN/SIMULATION
Page 25
1.5
0.5
-0.5
-1
-1.5
-2
1.5
2.5
3.5
4.5
Page 26
-12
x 10
0
-1
-2
-3
-4
-5
-6
-7
-8
8.5
9.5
10
10.5
11
11.5
12
The two simulations of the Regula Falsi Method show different curves with a very minimal error.
More importantly, the approximate roots of both curves are also shown .
Page 27
p
p
p
x
Solving for the slope of the tangent line,
0 f p0
f ' ( p0 )
p1 p0
Thus,
p1 p0
f p0
f ' p0
pk g pk 1 pk 1
f pk 1
f ' pk 1
Page 28
PROGRAM FLOW
Page 29
Page 30
P=[a,P];
disp(''),disp('Iterations for the Newton-Raphson Method')
disp(''), disp('
Iters #
P'), disp(P)
Page 31
SAMPLE RUN/SIMULATION
50
-50
-100
-150
-200
-250
-300
-350
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
Page 32
0.2
0.15
0.1
0.05
-0.05
-0.1
0.5
1.5
2.5
3.5
Page 33
Secant Method
Instead of dividing the interval in half, as done in the bisection method, the secant method
regards the function as approximately linear, passing through the two points x = a and x = b and
then finds the root of this linear function. The interval [a, b] needs not be a change-of sign
interval, and the next value of x needs no longer lie inside the said interval. In consequence,
residual convergence must be used in the algorithm instead of true convergence. The secant
method is oftentimes much faster than bisection, but since it does not maintain an interval in
which the solution must lie, this method can surprisingly fail to converge. Unlike bisection, the
secant method can be generalized to two or more dimensions, which is then usually called as
Broydens method.
Secant method does not involve differentiation of the function. This method somewhat like to
False Position method.
p0
y
p1
f p1 f p0 0 f p1
p1 p0
p2 p1
Thus,
p2 g p1 , p0 p1
f p1 p1 p0
f p1 f p0
pn 1 g pn , pn 1 pn
f pn pn pn 1
f pn f pn 1
Page 34
PROGRAM FLOW
Page 35
Page 36
end
p=p1;
H=[a, H];
disp(''),disp('Iterations for the Secant Method')
dis(''), disp('
Iter #
P'), disp(H)
Page 37
SAMPLE RUN/SIMULATION
Page 38
Page 39
NEWTON-RAPHSON METHOD IN 2D
This lesson introduces the third types of method for solving the point of intersection in two
dimension Newton Raphson Method.
Consider the system with u and v set equal to zero:
f1 x, y 0
f 2 x, y 0
consider small changes in the functions near the point (p0,q0):
p x p0
q y q0
The differential p and q can be estimated using Jacobian of the system, evaluated at (p 0,q0)
f1 p 0 , q 0
x
x f 2 p 0 , q 0
f1 p 0 , q 0
y
p f1 p 0 , q 0
f p , q
2 0 0
f 2 p 0 , q 0 q
y
J ( p0 , q0 )P F( p0 , q0 )
P1 P0 P
Suppose that Pk has been obtained.
Step 1. Evaluate the function
F P
k
x
J P
k
x
Step 3. Solve the system
f p ,q
1
k
k
pk , qk
for
J P P F P
k
k
f p ,q
1
k
k
f p , q
2
k
k
P
P P
k 1
k
Page 40
PROGRAM FLOW
Page 41
Page 42
q0=P1(2);
Y1=[feval(f1,p0,q0); feval(f2,p0,q0)];
err=norm(dP);
P0=P1;
Y0=Y1;
P=[P;P1];
if err<tol, break,end
end
Y0=Y0';
disp(''), disp('Iterations for the Newton-Raphson Mehtod')
disp(''), disp('
Q'), disp(P)
SAMPLE RUN/SIMULATION
Page 43
Jacobi Method
This is another popular numerical method to solve nonlinear equations of several unknowns,
especially for large number of unknowns. Jacobi Method is known to be effective for equations
of about 10 unknowns, and is still useful for about 20 unknowns if calculation time is not a great
factor.
This lesson introduces the method in solving the point of intersection of two or three nonlinear
equations. First method in solving the point of intersection is the Jacobi method. This method uses
matrix and differential concept to transform the nonlinear functions into x = g1(x,y) and y =
g2(x,y) for two nonlinear equations.
Jacobian Matrix. Assume that f1(x,y) and f2(x,y) are functions of the independent variables x
and y, then their Jacobian matrix J(x,y) is
f1
x
f 2
x
f1
y
f 2
y
For f1(x,y,z), f2(x,y,z) and f3(x,y,z) functions of the independent variables x, y and z, the Jacobian
matrix J(x,y,z) is
f1
x
f 2
x
f
3
x
f1
y
f 2
y
f 3
y
f1
z
f 2
z
f 3
For a function of several variables, the differential is used to show how changes of the
independent variables affect the change in the dependent variables. Suppose that we have
u = f1(x,y,z)
v = f2(x,y,z)
and
w = f3(x,y,z)
Suppose that the values of the three functions above are known at the point (x0,y0,z0). Their
differential changes and vector notation are:
f1
x0 , y0 , z0 dx f1 x0 , y0 , z0 dy f1 x0 , y0 , z0 dz
x
y
z
f
f
f
dv 2 x0 , y0 , z0 dx 2 x0 , y0 , z0 dy 2 x0 , y0 , z0 dz
x
y
z
du
dw
f 3
x0 , y0 , z0 dx f 3 x0 , y0 , z0 dy f 3 x0 , y0 , z0 dz
x
y
z
du
dx
dF dv J x0 , y0 , z0 dy J x0 , y0 , z0 dX
dw
dz
Page 44
ALGORITHM FLOW
1. Start by showing the user the general matrix equation AX = B which will be solved by Jacobi
method.
2. Prompt the user to enter the coefficient, constants and initial approximations matrices.
3. Check the dimensions of the matrices. If the dimensions do not match, display error message
and end the program.
4. Check pivot elements of the coefficient matrix. If any happens to be zero, display error
message and end the program.
5. Calculate the elements for the revised coefficient and constants matrices A and B as
shown in the formula.
6. Calculate new approximation for the variables using the formula. Result is stored as matrix X.
The number of iterations is stored as k.
7. Calculate absolute error and relative error. If any of the two is less than the 0.0001 tolerance,
end the program.
8. Repeat steps 6 and 7 until the condition stated in step 7 is satisfied, or if k = 100.
9. End the program.
Page 45
PROGRAM FLOW
Page 46
z'),
disp(H)
disp('The solution vector Ps is:'),disp(Ps)
disp('The estimated error is equal to'),disp(err)
Page 47
SAMPLE RUN/SIMULATION
Page 48
Gauss-Siedel Method
This lesson discusses another types of numerical method for solving the point of intersection of
two or three nonlinear equations - Gauss Siedel Method. Gauss Siedel method is an
improvement of Jacobi method. This method has a lesser number of iterations compared to
Jacobi method.
The Gauss Siedel Iteration for two nonlinear functions and three nonlinear functions,
respectively:
pk 1 g1 pk , qk
qk 1 g 2 pk 1 , qk
and
pk 1 g1 pk , qk , rk
qk 1 g 2 pk 1 , qk , rk
rk 1 g 3 pk 1 , qk 1 , rk
The Gauss Siedel method transforms all the equations into a matrix, with the constants set on the
right sides of the equations. The solution is commonly started with guessing that the values of all
variables are equal to zero, though other guesses are also accepted. Next, the first variable is
approximated using equation 1 (row 1), and substituting the initial guesses for the other variables
to solve for a new approximation. The second variable is approximated using equation 2 (row 2)
in a similar manner. However, the value that must be substituted for the first variable is not the
initial guess but the new approximation. This step is repeated for the nth variable using the nth
equation (nth row) and substituting all solved approximations whenever possible. The
approximation for all variables must be repeated a number of times until tolerance criteria are
achieved.
ALGORITHM FLOW
1. Prompt the user to enter the augmented matrix for the equations, the last column
representing the constants. The equations must take the form variables + constant = 0.
2. Check if the number of unknowns corresponds to the number of equations. If it doesnt,
display error message and end the program.
3. Initialize the variables matrix (answers) and a dummy variable matrix all equal to zero.
4. Perform the Gauss-Siedel method. Consider the cases for first variable, last variable and the
other variables as this uses different formulas due to matrix representation limitations.
5. The values for each approximation will be stored in the variables matrix first and after
approximating all the variables for one time, store in the dummy matrix for error calculations.
6. Calculate absolute and relative errors.
Page 49
7. Repeat Procedures 4 to 6 until the error is equal to or less than the tolerable amount, or if the
maximum number of iterations was already performed.
8. Display calculated values and the amount of error. For cases where calculated error is less
than tolerable amount, display calculated values only.
9. End the program.
PROGRAM FLOW
Page 50
z'),
disp(H)
disp('The solution vector Ps is:'),disp(Ps)
disp('The estimated error is equal to'),disp(err)
Page 51
SAMPLE RUN/SIMULATION
Page 52
Page 53
There are cases when the pivot element is 0 in some rows. This must be prevented by using row
interchange to any row (preferably to that with the maximum absolute value) below the pivot of
concern. These instances may lead to a divided error since the pivot is used as the divisor in
finding for the multiplier m. TO avoid such from happening, the process must undergo a row
interchange with any row below the pivot if there are non-zero elements under the pivots
column. This process is called pivoting and may only succeed if the matrix is nonsingular.
Pivoting can also be taken advantage if there are more than one noon zero elements under the
pivots column. Since larger magnitudes of a divisor usually reduces precision errors, then the row
below the pivot with the largest value of element under the pivots column shall be chosen.
To construct the solution to AX=B, by first reducing the augmented matrix [A,B] to uppertriangular form and then performing back substitutions.
Page 54
PROGRAM FLOW
Page 55
Page 56
SAMPLE RUN/SIMULATION
Page 57
12
22
0
1
= 21
31
0
1
32
and
13
23
33
0
0.
1
must first be solved. A permutation matrix P then must satisfy the condition
=
and a dummy matrix Y shall be created from the equation
= .
Finally, the matrix X shall be solved by back substitution method using the values of U and Y as
shown:
= .
ALGORITHM FLOW
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
Prompt the user to enter the coefficients matrix A and the constants column matrix B.
Calculate the size of the matrix A. Store.
Initialize the lower and upper triangular matrices L and U as A.
Compare the elements of first column of U. Set the row which contains the highest
element as Row 1.
Manipulate matrix U such that all the elements below the 1 st pivot element will be zero. To
do this, find the ratio of the row of concerns first element to the pivot element of first row,
multiply the ratio to the first row, and subtract the scaled first row to the row of concern.
Repeat steps 4 and 5 to all other rows of matrix U starting from the second row. However,
exclude in the comparison and manipulation rows above the pivot element.
Create the lower triangular matrix. Compare the elements of the last column of L. Set the
row which contains the highest element as the last row.
Divide the last row by its pivot element to set pivot to unity.
Manipulate matrix L such that all the elements above the last pivot element will be zero.
To do this, scale the last row by the last element of the row of concern and subtract the
result from the row of concern.
Repeat steps 7, 8 and 9 to all other rows of matrix L starting from the second to the last
row. However, exclude in the comparison and manipulation rows below the pivot
element.
Calculate the permutation matrix P and the dummy matrix Y.
Calculate matrix X by back substitution.
Display results and end the program.
Page 58
PROGRAM FLOW
Page 59
Page 60
end
X=zeros(n,1);
X(n)=Y(n)/A(row(n),n);
for k=n-1:-1:1,
X(k)=(Y(k)-A(row(k),k+1:n)*X(k+1:n))/A(row(k),k);
end
[n n]=size(A);
I=eye(n);
for k=1:n,
P(k,:)=I(row(k),:);
end
L=I;
for c=1:n-1,
L(c+1:n,c)=A(row(c+1:n),c);
end
U=zeros(n,n);
for c=1:n;
U(1:c,c)=A(row(1:c),c);
end
A=C;
R=B-A*X;
disp(''),disp('Implementation of th eLU factorization method.')
disp(''), disp('The matrix is A='),disp(A)
disp('The vector B is displayed as B`='), disp(B')
disp('The solution to AX=B is displayed as X`='),disp(X')
disp('The permutation matrix is P='),disp(P)
disp('THe lower-triangular matrix is L='),disp(L)
disp('The transformed vector PB is displayed as (PB)`'),disp((P*B)')
disp('The solution to LY=PB is displayed as Y`='),disp(Y')
disp(''),disp('The upper-triangular matrix is U='),disp(U)
disp('The vector Y is displayed as Y`='),disp(Y')
disp('The solution to UX=Y is displaced as X`='),disp(X')
disp('The residual R=B-A*X is displayed as R`='),disp(R')
Page 61
SAMPLE RUN/SIMULATION
Page 62
Where:
where
and
The computation of xi (k+1) requires each element in x(k) except itself. Unlike the GaussSeidel
method, we can't overwrite xi(k) with xi(k+1), as that value will be needed by the rest of the
computation. The minimum amount of storage is two vectors of size n.
Page 63
MATLAB CODES/SIMULATION
REAL matrix
tol
%
% output x
%
error
iter
flag
iter = 0;
% initialization
flag = 0;
bnrm2 = norm( b );
if ( bnrm2 == 0.0 ), bnrm2 = 1.0; end
r = b - A*x;
error = norm( r ) / bnrm2;
if ( error < tol ) return, end
[m,n]=size(A);
[ M, N ] = split( A , b, 1.0, 1 );
for iter = 1:max_it,
% matrix splitting
% begin iteration
x_1 = x;
x = M \ (N*x + b);
% update approximation
Page 64
% compute error
% check convergence
end
if ( error > tol ) flag = 1; end
% no convergence
% END jacobi.m
Page 65
There are two important characteristics of the Gauss-Seidel method should be noted. Firstly, the
computations appear to be serial. Since each component of the new iterate depends upon all
previously computed components, the updates cannot be done simultaneously as in the Jacobi
method. Secondly, the new iterate
depends upon the order in which the equations are
examined. If this ordering is changed, the components of the new iterates (and not just their
order) will also change.
In terms of matrices, the definition of the Gauss-Seidel method can be expressed as
MATLAB CODES
clear;clc
format compact
% Read or Input any square Matrix % any matrix can be inputted in this Matlab Codes
A = [-6 2 1 2 1;
3 8 -4 1 0;
-1 1 4 10 1;
3 -4 1 9 2;
2 0 1 3 10];% coefficients matrix
C = [3;4;-2 ;12;1];% constants vector
n = length(C);
X = zeros(n,1);
Page 66
Error_eval = ones(n,1);
% Check if the matrix A is diagonally dominant
for i = 1:n
j = 1:n;
j(i) = [];
B = abs(A(i,j));
Check(i) = abs(A(i,i)) - sum(B); % Is the diagonal value greater than the remaining row values
combined?
if Check(i) < 0
fprintf('The matrix is not strictly diagonally dominant at row %2i\n\n',i)
end
end
% Start the Iterative method
iteration = 0;
while max(Error_eval) > 0.001
iteration = iteration + 1;
Z = X; % save current values to calculate error later
for i = 1:n
j = 1:n; % define an array of the coefficients' elements
j(i) = []; % eliminate the unknow's coefficient from the remaining coefficients
Xtemp = X; % copy the unknows to a new variable
Xtemp(i) = []; % eliminate the unknown under question from the set of values
X(i) = (C(i) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xsolution(:,iteration) = X;
Error_eval = sqrt((X - Z).^2);
end
% Display Results
GaussSeidelTable = [1:iteration;Xsolution]';
MaTrIx = [A X C]
Page 67
Page 68
Lagrange Approximation
Page 69
Lagrange Method
The point-slope form shows the equation of a line provided two points to which the said line pass
is known. This formula can also be written as
= 1 () = 0
1
0 1
+ 1
0
1 0
The generalization of the formula is the construction of a polynomial PN(x) of degree N (at most)
that passes through N + 1 points. This has the form
() =
= 0 , ()
where the LN, k is the Lagrange coefficient given by the formula
, =
( 0 ) ...( 1 )( +1 ) ...( )
Note that the terms (x xk) and (xk xk) do not appear in the formula.
The method introduces other types of numerical method for solving the polynomial Lagrange
Approximation. What is Lagrange approximation?
The slope between (x0 , y0) and (x1 , y1) is m = (y1 y0) / (x1 x0), and the point slope formula
for the line y = y0 + m(x x0) can be rearranged as
y P x y0 y1 y0
x x0
x1 x0
Lagrange used a slightly different method to find this polynomial. He noticed that it could be
written as
y P1 x y0
x x0
x x1
y1
x0 x1
x1 x0
The quotients are called the Lagrange coefficient polynomials based on the nodes x 0 and x1
L1, 0 x
x x1
x0 x1
and
L1,1 x
x x0
x1 x0
Thus,
Page 70
PROGRAM FLOW
Page 71
Page 72
xlabel('x')
ylabel('y')
title('Comparison of f(x) (GReen) and P(Red)')
hold off;
X=a:0.1:b;
x=X;
Y=eval(f);
P=polyval(C,X);,format long;
pts=[X;Y;P;Y-P];
Mx1=['Lagrange Polynomial Approximation'];
Mx2=['
x(k)
f(x(k))
Pn(x(k))
error'];
disp(''),disp(Mx1),disp(''),disp(Mx2),disp(pts')
SAMPLE RUN/SIMULATION
Page 73
Page 74
Page 75
Page 76
Therefore, one goal of this method is to determine an equation of the line y = f(x) that best fit
on the set of data points (x1 , y1), , (xN , yN).
Consider the formula which is a linear function of the form
y f x Ax B
Due to some errors(experimental / measurement), the true values of the function f(x) satisfies
f xk yk ek
ek f xk yk
Maximum error:
Average error:
Root-mean-square
(RMS) error:
1 k N
E f
1max
f xk y k
k N
1
E1 f
N
1
E2 f
N
k 1
k 1
f xk y k
f xk yk
1/ 2
Page 77
PROGRAM FLOW
Page 78
MATLAB SOURCE
clc; clear all; format short;
disp('Least-Square Line')
X=input('Enter the abscissas for the points in X:');
Y= input('Enter the ordinates for the points in Y:');
xmean=mean(X);
ymean=mean(Y);
sumx2=(X-xmean)*(X-xmean)';
sumxy=(Y-ymean)*(X-xmean)';
A=sumxy/sumx2;
B=ymean-A*xmean;
a=min(X)-0.20;
b=max(X)+0.20;
c=min(Y)-0.35;
d=max(Y)+0.35;
points=[X;Y]; format short;
disp(''),disp('THe given x-y data points.')
disp('
y'),disp(points')
a=-1;
b=7;
Xs=[a b];
Ys=A*Xs+B;
c=-2;
d=12;
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis([a b c d]);
axis(axis);
hold on;
plot(X,Y,'or', Xs,Ys,'-g');
xlabel('x');
ylabel('y');
Mx1='Least squares line:f(x)=';
Mx2=[Mx1,num2str(A),'x'];
Page 79
if B>0,
Mx3=[Mx2,'+',num2str(B)];
else
Mx3=[Mx2,'-',num2str(abs(B))];
end;
title(Mx3);
grid;
hold off;
points=[X;Y;A*X+B;Y-(A*X+B)]';
Mx4='The given x-y data points:';
disp(''),disp(Mx3),disp(Mx4),...
disp('
Mx5='
y'), disp([X;Y]')
x(k)
y(k)
f(x(k))
error';
disp(''),disp(Mx3)
disp(''),disp(Mx5),disp(points)
SAMPLE RUN/SIMULATION
Page 80
Page 81
If m is 1, then the equation is a line; if m is 2, then it is a parabola, and so on. Similar to the least
square lines, the sum of the root mean square of the error must be minimized,
When partial differentiation is applied with respect to A,B, and C, a linear system will be formed
as follows:
The values of A,B and C can be solved by using any of the solutions to linear systems.
Page 82
PROGRAM FLOW
Page 83
end
A=F'*F;
B=F'*Y';
C=A\B;
C=flipud(C);
a=-4;b=5;
h=(b-a)/150;
Xs=a:h:b;
Ys=polyval(C,Xs);
figure(1)
a=-4;b=5;
c=0;d=6;
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis([a b c d]);
axis(axis);
hold on;
plot(X,Y,'or',Xs,Ys,'g');
xlabel('x');
xlabel('x');
ylabel('y');
Mx1='Least squares polynomial:y=P';
Mx2=[Mx1,num2str(m),'(x).'];
title(Mx2);
grid;
hold off;
points1=[X;Y];
Mx1='y =P(x)=c(1)x^m+c(2)x^m-1+...+c(m)x+c(m+1)';
Mx2=['A polynomial of degree m=',num2str(m),'has been fit.'];
Mx3='THe coefficients are stored in the array C=';
disp(''),disp(Mx1),disp(Mx2),disp(Mx3),...
disp(''),disp(C'),disp('The given x-y points:')
disp('
y'), disp(points1)
points2=[X;Y;polyval(C,X);Y-polyval(C,X)]';
Page 84
y(k)
P(x(k))
error';
disp(''),disp(Mx4),disp(points2)
SAMPLE RUN/SIMULATION
Page 85
There are two ways that are used to complete the tasts. First is the data linearization method
and second is the nonlinear least squares method. Data linearization involves the transformation
of the original point (xk,yk) in the xy plane (Xk,Yk) in the XY plane thereby represented by a linear
equation:
Since the data points have been linearized, the least square lines method may be applied to
transform the data points. Finally, after obtaining the values of the linear coefficients, they can
be transformed back to their nonlinear equivalent. Nonlinear least squares method on the other
hand, solves the nonlinear curve and uses the Newtons method in obtaining its coefficients. But
since the solution in getting the coefficients needs a lot of time, requires good starting values for
the iteration process, and when compared with the former, differs only for a small amount of
value. Hence, it is more preferable to use the data linearization method.
Page 86
PROGRAM FLOW
Page 87
y'),disp(points')
Mx1='
Mx2='(1) y=A/x+B
Mx3='(4) y=x/(A+Bx)
Mx4='(7) y=C x^A
(2) y=D/(x+C)
(3) y=1/(Ax+B)';
Page 88
grid;
hold off;
points =[X;Y;crvfun(Clist,ct,X);Y-Crvfun(Clist,ct,X)]';
Mx1=crvnam(Clist,ct);
Mx2='The given x-y points:';
disp(''),disp(Mx1),disp(''),disp(Mx2)
disp('
Mx3='
y'),disp([X;Y]')
x(k)
y(k)
f(x(k))
error';
disp('')
disp(['
',Mx1]),disp(Mx3),disp(points)
Y='),disp(y),...
disp('
Y(x)=L/(1+Cexp(Ax))'),...
disp('
lim y(x)=L'),...
disp(' x--->oo'),...
L=input('Enter the constant L=');
end
if length (L)==0,L=0;end
if L<=max(y),
L=max(y)+0.01*abs(max(y));
end
if ct==1, X=x.^(-1);Y=y;end
if ct==2, X=x.*y; Y=y; end
if ct==3, X=x; Y=y.^(-1); end
if ct==4, X=x.^(-1); Y=y^(-1); end
if ct==5, X=log(x); Y=y; end
if ct==6, X=x; Y= log(y); end
if ct==7, X=log(x); Y=log(y); end
Page 89
Page 90
C=Clist(3);
D=Clist(4);
L=Clist(5);
if ct==1&B>0,
z=['f(x) =', num2str(A),'/x+',num2str(B)];end
if ct==1&B<0,
z=['f(x) =', num2str(A),'/x-',num2str(abs(B))];end
if ct==2&C>=0,
z=['f(x) =', num2str(D),'/x+',num2str(C),')'];end
if ct==2&C<=0,
z=['f(x) =', num2str(D),'/x-',num2str(abs(C)),')'];end
if ct==3&B>=0,
z=['f(x)=1/(',num2str(A) ,' x+',num2str(B),')'];end
end
SAMPLE RUN/SIMULATION
Page 91
Page 92
Page 93
where
To find the second derivative, first look at the first derivatives at the half intervals (x0+h/2) and (x0h/2):
and
Continuing in this fashion, the first and second derivatives correct to fourth order in h are:
Page 94
MATLAB CODES:
%CENTRAL DIFFERENCE METHOD FOR LAPLACE EQUATION
% Numerical scheme used is an implicit second order central difference
...in space(5-point difference)
%Specifying parameters
nx=100;
ny=100;
dx=2/(nx-1);
dy=2/(ny-1);
x=0:dx:2;
y=0:dy:2;
UW=0;
UE=y;
US=0;
UN=0;
UnW=0;
UnE=0;
UnS=0;
UnN=0;
u=zeros(nx,ny);
%B.C vector
bc=zeros(nx-2,ny-2);
bc(1,:)=UW/dx^2; bc(nx-2,:)=UE(2:ny-1)/dx^2; %Dirichlet B.Cs
%bc(:,1)=US/dy^2; bc(:,ny-2)=UN/dy^2; %Dirichlet B.Cs
%bc(1,:)=-UnW/dx; bc(nx-2,:)=UnE/dx; %Neumann B.Cs
bc(:,1)=-UnS/dy; bc(:,ny-2)=UnN/dy; %Neumann B.Cs
%B.Cs at the corners:
bc(1,1)=UW/dx^2-UnS/dy; bc(nx-2,1)=UE(2)/dx^2-UnS/dy;
bc(1,ny-2)=UW/dx^2+UnN/dy; bc(nx-2,ny-2)=UE(ny-1)/dx^2+UnN/dy;
%Calculating the coefficient matrix for the implicit scheme
Ex=sparse(2:nx-2,1:nx-3,1,nx-2,nx-2);
Ax=Ex+Ex'-2*speye(nx-2);
%Dirichlet B.Cs
%Ax(1,1)=-1; Ax(nx-2,nx-2)=-1; %Neumann B.Cs
Ey=sparse(2:ny-2,1:ny-3,1,ny-2,ny-2);
Ay=Ey+Ey'-2*speye(ny-2);
%Dirichlet B.Cs
Ay(1,1)=-1; Ay(ny-2,ny-2)=-1; %Neumann B.Cs
A=kron(Ay/dy^2,speye(nx-2))+kron(speye(ny-2),Ax/dx^2);
S=zeros(nx-2,ny-2);
S=reshape(S-bc,[],1);
%Source term
Page 95
S=A\S;
S=reshape(S,nx-2,ny-2);
u(2:nx-1,2:ny-1)=S;
%Boundary conditions
%Dirichlet:
u(1,:)=UW;
u(nx,:)=UE;
%u(:,1)=US;
%u(:,ny)=UN;
%Neumann:
%u(1,:)=u(2,:)-UnW*dx;
%u(nx,:)=u(nx-1,:)+UnE*dx;
u(:,1)=u(:,2)-UnS*dy;
u(:,ny)=u(:,ny-1)+UnN*dy;
%Plotting the solution
surf(x,y,u','EdgeColor','none');
shading interp
title('2-D Laplace''s equation')
xlabel('Spatial co-ordinate (x) \rightarrow')
ylabel('{\leftarrow} Spatial co-ordinate (y)')
zlabel('Solution profile (P) \rightarrow')
Page 96
MATLAB CODES
clc;clear all; format short;
disp('Forward-Difference Solution for the Heat Equation')
disp('
disp('
disp('
2')
u(x,t) = c u(x,t)')
t
xx')
Page 97
Page 98
PROGRAM FLOW
Page 99
Page 100
Page 101
(, ) = (() + ()) + 1
=1 ( ).
2
Composite Trapezoidal Rule is an intuitive method of finding the area under a curve y = f(x) is
by approximating that area with a series of trapezoids that lie above the intervals
.
Theorem 14.1 (Composite Trapezoidal Rule) Suppose that the interval [a,b] is subdivided into M
subintervals [xk, xk+1] of which h=(a-b)/M by using the equally spaced nodes xk=a+kh for k
=0,1,., M. The composite trapezoidal rule for M subintervals can be expressed in any of the
equivalent ways:
T f , h
T f , h
h M
f xk 1 f xk
k
2 1
h
f 0 2 f1 2 f 2 2 f 3 .... 2 f M 2 2 f m1 f M
2
T f , h
h
2
A h
M 1
f xk
k 1
b
a f x dx T f , h
Page 102
of width
is subdivided
, then there exists a value c with a < c < b so that the error
has the form
.
This is expressed using the "big
" notation
.
the error term
should be
ALGORITHM FLOW
1.
2.
3.
4.
5.
Page 103
PROGRAM FLOW
Page 104
Page 105
end
h=(b-a)/200; X1=a:h:b;
Y1=f(X1);Z=zeros(size(X));
plot(X,Y,'or',X,Z,'r',X1,Y1,'-g',Xp,Yp,'-r');
h0=(b-a)/m;X=a:h0:b; Y=f(X);
for k=1:m+1,
X0=X(k); Y0=f(X(k));
plot([X0 X0],[0 Y0],'-r');
end
end;
grid; hold off;
disp(''),disp(Mx2),disp(''),disp(s)
Page 106
SAMPLE RUN/SIMULATION
Page 107
(, ) = (() + ()) +
1
=1 (2 ) +
4
3
=1 (21 ).
This method of finding the area under a curve y = f(x) is by approximating that curve with a
series of parabolic segments that lie above the intervals
. When several
parabolas are used, we call it the Composite Simpson Rule.
Simpsons Rule is credited to the mathematician Thomas Simpson (1710 - 1761) of Leicestershire,
England.
Theorem 15.1 (Composite Simpson Rule) Suppose that [a,b] is subdivided into 2M subintervals
[xk, xk+1] of which h=(b-a)/2M by using xk=x0+kh for k =0,1,., 2M. The composite Simpson rule for
2M subintervals can be expressed in any of the equivalent ways:
S f , h
S f , h
h M
f x2 k 2 4 f x2 k 1 f x 2 k
3 k 1
h
f 0 4 f1 2 f 2 4 f 3 .... 2 f 2 M 2 4 f 2 M 1 f 2 M
3
Page 108
S f , h
h
f a f b 2h
3
3
M 1
k 1
f x2 k
4h
3
M 1
f x
k 1
2 k 1
x dx
S f , h
of width
is subdivided into
with
.
This is expressed using the "big
" notation
should
Page 109
PROGRAM FLOW
Page 110
ALGORITHM FLOW
1.
2.
3.
4.
5.
Page 111
X=a:h:b; Y=f(X);
Xp(1)=a;Yp(1)=f(a);
for k=1:m,
Va=X(n*(k-1)+1);Vb=X(n*(k)+1);
VM=ceil(200/m);Vh=(Vb-Va)/VM;
Vab=X(n*(k-1)+1:n*(k)+1);Vyy=f(Vab);
Px=polyfit(Vab,Vyy,n); Vx=Va+Vh:Vh:vb;
Vy=polyval(Px,Vx);Xp=[Xp,Vx];
Yp=[Yp,Vy];
end
h=(b-a)/200; X1=a:h:b;
Y1=f(X1);Z=zeros(size(X));
plot(X,Y,'or',X,Z,'r',X1,Y1,'-g',Xp,Yp,'-r');
h0=(b-a)/m;X=a:h0:b; Y=f(X);
for k=1:m+1,
X0=X(k); Y0=f(X(k));
plot([X0 X0],[0 Y0],'-r');
end
end;
grid; hold off;
disp(''),disp(Mx2),disp(''),disp(s)
Page 112
SAMPLE RUN/MATLAB
Page 113
Euler's Method
Heun's Method
Taylor Method of Order 4
Runge-Kutta Method of Order 4
Page 114
EULERS METHOD
Page 115
PROGRAM FLOW
Page 116
MATLAB CODES
clc; clear all; format long;
disp('Eulers Method')
disp('will solve the differential equation y=(t-y)/2')
a=input('Enter left endpoint of [a,b]:');
b=input('Enter right endpoint of[a,b]:');
ya=input('Enter the initial value:');
m=input('Enter the number of steps:');
h=(b-a)/m;
T=zeros(1,m+1);
Y=zeros(1,m+1);
T(1)=a; Y(1)=ya;
for j=1:m,
Y(j+1)=Y(j)+h*f(T(j),Y(j));
T(j+1)=a+h*j;
end
points=[T;Y];
a=0; b=3;
c=0; d=1.75;
axis([a b c d]);
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis(axis);
hold on;
plot(T,Y,'g');
if m<=30,
plot(T,Y,'or');
end;
xlabel('t');
ylabel('y');
title('Euler solution to y=f(t,y)');
grid;
hold off;
Mx1='Euler soluiion to y=f(t,y).';
Mx2='
t(k)
y(k)';
disp(''),disp(Mx1)
disp(''),disp(Mx2),disp(points')
Page 117
Page 118
HEUNS METHOD
This lesson introduces another method for solving the solution of a differential equations (Initial
Value Problem). Heuns method is a modification of Eulers method.
To obtain the solution of an Initial Value Problem using Heuns method.
yk 1 yk
where
h
f tk , yk f tk 1 , pk 1
2
tk 1 tk h
pk 1 yk hf tk , yk
ALGORITHM FLOW
1. At runtime, prompt the user to enter the D. E. in terms of y and t. Note that the user is only
to input the right side of the equation, and the left side is understood to be y.
2. Prompt the user to enter left and right endpoints of t, the M number of steps method is
to be implemented, and the value of y when t = left endpoint.
Page 119
PROGRAM FLOW
Page 120
MATLAB CODES
clc; clear all; format long;
disp('Heun`s Method')
disp('will solve the differential equation y=f(t,y)')
a=input('Enter left endpoint of [a,b]:');
b=input('Enter th eright endoint of [a, b]:');
ya= input('Enter the initial value:');
m=input('Enter the number of steps:');
h=(b-a)/m;
T=zeros(1,m+1);Y=zero(1,m+1);
T(1)=a;Y(1)=ya;
for j=1:m,
k1=f(T(j),Y(j));p=Y(j)+h*k1;
T(j+1)=a+h*j; k2=f(T(j+1),p);
Y(j+1)=Y(j)+h*(K1+k2)/2;
end
points=[T;Y];
a=0;b=3;c=0;d=1.75;
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis([a b c d]);axis(axis);
hold on; plot(T,Y,'g');
if m<=30,
plot(T,Y,'or');
end;
xlabel('t'); ylabel('y');
title('Heun`s solution to y=f(t,y)');
grip;hold off;
Mx1='Heuns solution to y=f(t,y).';
Mx2='
t(k)
y(k)';
disp(''),disp(Mx1)
disp(''),disp(Mx2),disp(points')
Page 121
Page 122
of order N, then
ek yt k yk O h N 1
In particular, the final global error (FGE) at the end of the interval will satisfy
E yb , h yb yM O h N
Page 123
PROGRAM FLOW
Page 124
MATLAB CODES
clc;clear all; format long;
disp('Taylors Method of Order 4')
disp('will solve the IVP y=f(t,y) with y(a)=y0')
a=input('Enter the left endpoint of [a,b]:');
b=input('Enter the right endpoint of [a,b]:');
ya=input('ENter th einitial value of ya:');
m=input('Enter the number of steps:');
h=(b-a)/m;
T=zeros(1,m+1);
Y=zeros(1,m+1);
T(1)=a;Y(1)=ya;
for j=1:m,
tj=T(j);yj=Y(j);
D=feval('df',tj,yj);
Y(j+1)=yj+h*(D(1)+h*(d(2)/2+h*(D(3)/6+h*D(4)/24)));
T(j+1)=a+h*j;
end
points=[T;Y];
a=0;b=3;
c=0;d=1.75;
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis([a b c d]);axis(axis);
hold on; plot(T,Y,'g');
if m<=30,
plot(T,Y,'or');
end;
xlabel('t');ylabel('y');
title('Taylors solution to y=f(t,y)');
grid;
Mx1='Taylors solution to y=f(t,y)';
Mx2='
t(k)
y(k)';
Page 125
disp(''),disp(Mx1)
disp(''),disp(Mx2),disp(points')
Page 126
+1 = + (1 + 22 + 23 + 4 )
6
where
1 = ( , )
2 = ( +
, + 1 )
2
2
3 = ( +
, + 2 )
2
2
4 = ( + , + 3 )
These equations are the very core of RK4 method. For an overview, the equations may be
obtained by substituting Simpsons rule into the integral form of differential equation and
replacing fk1 + 1/2 with the average of successive function values (fk2 + fk3)/2. The original
derivation was done by matching coefficients of the 4 th order Taylor Series. Accordingly, the RK4
method is expected to work better than the Euler and Heun methods.
Page 127
PROGRAM FLOW
Page 128
ALGORITHM FLOW
1. At runtime, prompt the user to enter the D. E. in terms of y and t. Note that the user is only
to input the right side of the equation, and the left side is understood to be y.
2. Prompt the user to enter left and right endpoints of t, the M number of steps method is to
be implemented, and the value of y when t = left endpoint.
3. Calculate step size.
4. Calculate fk1, fk2, fk3, fk4 and y at t = left endpoint. Display results.
5. Repeat step 4 for M number of times but increment t by the step size for each repetition.
6. End the program.
MATLAB CODES
clc; clear all; format long;
disp('Runge-Kutta Mthod of Order 4')
disp('will solve the IVP y=f(t,y) with y(a)=y0')
a= input('Enter the left endpoint of [a,b]:');
b= input('Enter the right end point of [a,b]:');
ya= input('Enter the initial value of steps:');
m= input('Enter the number of steps:');
h=(b-a)/m;
T=zeros(1,m+1);
Y=zeros(1,m+1);
T(1)=a; Y(1)=ya;
for j=1:m,
tj=T(j);yj=Y(j);
k1=h*f(tj,yj);
k2=h*f(tj+h/2,yj+k1/2);
k3=h*f(tj+h,yj+k3);
k4=h*f(tj+h,yj+k3);
Y(j+1)=yj+(k1+2*k2+2*k3+k4)/6;
T(j+1)=a+h*j;
end
points=[T;Y];
c=0; d=1.75;
Page 129
t(k)
y(k)';
disp(''), disp(Mx1)
disp(''),disp(Mx2), disp(points')
Page 130
Page 131
Adam-Bashfort-Moulton Method
Milne-Simpson Method
Systems of Ordinary Differential
Equations/Predator-prey Model
Higher Order Differential
Equations/Pendulum Model
BVP by FINITE Difference
Page 132
Adams-Bashforth-Moulton Method
The fundamental theorem of calculus is the basis of this method. Unlike other IVP methods, this
involves four initial points in obtaining a new point. This is why it is also called as the four-step
method while the other methods are called as single-step. This has two parts, called the
predictor and the corrector, which equations are shown below, respectively:
+ 1 = +
(9 3 + 37 2 59 1 + 55
24
+ 1 = +
24
( 2 5 1 19 + 9 + 1 .
The two equations were derived from Lagrange approximation. Extrapolation was used to
obtain Pk + 1 while interpolation was used to obtain yk + 1. The resulting formula then, when
truncation error is considered negligible (it is always negligible for this method), is
( + 1 ) + 1
19
270
( + 1 + 1 ).
The step size may be adjusted whenever accuracy doesnt meet the prescribed error.
ALGORITHM FLOW
Page 133
PROGRAM FLOW
Page 134
MATLAB CODES
clc; clear all; format long;
disp('Adams-Bashfort-Moulton Method')
disp('will solve the IVP y=f(t,y) with y(a)=y0')
a=input('Enter the left end point of [a,b]:');
b=input('Enter the right endpoint of[a, b]:');
ya=input('Enter the initial value ya:');
n=input ('ENter the number of subintervals:');
h=(b-a)/n;T=zeros(1,n+1);
Y=zeros(1,n+1); b=a+3*h;m=6;
h=(b-a)/m;Ts=zeros(1,m+1);
Ys=zeros(1,m+1);Ts(1)=a;Ys(1)=ya;
for j=1:m,
tj=Ts(j);yj=Ys(j);
k1=h*f(tj, yj);k2=h*f(tj+h/2,yj+k1/2);
k3=h*f(tj+h/2,yj+k2/2);k4=h*f(tj+h,yj+k3);
Ys(j+1)=yj+(k1+2*k2+2*k3+k4)/6;
Ts(j+1)=a+h*j;
end
T(1:4)=Ts(1:2:7);
Y(1:4)=Ys(1:2:7);n=length(T);
f0=f(T(1),Y(1)); f1=f(T(2),Y(2));
f2=f(T(3),Y(3)); f3=f(T(4),Y(4));
h=T(2)-T(1); h2=h/24;a =T(1);
for k=4:n-1,
p=Y(k)+h2*(*f2+55*f3);
T(k+1)=a+h*k;f4=f(T(k+1),p);
Y(k+1)=Y(k)+h2*(f1-5*f2+19*f3+9*f4);
f0=f1;f1=f2;f2=f3;f3=f(T(k+1),Y(k+1));
end
points=[T;Y];
a=0;b=10;c=0;d=7;
plot([a b],[0 0],'b',[0 0],[c d],'b');
Page 135
axis(axis);
hold on; plot(T,Y,'-r','linewidth',2);
xlabel('t');ylabel('y');
Mx1='Adams-Bashforth-Moulton Method is stable';
Mx4=['for n=',num2str(n),' and h='];
Mx5=[Mx4, num2str(h),'becasue'];
Mx6='
h<0.75/|f(t,y)|';
disp(''),disp(Mx1),disp(''),disp(Mx2),disp(points')
disp(Mx3),disp(Mx5),disp(''),disp(Mx6),disp(Mx7)
Page 136
Milne-Simpson Method
This lesson discusses the Milne Simpson Method.
A desirable feature of a multistep method is that the local truncation error (L. T. E.) can be
determined and a correction term can be included, which improves the accuracy of the
answer at each step. Also, it is possible to determine if the step size is small enough to obtain
an accurate value for yk+1, yet large enough so that unnecessary and time-consuming
calculations are eliminated. If the code for the subroutine is fine-tuned, then the combination
of a predictor and corrector requires only two function evaluations of f(t,y) per step.
Another popular predictor-corrector scheme is known as the Milne - Simpson method. Its
predictor is based on integration of f(t, y(t)) over the interval [t k-3, tk+1]. The predictor uses the
Lagrange polynomial approximation for f(t, y(t)) based on the points (t k-3, fk-3), (tk-2, fk-2), (tk-1, fk-1)
and (tk, fk). It is integrated over the interval [tk-3, tk+1]. This produces the Milne predictor:
pk 1 y k 3
4h
2 f k 2 f k 1 2 f k
3
The corrector is developed similarly. The value pk+1 can now be used. A second Lagrange
polynomial for f(t, y(t)) is constructed which is based on the points (tk-2, fk-2), (tk-1, fk-1) and (tk, fk)
and the new point (tk+1, fk+1) = (tk+1, f(tk+1, pk+1)). The polynomial is integrated over [t k-1, tk+1] and
the result is the familiar Simpsons rule:
y k 1 y k 1
h
f k 1 4 f k f k 1
3
This method is similar to Adams-Bashforth-Moulton method in that it requires four initial points to
obtain the value of the new point. It also utilizes the predictor-corrector scheme and the
fundamental theorem of calculus is the basis of the method.
From this equation, the predictor can be improved through a modifier with the formula
+ 1 = + 1 +
28
29
( ).
+ 1 = 1 + ( 1 + 4 + ( + 1 , + 1 ).
3
Page 137
ALGORITHM FLOW
1. Declare a global variable z and symbolic variables y and t for the D. E. function.
2. Prompt the user to enter the function y in terms of y and t.
3. Prompt the user to enter the initial and final time, the initial value for y and the intended
number of subintervals.
4. Initialize results matrices Y and T at zero and calculate for the appropriate step size. Do
the same for intermediate results matrices Ys and Ts.
5. Calculate for intermediate results using the RK4 method.
6. Extract the 1st, 3rd, 5th and 7th elements of Ys and Ts and store as matrices Y and T,
respectively.
7. Calculate for the predictor and the corrector using the Milne-Simpson formulas. Use a
subfunction that substitutes a numerical value for the symbolic variables. Repeat as
needed.
8. Display results and end the program.
Page 138
PROGRAM FLOW
Page 139
MATLAB CODES
Page 140
points=[T;Y];
a=0; b=10; c=0; d=7;
plot([a b], [0 0],'b',[0 0],[c d],'b');
axis([a b c d ]); axis(axis);
hold on; plot(T,Y,'-r','linewidth',2);
xlabel('t');ylabel('y');
Mx1='Milne-Simpson Solution to y=f(t,y).';
title(Mx1);grid;hold off;
Mx2='
t(k)
y(k)';
h<0.45/|f(t,y)|';
Page 141
Page 142
where
and
, , , are positive real parameters describing the interaction of the two species.
Page 143
Prey
When multiplied out, the prey equation becomes:
The prey are assumed to have an unlimited food supply, and to reproduce exponentially
unless subject to predation; this exponential growth is represented in the equation above by
the term x. The rate of predation upon the prey is assumed to be proportional to the rate at
which the predators and the prey meet; this is represented above by xy. If eitherx or y is
zero then there can be no predation.
With these two terms the equation above can be interpreted as: the change in the prey's
numbers is given by its own growth minus the rate at which it is preyed upon.
Predators
The predator equation becomes:
In this equation,
similarity to the predation rate; however, a different constant is used as the rate at which
the predator population grows is not necessarily equal to the rate at which it consumes
the prey).
represents the loss rate of the predators due to either natural death or
The equations have periodic solutions and do not have a simple expression in terms of the
usual trigonometric functions, although they are quite tractable.
If none of the non-negative parameters ,,, vanishes, three can be absorbed into the
normalization of variables to leave but merely one behind: Since the first equation is
homogeneous in x, and the second one in y, the parameters / and /, are absorbable in the
normalizations of y and x, respectively, and into the normalization of t, so that only / remains
arbitrary. It is the only parameter affecting the nature of the solutions.
A linearization of the equations yields a solution similar to simple harmonic motion with the
population of predators trailing that of prey by 90 in the cycle.
Page 144
Frequency Plot
A simple example
Suppose there are two species of animals, a baboon (prey) and a cheetah (predator). If the
initial conditions are 80 baboons and 40 cheetahs, one can plot the progression of the two
species over time. The choice of time interval is arbitrary.
Frequency Plot
Page 145
One may also plot solutions parametrically as orbits in "phase-space", without representing time,
but with one axis representing the number of prey and the other axis representing the number of
predators for all times.
MATLAB CODES
% ppmodel2.m
% Matlab file for Part 2 of the Predator-Prey Models module
% in the Differential Equations section.
global a b c p
disp('********************************************')
disp('Part 2: The Lotka-Volterra Model')
disp('********************************************')
disp(' ')
format short
disp('Steps 1 thru 3: ')
disp('Enter your answers to the questions as')
disp('MATLAB comments for your')
disp('diary file.')
disp(' ')
disp('To continue afterwards, type the word return')
disp('and hit enter!')
disp(' ')
keyboard;
disp(' ')
disp('----------------------------------------------')
disp('Step 4:')
disp('Here are parameters needed by the model:')
a=1
b=0.03
c=0.4
p=0.01
disp(' ')
disp('To continue, hit any key!')
disp(' ')
pause
disp(' ')
disp('----------------------------------------------')
disp('Step 4 (cont.):')
disp('In order to draw the direction field,')
disp('for a system of differential equations,')
I c a must
n d ofirsta define
ll tha
i nMATLAB
g s i n function')
Christ who strengthens me
disp('you
disp('giving the two right hand sides of the')
disp('system in an m-file called de_rhs.m.')
disp(' ')
Page 146
% ppmodel3.m
% Matlab file for Part 3 of the Predator-Prey Models module
% of the Differential Equations section
global a b c p
disp('********************************************')
disp('Part 3: Graphical Representations')
disp('********************************************')
disp(' ')
format short
a=1;b=0.03;c=0.4;p=0.01;
disp('Step 1 and 2: ')
disp('We want to plot the prey population as a')
disp('function of time and likewise the predator')
disp('population as a function of time.')
disp('Let''s choose the first scenario in')
disp('which (x0,y0)=(15,15) and look at the')
disp('populations over two periods.')
disp(' ')
disp('First we solve the system over two periods.')
disp('For efficiency, we will use MATLAB''s')
disp('build-in numerical DE solver:')
disp(' [t,z]=ode45(function_name,[t0,tf],z0)')
disp('
where z0=[x0,y0]'', a column vector ')
disp('
of initial conditions.')
disp(' ')
disp('--------------------------------------------')
disp('Copy the following commands and paste them as')
disp('a group at a MATLAB prompt, then execute them.')
disp(' ')
disp(' z0=[15,15]''; ')
disp(' [t,z]=ode45(''de_rhs'',[0,24], z0); ')
disp(' x=z(:,1); ')
disp(' y=z(:,2); ')
disp(' ')
disp('To check, type the variable name: t, which')
disp('should give times from 0 to 24 used by ode45.')
disp('Then type the variable name: x to see the')
disp('corresponding x values.')
disp('Likewise, you can type y to see those values.')
disp(' ')
disp('To continue afterwards, type the word return')
disp('and hit enter!')
disp(' ')
keyboard;
disp(' ')
disp('------------------------------------------------')
disp('Step 1 (cont.): ')
I c a n let''s
d olook
a l lattahplot
i n gof
s the
i n x(t)
C hsolution')
rist who strengthens me
disp('Now,
disp('for the prey (in red). On the same plot, we ')
disp('will show the y(t) solution for the predator')
disp('(in green).')
Page 147
% ppmodel4.m
% Matlab file for Part 4 of the Predator-Prey Models module
% of the Differential Equations section
global a b c p
disp('********************************************')
disp('Part 4: Varying the Parameters')
disp('********************************************')
disp(' ')
format short
disp('When you hit any key, you will see the')
disp('direction field of the prey-predator')
disp('system along with the trajectories we')
disp('plotted for initial conditions (15,15),')
disp('(20,20), (25,25), and (30,30) in Part 2.')
disp(' ')
disp('The parameters used were a = 1, b = 0.03, ')
disp('c = 0.4, p = 0.01. ')
disp(' ')
disp('Hit any key to continue!')
pause
disp(' ')
a=1; b=0.03; c=0.4; p=0.01;
figure(2); clf; dirfield(0,250,0,150); hold on
z0=[15,15]'; [t,z]=ode45('de_rhs',[0,12],z0);
plot(z(:,1),z(:,2),'r')
z0=[20,20]'; [t,z]=ode45('de_rhs',[0,12],z0);
plot(z(:,1),z(:,2),'r')
z0=[25,25]'; [t,z]=ode45('de_rhs',[0,12],z0);
plot(z(:,1),z(:,2),'r')
z0=[30,30]'; [t,z]=ode45('de_rhs',[0,12],z0);
plot(z(:,1),z(:,2),'r')
disp('---------------------------------------------')
disp('Step 1:')
disp('Increase a by 50% and replot. Compare your ')
disp('new plot to the plot with the old parameters')
disp('in the figure 2 window. Describe the')
disp('changes you see using MATLAB comments in your')
disp('diary file.')
disp(' ')
disp('Set the parameters and then enter:')
disp(' ')
disp(' a = 1 ')
disp(' b = 0.03 ')
disp(' c = 0.4 ')
disp(' p = 0.01 ')
disp(' figure(1); clf; dirfield(0,250,0,150); hold on ')
disp(' [t,z]=ode45(''de_rhs'',[0,12], [15,15]''); ')
I c a n dz(:,2),
o a ''r'')
l l t') h i n g s i n C h r i s t w h o s t r e n g t h e n s m e
disp(' plot(z(:,1),
disp(' [t,z]=ode45(''de_rhs'',[0,12], [20,20]''); ')
disp(' plot(z(:,1), z(:,2), ''r'') ')
disp(' [t,z]=ode45(''de_rhs'',[0,12], [25,25]''); ')
Page 148
Page 149
As can be seen in the figure, the graph of the predators increases as the number of population
of the prey decreases.
Page 150
Now s and theta are related as arc length and central angle in a circle of radius L: s = L theta.
Thus, the second derivative of s is L times the second derivative of theta. That brings us to
our undamped model differential equation with a single dependent variable, the angular
displacement theta:
Page 151
Next, we add damping to the model. We make the simplest possible assumption about the
damping force, that it is proportional to velocity. Since arc length and central angle are
themselves proportional (with proportionality constant L), it makes no difference whether we use
linear or angular velocity. Having selected theta as our dependent variable, we will represent
the damping as proportional to angular velocity, say, -b (d theta / dt). The negative sign is
because the damping force has to be opposite the direction of motion. When we include this
term in the model, our equation becomes
When we bring all the terms to the left-hand side, our model equation becomes
with theta replacing y, g replacing k, and L replacing one occurrence of m. But there is an
important difference between the two equations: the presence of the sine function in pendulum
equation. Recall that for springs, trigonometric functions turned up only in the solutions. We know
the pendulum problem must have solutions, because we see the pendulum move. Indeed, the
Existence-Uniqueness Theorem for second-order equations assures that there will be a unique
solution for any given initial conditions. But the presence of sin in the differential equation makes
it impossible to give a simple formula that describes a solution function.
Page 152
MATLAB CODES
%
%
%
%
%
%
%
%
%
%
%
%% INITIALIZATION:
clear,clc
% system parameters
m = 5;
L = 1;
d = 0.002;
a = 0.3;
b = 0.2;
%
%
%
%
%
% simulation parameters
t0 = 0;
% initial time (s)
tf = 12;
% final time (s)
tstep = 0.05;
% time increment (s)
tspan = t0:tstep:tf;% time vector
% calculate initial state using initial height
h0 = 0.05;
% initial height (m) (h0 < 2L)
x0 = [acos(1-h0/L)
0]';
% initial state values:
% [angular position (radians),
% ang. velocity (rad/sec)]
% settings
aflag = 1;
% flag to enable animation creation
fignum = 1;
% figure number
moviename = 'pendulum.avi';
% avi file name for movie2avi
moviename2 = 'pendulum2.avi';
% avi file name for VideoWriter
%% SIMULATION:
% simulate dynamics of pendulum free oscilation (nonlinear)
[T, S] = ode45(@(t,y) F_nonlin(t,y,L),tspan,x0);
% simulate dynamics of pendulum free oscilation (nonlinear)
[T_L, S_L] = ode45(@(t,y) F_lin(t,y,L),tspan,x0); % could use lsim instead
%% POSTPROCESSING:
% Convert position of pendulum mass to cartesian coordinates:
% nonlinear result
X = L*sin(S(:,1));
Y = L*(1-cos(S(:,1)));
% linear result
Page 153
X_L = L*sin(S_L(:,1));
Y_L = L*(1-cos(S_L(:,1)));
% Calculate tangential velocity
% nonlinear result
Vt = L*S(:,2);
Vtmax = max(abs(Vt));
% linear result
Vt_L = L*S_L(:,2);
Vtmax_L = max(abs(Vt_L));
% Create mass shape vectors
V = [a/2 a/2 -a/2 -a/2
b/2 -b/2 -b/2 b/2];
% position plot window size
dt = (tf-t0)/8;
ndt = floor(dt/tstep);
% number of time increments displayed in window
h=figure(fignum); clf
set(h,'Position',[10 40 1000 400]);
%% ANIMATION
% step through each time increment and plot results
if aflag
vidObj = VideoWriter(moviename2);
vidObj.FrameRate = length(T)/(tf-t0);
open(vidObj);
F(length(T)).cdata = []; F(length(T)).colormap = []; % preallocate
end
for ti=1:length(T)
% Position and orientation of pendulum mass at T(ti)
% nonlinear
x1 = X(ti);
% x coord
y1 = Y(ti);
% y coord
s1 = S(ti,1);
% rotation
% linear
x1_L = X_L(ti);
% x coord
y1_L = Y_L(ti);
% y coord
s1_L = S_L(ti,1);
% rotation
% Create rotation matrices
R = [cos(s1) -sin(s1)
sin(s1) cos(s1)];
R_L = [cos(s1_L) -sin(s1_L)
sin(s1_L) cos(s1_L)];
R90 = [cos(-pi/2) -sin(-pi/2)
sin(-pi/2) cos(-pi/2)];
% Rotate and shift
% nonlinear
Vr = R*V; Vrs = Vr + repmat([x1 y1]',1,4);
% linear
Vr_L = R_L*V; Vrs_L = Vr_L + repmat([x1_L y1_L]',1,4);
% Prepare figure
Page 154
Page 155
if (ti-ndt) >= 1
% full time history indices
thi_L = ti-ndt:ti;
y2h_L = Y_L(thi_L);
else
% partial time history indices
thi_L = 1:ti;
y2h_L = Y_L(thi_L);
end
th_L = T_L(thi_L);
% plot position time history
hcolor = [200 225 255]/255;
plot(th_L,y2h_L,'b-','LineWidth',3,'Color',hcolor); hold on
axis([(tc-dt) (tc+dt) -b L])
% plot marker
plot(tc,y1_L,'k+','MarkerSize',18,'LineWidth',2,'MarkerEdgeColor',scolor)
plot(tc,y1_L,'ko','MarkerSize',10,'LineWidth',2,'MarkerEdgeColor',scolor)
% plot vertical position line
plot([(tc-dt) (tc+dt)],[y1_L y1_L],'r-')
% plot zero velocity line
plot([(tc-dt) (tc+dt)],[0 0],'k-')
% obtain time history for position to current time
% nonlinear
if (ti-ndt) >= 1
% full time history indices
thi = ti-ndt:ti;
y2h = Y(thi);
else
% partial time history indices
thi = 1:ti;
y2h = Y(thi);
end
th = T(thi);
% plot position time history
plot(th,y2h,'b-','LineWidth',3); hold on
axis([(tc-dt) (tc+dt) -b L])
% plot marker
plot(tc,y1,'k+','MarkerSize',18,'LineWidth',2)
plot(tc,y1,'ko','MarkerSize',10,'LineWidth',2)
% plot vertical position line
plot([(tc-dt) (tc+dt)],[y1 y1],'r-')
% plot zero velocity line
plot([(tc-dt) (tc+dt)],[0 0],'k-')
title('\fontsize{12}\fontname{Arial Black}Pendulum Height')
xlabel('\fontsize{10}\fontname{Arial Black}Time (sec)')
ylabel('\fontsize{10}\fontname{Arial Black}Height (m)')
set(gca,'XTick',0:ceil(tf)+1)
clear th th_L
clear thi thi_L
clear y2h y2h_L
% plot velocity time history
Page 156
ax(3) = subplot(1,3,3);
subplot('Position',[0.60 .34 0.125 0.42]); hold on
axis([-dt dt -Vtmax Vtmax])
% obtain velocity time history to current time
% linear
if (ti-ndt) >= 1
% full time history
thi_L = ti-ndt:ti;
Vth_L = Vt_L(thi_L);
else
% partial time history
thi_L = 1:ti;
Vth_L = Vt_L(thi_L);
end
th_L = T_L(thi_L);
% plot time history
plot(th_L,Vth_L,'g-','LineWidth',3,'Color',acolor); hold on
axis([(tc-dt) (tc+dt) -2*Vtmax 2*Vtmax])
% plot marker
plot(tc,Vt_L(ti),'k+','MarkerSize',18,'LineWidth',2,'MarkerEdgeColor',scolor)
plot(tc,Vt_L(ti),'ko','MarkerSize',10,'LineWidth',2,'MarkerEdgeColor',scolor)
% obtain velocity time history to current time
% nonlinear
if (ti-ndt) >= 1
% full time history
thi = ti-ndt:ti;
Vth = Vt(thi);
else
% partial time history
thi = 1:ti;
Vth = Vt(thi);
end
th = T(thi);
% plot time history
plot(th,Vth,'g-','LineWidth',3); hold on
axis([(tc-dt) (tc+dt) -2*Vtmax 2*Vtmax])
% plot marker
plot(tc,Vt(ti),'k+','MarkerSize',18,'LineWidth',2)
plot(tc,Vt(ti),'ko','MarkerSize',10,'LineWidth',2)
% plot zero velocity line
plot([(tc-dt) (tc+dt)],[0 0],'k-')
title('\fontsize{12}\fontname{Arial Black}Pendulum Velocity')
xlabel('\fontsize{10}\fontname{Arial Black}Time (sec)')
ylabel('\fontsize{10}\fontname{Arial Black}Velocity (m/s)')
set(gca,'XTick',0:ceil(tf)+1)
set(gcf,'Color','w')
if aflag
h=figure(fignum);
F(ti) = getframe(h,[0 100 750 250]);
writeVideo(vidObj,F(ti));
Page 157
end
end
if aflag
close(vidObj);
movie2avi(F,moviename,'fps',length(T)/(tf-t0),'compression','none')
end
MATLAB RUN/SIMULATION
Page 158
Page 159
MATLAB CODES
2')
disp('
u (x,t)
disp('
tt
= c
u (x,t)')
xx')
Page 160
h=a/(n-1);k=b/(m-1);
r=c*k/h; r2=r^2;
r22=r^2/2;s1=1-r^2;
s2=2-2*r^2; U=zeros(n,m);
for i=2:(n-1),
U(i,1)=f(h*(i-1));
U(i,2)=s1*f(h*(i-1))+k*g(h*(i-1))+r22*(f(h*(i))+f(h*(i-2)));
end
for j=3:m,
for i=2:(n-1),
U(i,j)=s2*U(i,j-1)+r2*(U(i-1,j-1)+U(i+1,j-1))-U(i,j-2);
end
end
h=a/(n-1);k=b/(m-1);
[X,T]= meshgrid(0:h:a,0:h:b);
meshz(T,X,U); xlabel('t'); ylabel('x');
zlabel('u'); view([1 1 1]);
title(Mx1);W(:,2:n-1);
disp(''), disp(Mx1), disp(''), disp(points)
Page 161
PROGRAM FLOW
Page 162
Page 163
Wave Equation
Heat Equation
Page 164
WAVE EQUATION
The wave equation is an important second-order linear partial differential equation for the
description
of waves
as
they
occur
in
physicssuch
and water waves. It arises in fields like acoustics, electromagnetics, and fluid dynamics.
The wave equation is a hyperbolic partial differential equation. It typically concerns a time
variable t, one or more spatial variables x1, x2, , xn, and a scalar functionu = u (x1, x2, , xn; t),
whose values could model, for example, the mechanical displacement of a wave. The wave
equation for u is
wave
equations,
and
modifications
of
it,
are
also
found
in elasticity, quantum
Page 165
Page 166
MATLAB CODES
% WAVE EQUATION
clc;
clear all;
%format short;
fprintf('\n');
disp('************************** WAVE EQUATION **************************');
fprintf('\n');
% This Matlab script creates a simple waveguide string model that can
% be used to view traveling-wave components
% as they move along the
% string.
%
%
% ******* Constants and Other Parameters *******
fs = 44100;
% sampling rate
N = 300;
% number of iterations to display
T = 1 / fs;
% sampling period
fprintf('\nSelect waveform type:\n\n');
fprintf(' 1. Pluck\n');
fprintf(' 2. Sinusoid\n');
fprintf(' 3. Impulse\n\n');
waveform = input('Enter option number: ');
if isempty(waveform),
waveform = 1;
end
if waveform < 1 || waveform > 3,
waveform = 1;
end
if waveform == 1,
pp = input('\nSet pluck point (0-1.0) [0.5]:\n\n');
if isempty(pp),
pp = 0.5;
end
if pp < 0.0 || pp > 1.0,
pp = 0.5; % pluck point between bridge (0) and nut (1.0)
end
end
if waveform == 2,
harmonic = input('\nSet harmonic number [1]:\n\n');
if isempty(harmonic),
harmonic = 1;
end
if harmonic < 1,
harmonic = 1;
end
end
% ******* String dimensions (m) ******* %
strLength = 0.33;
% real violin string length (meters)
c = [196, 294, 440, 660]*2*strLength; % velocities for 4 violin strings
strLength = 0.3;
% synthesized string length (meters)
Page 167
D = round(strLength*fs/c(1));
% length of string in samples
if ~mod(D,2),
% make D odd, better visual
D = D+1;
end
% ******* Simple String Dispersion Filter ******* %
b_disp = -0.97*[0.5 0.5];
a_disp = 1;
z_disp = 0;
% ******* Initialize delay lines ******* %
% Offset string samples by 1/2 sample to avoid a double zero end condition.
% In this case, the end of the string is actually 1/2 sample to the right
% and left of the end sample values seen. However, the displayed
% plots include the end points by incorporating an extra (repeated)
% sample on both the upper and lower delay lines.
if waveform == 1,
% triangular pluck
ppsamp = round(pp*D);
dlines = [[0.5:ppsamp-1+0.5]/(ppsamp-1), [(D-ppsamp-0.5):-1:0.5]/(Dppsamp)];
dlines = 0.45*[fliplr(dlines); dlines];
elseif waveform == 2, % sinusoidal
dlines = sin(harmonic*pi*[0.5:D-0.5]/D);
dlines = 0.45*[fliplr(dlines); dlines];
elseif waveform == 3, % pulse
plen = round(0.2*D);
if ~mod(plen,2),
plen = plen+1;
end
dlines = [0.5*sin(pi*[0:plen-1]/(plen-1)), zeros(1,D-plen)];
dlines = [fliplr(dlines); zeros(1,D)];
end
ptr = 1;
% ******* Plot initial string shape and traveling-wave components ******* %
x = [0:D]/(D);
upline = fliplr([dlines(1,ptr:D), dlines(1,1:ptr-1), -dlines(2,ptr)]);
dwnline = [dlines(2,ptr:D), dlines(2,1:ptr-1), -dlines(1,ptr)];
plot(x,upline+dwnline)
hold on
plot(x,upline,'r')
plot(x,dwnline,'g')
hold off
axis([0 1 -1 1])
grid
ylabel('Vertical Displacement')
xlabel('Normalized String Length')
disp('Paused ... hit any key');
pause
% *************************************** %
%
%
%
Delay Line(d)
%
%
|-------------------------------|
%
%
^
%
% pointer
%
%
%
%
>>--- pointer increments --->>
%
%
%
Page 168
% *************************************** %
%
% The pointer initially points to the delay line output.
% We can take the output and calculate a new input value
% which is placed where the output was taken from. The
% pointer is then incremented and the process repeated.
% ******* Run Loop Start ******* %
for i = 1:N,
temp = dlines(2,ptr);
dlines(2,ptr) = -dlines(1,ptr);
% Uncomment next line for commuted losses.
%[dlines(1,ptr),z_disp] = filter(b_disp, a_disp, temp, z_disp);
% Uncomment next line for lossless wave propagation.
dlines(1,ptr) = -temp;
% ****** Increment Pointers & Check Limits ****** %
ptr = ptr + 1;
if ptr > D
ptr = 1;
end
% ****** Plot string shape and traveling-wave components ****** %
if mod(i,1) == 0,
upline = fliplr([dlines(1,ptr:D), dlines(1,1:ptr-1), -dlines(2,ptr)]);
dwnline = [dlines(2,ptr:D), dlines(2,1:ptr-1), -dlines(1,ptr)];
plot(x,upline+dwnline)
hold on
plot(x,upline,'r')
plot(x,dwnline,'g')
hold off
axis([0 1 -1 1])
grid
ylabel('Vertical Displacement')
xlabel('Normalized String Length')
% Uncomment to trigger each tick with key hit
pause
% Uncomment to have a uniform pause between each tick
pause(0.05)
end
end
Page 169
MATLAB SIMULATION
Page 170
HEAT EQUATION
The heat equation is a parabolic partial differential equation that describes the distribution
of heat (or variation in temperature) in a given region over time.
For a function u(x,y,z,t) of three spatial variables (x,y,z) (see cartesian coordinates) and
the time variable t, the heat equation is
where is a positive constant, and or 2 denotes the Laplace operator. In the physical
problem of temperature variation, u(x,y,z,t) is the temperature and is the thermal diffusivity. For
the mathematical treatment it is sufficient to consider the case = 1.
The heat equation is of fundamental importance in diverse scientific fields. In mathematics, it is
the prototypical parabolic partial differential equation. In probability theory, the heat equation is
connected with the study of Brownian motion via the FokkerPlanck equation. In financial
mathematics it is used to solve the BlackScholes partial differential equation. The diffusion
equation, a more general version of the heat equation, arises in connection with the study of
chemical
diffusion
and other
related
processes.
Page 171
Page 172
MATLAB CODES
[u,err,x,t] = heat1(t_0,t_f,M,N)
%
%
%
%
%
%
this solves the heat equation u_t = u_xx with initial data u_0 =
cos(x) with periodic boundary conditions using finite-differences in
space and explicit time-stepping. t_0 is the initial time, t_f is the
final time, N is the number of mesh-points, and M is the number of
time steps. err is the error.
I just
for j=1:M
u(1,j+1) = u(1,j) + r*(u(2,j)-2*u(1,j)+u(N-1,j));
for i=2:N-1
u(i,j+1) = u(i,j) + r*(u(i+1,j)-2*u(i,j)+u(i-1,j));
end
u(N,j+1) = u(N,j) + r*(u(2,j)-2*u(N,j)+u(N-1,j));
err(:,j+1) = u(:,j+1) - exp(t_0-t(j+1))*cos(x);
end
Page 173
For better understanding, some boundary conditions are given to illustrate the Heat Equation.
Boundary Conditions
The heat equation is an example of what is known as a "partial differential equation." A
differential equation is any equation in which a function (Temperature in time and space in this
instance) is not represented directly, but via it's derivative. Partial indicates that there are at least
two variables (time and space) in the derivatives. An ordinary DE only has one derivative.
In order to solve a PDE numerically, we need to specify boundary conditions. The four
boundaries of our "space" are the surface (0m), the bottom depth (100m), an arbitrary
beginning time, and one year later.
To illustrate this:
Page 174
Page 175
Page 176
MATLAB CODES
clc;clear all; format short;
disp('Crank-Nicholson Method for the Heat Equation')
disp('
disp('
disp('
2')
u(x,t) = c u (x,t)')
t
xx')
Page 177
h=a/(n-1);k=b/(m-1);
r=c^2*k/h^2;s1=2+2/r;
s2=2/r-2; U=zeros(n,m);
for j=1:m,
U(1,j)=g1(k*(j-1));
U(n,j)=g2(k*(j-1));
end
for i=2:(n-1),
U(i,1)=f(h*(i-1));
end
Vd=s1*ones(1,n);Vd(1)=1;
Vd(n)=1;Va=-0nes(1,n-1);
Va(n-1)=0; Vc=-ones(1,n-1);
Vc(1)=0;Vb(1)=g1(k*0);
Vb(n)=g2(k*0);
for j=2:m,
for i=2:(n-1),
Vb(i)=U(i-1,j-1)+U(i+1,j-1)+s2*U(i,j-1);
end
A=Va;B=Vb;C=Vc;D=Vd;
n=length(B);
for k=2:n,
mult=A(k-1)/D(k-1);
D(k)=D(k)-mult*C(k-1);
B(k)=B(k)-mult*B(k-1);
end
X(n)=B(n)/D(n);
for k=(n-1):-1:1,
X(k)=(B(k)-C(k)*X(k+1))/D(k);
end
U(1:n,j)=X';
end
h=a/(n-1);k=b/(m-1);
[X,T]=meshgrid(0:h:a,0:h:b);
Page 178
meshz(T,X,U);xlabel('t');
ylabel('x');zlabel('u');
view([1 1 1]);
Mx1='The Crank Nicholson solution to the heat equation.';
title(Mx1);
W=U';
pointd=W(:,2:n-1);
disp(''),disp(Mx1),disp(''),disp(points)
Page 179
PROGRAM FLOW
Page 180
Page 181
Page 182
MATLAB CODES
2')
disp('
u(x,t)
disp('
= c u(x,t)')
xx')
Page 183
end
for i=1:n,
U(i,1)=f1(h*(i-1));
U(i,m)=f2(h*(i-1));
end
U(i,i)=(U(1,2)+U(2,1))/2;
U(1,m)=(U(i,m-1)+U(2,m))/2;
U(n,1)=(U(n-1,1)+U(n,2))/2;
U(n,m)=(U(n-1,m)+U(n,m-1))/2;
w=4/(2+sqrt(4-(cos(pi/(n-1))+cos(pi/(m-1)))^2));
err=1;cnt=0;
while ((err>tol)&&(cnt<=max1))
err=0;
for j=2:(m-1),
for i=2:(n-1),
relx=w*(U(i,j+1)+U(i,j-1)+U(i+1,j)+U(i-1,j)-4*U(i,j))/4;
U(i,j)=U(i,j)+relx;
if (err<=abs(relx)),err=abs(relx);end
end
end
cnt=cnt+1;
end
[X,Y]=meshgrid(0:h:a,0:h:b);
W=rot90(U);W=flipud(W);
meshz(X,Y,W);xlabel('x');
ylabel('y');zlabel('u');
view([1 -1 1]);
Mx1='The solution to Laplace Equation.';
title(Mx1);
W=rot90(U);
disp(''),disp(Mx1),disp(''),disp(W)
Page 184
Page 185
Page 186