You are on page 1of 187

Simplified Engineering Approach to

Numerical Methods: Compilation of


Algorithms Using MATLAB
FIRST EDITION
2015

Julito B. Anora Jr., REE

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Dedication:

To Atty. Nina Valenzona


and Lola Sising:

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.

I can only cherish our memories shared.

Wherever you are now


This book is humbly dedicated.

I can do all things in Christ who strengthens me

Page 1

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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.

I can do all things in Christ who strengthens me

Page 2

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

-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

I can do all things in Christ who strengthens me

Page 3

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/SIMULATION ...................................................................................................................... 43


Jacobi Method ............................................................................................................................................ 44
ALGORITHM FLOW .................................................................................................................................. 45
PROGRAM FLOW ..................................................................................................................................... 46
MATLAB SOURCE CODE ......................................................................................................................... 47
SAMPLE RUN/SIMULATION..................................................................................................................... 48
Gauss-Siedel Method ................................................................................................................................. 49
ALGORITHM FLOW .................................................................................................................................. 49
PROGRAM FLOW ..................................................................................................................................... 50
MATLAB SOURCE CODE ......................................................................................................................... 51
SAMPLE RUN/SIMULATION ...................................................................................................................... 52
Chapter 3 Part 2: The Solution of Linear Systems AX=B ........................................................................ 53
Upper Triangularization Followed by Back Substitution Method ....................................................... 54
........................................................................................................................................................................ 55
PROGRAM FLOW ..................................................................................................................................... 55
MATLAB FILE SOURCE CODE ................................................................................................................. 56
SAMPLE RUN/SIMULATION ...................................................................................................................... 57
LU Factorization with Pivoting ................................................................................................................... 58
ALGORITHM FLOW .................................................................................................................................. 58
PROGRAM FLOW ..................................................................................................................................... 59
MATLAB SOURCE CODE ......................................................................................................................... 60
SAMPLE RUN/SIMULATION ...................................................................................................................... 62
Jacobi Method for Solving Linear Systems AX=B .................................................................................. 63
MATLAB CODES/SIMULATION ................................................................................................................ 64
SAMPLE MATLAB RUN/SIMULATION ...................................................................................................... 65
........................................................................................................................................................................ 65
Gauss-Siedel Method for Solving Linear Systems AX=B ....................................................................... 66
MATLAB CODES ....................................................................................................................................... 66
SAMPLE MATLAB RUN / SIMULATION .................................................................................................... 68
Chapter 4: Interpolation and Polynomial Approximation .................................................................. 69
Lagrange Method ...................................................................................................................................... 70
PROGRAM FLOW ......................................................................................................................................... 71
MATLAB SOURCE CODES ....................................................................................................................... 72

I can do all things in Christ who strengthens me

Page 4

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/SIMULATION ...................................................................................................................... 73


Chapter 5: Curve Fitting ........................................................................................................................... 76
LEAST SQUARE LINES .................................................................................................................................... 77
PROGRAM FLOW ..................................................................................................................................... 78
MATLAB SOURCE ..................................................................................................................................... 79
SAMPLE RUN/SIMULATION ...................................................................................................................... 80
LEAST SQUARE POLYNOMIALS ................................................................................................................... 82
PROGRAM FLOW ..................................................................................................................................... 83
MATLAB SOURCE CODE ......................................................................................................................... 83
SAMPLE RUN/SIMULATION ...................................................................................................................... 85
NON LINEAR CURVE FITTING ...................................................................................................................... 86
PROGRAM FLOW ..................................................................................................................................... 87
MATLAB SOURCE CODE ......................................................................................................................... 88
SAMPLE RUN/SIMULATION .......................................................................................................................... 91
FOURIER SERIES AND TRIGONOMETRIC EXPANSION .............................................................................. 92
Chapter 6: Numerical Differentiation ...................................................................................................... 93
CENTRAL DIFFERENCE FORMULAS ............................................................................................................ 94
FORWARD DIFFERENCE METHOD FOR THE HEAT EQUATION ............................................................... 97
MATLAB CODES ....................................................................................................................................... 97
PROGRAM FLOW ..................................................................................................................................... 99
SAMPLE MATLAB RUN/SIMULATION ....................................................................................................100
Composite Trapezoidal Rule ...................................................................................................................102
ALGORITHM FLOW ................................................................................................................................103
PROGRAM FLOW ...................................................................................................................................104
MATLAB SOURCE CODE .......................................................................................................................105
SAMPLE RUN/SIMULATION ....................................................................................................................107
Composite Simpsons Rule ......................................................................................................................108
PROGRAM FLOW .......................................................................................................................................110
ALGORITHM FLOW ................................................................................................................................111
MATLAB SOURCE CODE FILE ...............................................................................................................111
SAMPLE RUN/MATLAB ...........................................................................................................................113
Chapter 9, Part 1: Solution of Differential Equations ............................................................................ 114
EULERS METHOD .........................................................................................................................................115

I can do all things in Christ who strengthens me

Page 5

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW .......................................................................................................................................116


MATLAB CODES .....................................................................................................................................117
MATLAB SAMPLE RUN ...........................................................................................................................118
HEUNS METHOD .........................................................................................................................................119
ALGORITHM FLOW ................................................................................................................................119
PROGRAM FLOW ...................................................................................................................................120
MATLAB CODES .....................................................................................................................................121
MATLAB SAMPLE RUN ...........................................................................................................................122
TAYLOR METHOD OF ORDER 4 ................................................................................................................123
PROGRAM FLOW ...................................................................................................................................124
MATLAB CODES .....................................................................................................................................125
MATLAB SAMPLE RUN ...........................................................................................................................126
Runge-Kutta Method (RK4) .....................................................................................................................127
PROGRAM FLOW ...................................................................................................................................128
ALGORITHM FLOW ................................................................................................................................129
MATLAB CODES .....................................................................................................................................129
MATLAB SAMPLE RUN................................................................................................................................131
Chapter 9, Part 2: Solution of Differential Equations ............................................................................ 132
Adams-Bashforth-Moulton Method .......................................................................................................133
ALGORITHM FLOW ................................................................................................................................133
PROGRAM FLOW ...................................................................................................................................134
MATLAB CODES .....................................................................................................................................135
MATLAB SAMPLE RUN ...........................................................................................................................136
Milne-Simpson Method ............................................................................................................................137
ALGORITHM FLOW ................................................................................................................................138
PROGRAM FLOW .......................................................................................................................................139
MATLAB CODES .....................................................................................................................................140
SAMPLE MATLAB RUN/SIMULATION ........................................................................................................142
Systems of Ordinary Differential Equations/Predator-Prey Model ...................................................143
Physical meanings of the equations .................................................................................................143
Prey ..........................................................................................................................................................144
Predators.................................................................................................................................................144
A simple example .................................................................................................................................145

I can do all things in Christ who strengthens me

Page 6

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB CODES .....................................................................................................................................146


SAMPLE MATLAB RUN/SIMULATION ....................................................................................................149
Higher Order Differential Equations/Pendulum Model ......................................................................151
MATLAB CODES .....................................................................................................................................153
MATLAB RUN/SIMULATION ...................................................................................................................158
FINITE DIFFERENCE EQUATION FOR WAVE EQUATION ........................................................................159
MATLAB CODES .....................................................................................................................................160
PROGRAM FLOW ...................................................................................................................................162
SAMPLE MATLAB RUN/SIMULATION ....................................................................................................163
Chapter 10: Solution of Partial Differential Equations .......................................................................... 164
WAVE EQUATION .......................................................................................................................................165
MATLAB CODES .....................................................................................................................................167
MATLAB SIMULATION ............................................................................................................................170
HEAT EQUATION .........................................................................................................................................171
MATLAB CODES .....................................................................................................................................173
Boundary Conditions ............................................................................................................................174
CRANK NICHOLSON METHOD FOR HEAT EQUATION ..........................................................................177
......................................................................................................................................................................177
MATLAB CODES .....................................................................................................................................177
PROGRAM FLOW .......................................................................................................................................180
SAMPLE MATLAB RUN/SIMULATION ........................................................................................................181
DIRICHLET METHOD FOR LAPLACEs EQUATION ..............................................................................182
MATLAB CODES .....................................................................................................................................183
SAMPLE MATLAB RUN/SIMULATION ....................................................................................................185

I can do all things in Christ who strengthens me

Page 7

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 1. Part 1: Nonlinear Equations (Single


Equation)

Fixed Point Method

Bisection Method

False Position Method

Newton Raphson Method

Secant Method

I can do all things in Christ who strengthens me

Page 8

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

FIXED POINT ITERATION


This method is one of the most basic to determine the root of a non-linear equation. In order to
solve a nonlinear equation f(x) = 0 using fixed point iteration, the equation must first be
rearranged to the form
= ().
Start the iteration with a certain value x = x0, substitute it at g(x) and check if
|x g(x)|
where is the acceptable error. If the condition is not satisfied, g(x) becomes the new value for
x, repeat the iteration until the given condition is satisfied. The limitation of this method is at many
instances, the iteration diverges from or oscillates between the correct value of root. Hence, the
challenge is to rearrange the equation ensuring convergence and to estimate x 0 well. This may
require more than one round of approximation attempt.

UNDERLYING PRINCIPLES AND THEOREMS


Theorem 1.1.

Assume the g(x) is a continuous function and that


point. If

pn n0

is a sequence generated by fixed

lim pn P , then P is a fixed point of g(x). Supposed that lim pn P then


n

lim pn1
n

P,

P lim pn 1 lim g pn g lim pn g P


n
n
n
Theorem 1.2.

Assume that

a xb

g Ca, b .

If the range of mapping y = g(x) satisfies.

a yb

for all

then g has a fixed point in [a , b]. Furthermore, suppose that g(x) is defined over

(a , b) and that a positive constant K < 1 exists with

g ' x K 1 for all x a, b

then g

has a unique fixed point P in [a , b].

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

g ' x K 1 for all a x b , then the iteration pn g pn 1 will converge to P. If

g ' x 1 for all a x b

then the iteration

pn g pn 1 will not converge to P.

I can do all things in Christ who strengthens me

Page 9

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

------------------------------- does not converge

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)

Differentiating the g(x), thus

g ' ( x)

1
10

2 4
10
x

2
4x
x

For x = 1, then

g ' ( x) 2.86 > 1


Therefore, the g(x) will diverge in the interval [1 , 2]
2.

x g ( x)

1
10 x 3
2

Differentiating the g(x), thus

g ' ( x)

3x 2
4 10 x 3

For x = 1, then

g ' ( x) 0.25 < 1


For x = 2, then

g ' ( x) 2.12 > 1


For x = 1.5, then

g ' ( x) 0.66 < 1


Therefore, the g(x) will diverge In the interval [1 , 1.5]

I can do all things in Christ who strengthens me

Page 10

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Fixed Point Iteration behaves monotone convergence, oscillating convergence, monotone


divergence, oscillating divergence.

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 11

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

The Algorithm prompts the user to input the desired equation.

MATLAB CODES SOURCE FILE

clc; clear all; format long


disp('Fixed Point Iteration')
g =

inline(input('Enter a funciton g(x)=','s'));

p0 =

input('Enter starting value:');

tol =

input('Enter convergence tolerance:');

m_iter =

input('Enter maximum number of iterations:');

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;

I can do all things in Christ who strengthens me

Page 12

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

SAMPLE RUN/SIMULATION OF THE PROGRAM

I can do all things in Christ who strengthens me

Page 13

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

I can do all things in Christ who strengthens me

Page 14

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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.

I can do all things in Christ who strengthens me

Page 15

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

I can do all things in Christ who strengthens me

Page 16

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

In general, the Bisection Iteration Method can be described as below:

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.

I can do all things in Christ who strengthens me

Page 17

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 18

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB CODES SOURCE FILE

clc; clear all; format long;


disp('BISECTION METHOD')
f= inline(input('Enter function f(x)=0: ','s'));
a= input('Enter the left endpoint:');
b= input ('Enter the right end point:');
tol= input ('Enter the tolerance:');
ya= feval (f,a);
yb= feval (f,b);
if ya*yb>0, break, end
max1= 1+round((log(b-a)-log(tol))/log(2));
for k=1:max1,
c=(a+b)/2;
yc=feval (f,c);
if yc==0,
a=c;
b=c;
elseif yb*yc >0,
b=c;
yb=yc;
else
a=c;
ya=yc;
end
A(k)=a;
B(k)=b;
if (b-a)<tol, break, end
end
R=[A;B];
c=(a+b)/2;
err=abs(b-a)/2;

I can do all things in Christ who strengthens me

Page 19

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

disp('')
disp(''),disp('Iterations for the bisection method')
disp(''), disp(' a

b'), disp(R')

disp(''), disp('THe approximate root is:'), disp(c)


disp(''), disp('The error estimate is:'), disp(err)
a=round(c)-2;
b=round(c)+2;
h=(b-a)/500;
i=1;
for x=a:h:b;
Y(i)=feval(f,x);
X(i)=x;
i=i+1;
end
plot(X,Y,'-b','linewidth',2.5), hold on
plot(c,0,'.r','markersize',30)
title('The curve y=f(x)');
grid on
hold off

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 20

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

The curve y=f(x)


0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
-0.05
-0.1
-0.15

0.5

1.5

2.5

3.5

The simulation prompts the user to input the function:

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.

I can do all things in Christ who strengthens me

Page 21

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

The curve y=f(x)


1

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

I can do all things in Christ who strengthens me

Page 22

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Regula Falsi Method


This lesson introduces another type of bracketing methods,that is the False Position method. This
is another popular numerical method and it is also known as Regula Falsi method. It was
developed of the fact that the bisection method has a slow rate of convergence. False
position method is better compared to bisection method. Since the approximation of the False
position method is obtained from the point (xm,0) where the secant line L joining the points (x l ,
f(xl)) and (xu , f(xu)) crosees the x axis(see the figure below).

Then to find the value of c, that is


Using the concept of slope, the slope of the line L is:

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

Choosing the interval for False Position method:


- If f(xl) and f(xm) have opposite signs, a root lies in [xl, xm].
- If f(xm) and f(xu) have opposite signs, a root lies in [xm, xu].
- If f(xm) = 0, we found at x = xm.
Sequence for solving xmn :

xmn xun

f xun xun xln


f xun f xln

I can do all things in Christ who strengthens me

Page 23

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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.

CONVERGENCE OF FALSE POSITION METHOD


When xm approaches to the roots of the nonlinear equation, f(x m) 0.
Thus, |xu - xl| will getting smaller.

MATLAB CODES SOURCE FILE

clc;clear all; format long;


disp('REGULAR-FALSI METHOD/FALSE POSITION METHODS')
f = inline(input('Enter the function f(x)=0:','s'));
a= input('Enterr the left endpoint of the initial interval: ');
b= input('Enter the right endpoint of the interval:
tol= input('Enter desired convergence tolerance:

');

');

max_iter=input('Enter the maximum numer of iterations:');


P=[a b];
ya=feval(f,a);
yb=feval(f,b);
if ya*yb>0, break, end
for k=1:max_iter,
dx=yb*(b-a)/(yb-ya);
c=b-dx;
ac=c-a;
yc=feval(f,c);
if yc==0,
break;
elseif yb*yc>0,
b=c;
yb=yc;
else
a=c;
ya=yc;
end
P=[P;a b];
dx=min(abs(dx),ac);
if abs(dx)<tol, break, end
end
err=abs(dx);

I can do all things in Christ who strengthens me

Page 24

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

disp('')
disp('')
disp(''), disp('Iterations for the Regula-Falsi/False Position Method')
disp(''), disp('

b'), disp(P)

disp(''), disp('THe approximate root is: '), disp(c)


disp(''), disp('The error estimate is :'), disp(err)
a=round(c)-2;
b=round(c)+2;
h=(b-a)/500;
i=1;
for x=a:h:b;
Y(i)=feval(f,x);
X(i)=x;
i=i+1;
end
plot(X,Y,'-b','linewidth',2.5), hold on
plot(c,0,'.r', 'markersize',30)
title('THe curve y=f(x)');
grid on
hold off

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 25

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

THe curve y=f(x)


2

1.5

0.5

-0.5

-1

-1.5

-2

1.5

2.5

3.5

4.5

I can do all things in Christ who strengthens me

Page 26

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

-12

Julito B. Anora Jr. MEEE-CSO

THe curve y=f(x)

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 .

I can do all things in Christ who strengthens me

Page 27

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Newton -Raphson Method


This is another method used to find the root of nonlinear equations which can be much faster
than bisection method. It must be provided that the rst derivative of function f(x) is existing and
continuous. The method uses the instantaneous values of the tangential lines to f(x) for certain
values of the independent variable x until the function is very near or at the x-axis. The method is
suited for functions which slopes are not steep such that only few approximations of the root
already lead to an accurate estimate.
Newton Raphson method, or simply as Newton method, is a powerful technique for solving
nonlinear equations. Also, it is under types of slope method. The Newton method reduces a
nonlinear problem to a sequence of linear problems, where the roots are easy to compute.
Let f(x) be a well behaved function, and let p be a root of the equation f(x) = 0. Starting from
the initial root p0 , then drawing a tangent line at point (p 0 , f(p0)). The tangent line must cross to
the x axis. With that, p1 is determined and the sequence continue.

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

Therefore, the iteration formula for Newton Raphson is:

pk g pk 1 pk 1

f pk 1
f ' pk 1

I can do all things in Christ who strengthens me

Page 28

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 29

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SOURCE CODE

clc; clear all; format long;


disp('NEWTON-RAPHSON METHOD')
syms x
f=input('Enter the function f(x)=0: ','s');
P0= input('Enter the starting value:');
tol= input('Enter the convergence tolerance for P0:');
max_iter=input('Enter the maximum number of iterations:');
df=diff(f,x);
f=inline(f);
df=inline(df);
P(1)=P0;
y0=feval(f,P0);
a(1)=0;
for k=1:max_iter,
df0=feval(df,P0);
if df0==0,
dp=0;
else
dp=y0/df0;
end
p1=P0-dp;
y1=feval(f,p1);
err=abs(dp);
P0=p1;
y0=y1;
P=[P;p1];
a=[a;k];
if err<tol, break, end
end

I can do all things in Christ who strengthens me

Page 30

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

P=[a,P];
disp(''),disp('Iterations for the Newton-Raphson Method')
disp(''), disp('

Iters #

P'), disp(P)

disp(''), disp('THe approximate root is :'), disp(P0)


disp(''), disp('The error estimate is:'), disp(err)
a=round(P0)-2;
b=round(P0)+2;
h=(b-a)/500;
i=1;
for x=a:h:b;
Y(i)=feval(f,x);
X(i)=x;
i=i+1;
end
plot(X,Y,'-b','linewidth',2.5),hold on
plot(P0,0,'.r','markersize',30)
title('THe curve y=f(x)');
grid on
hold off

I can do all things in Christ who strengthens me

Page 31

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/SIMULATION

THe curve y=f(x)


100

50

-50

-100

-150

-200

-250

-300

-350
-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

I can do all things in Christ who strengthens me

Page 32

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

THe curve y=f(x)


0.25

0.2

0.15

0.1

0.05

-0.05

-0.1

0.5

1.5

2.5

3.5

I can do all things in Christ who strengthens me

Page 33

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

Solving for the slope of the line,

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

Therefore, the iteration formula for Secant method is:

pn 1 g pn , pn 1 pn

f pn pn pn 1
f pn f pn 1

I can do all things in Christ who strengthens me

Page 34

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 35

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SOURCE CODE


clc; clear all; format long;
disp('SECANT METHOD')
syms x
f= input('Enter the fucntion f(x)=0:','s');
p0=input('Enter the starting value P0:');
p1=input('Enter the starting value P1:');
tol=input('Enter the convergence tolerance for P:');
max_iter=input('Enter the maximum number of iterations:');
f=inline(f);
H(1)=p0;
H=[H,p1];
y0=feval(f,p0);
y1=feval(f,p1);
a(1)=0;
a=[a;1];
for k=1:max_iter,
df=(y1-y0)/(p1-p0);
if df==0,
dp=0;
else
dp=y1/df;
end
p2=p1-dp;
y2=feval(f,p2);
err=abs(dp);
p0=p1;
y0=y1;
p1=p2;
y1=y2;
H=[H, p2];
a=[a;k+1];
if err<tol, break, end

I can do all things in Christ who strengthens me

Page 36

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

end
p=p1;
H=[a, H];
disp(''),disp('Iterations for the Secant Method')
dis(''), disp('

Iter #

P'), disp(H)

disp(''), disp(' The approximate root is:'),disp(P)


disp(''), disp('The error estimate is :'), disp(err)
a=round(P)-2;
b=round(P)+2;
h=(b-a)/500;
i=1;
for x=a:h:b;
Y(i)=feval (f,x);
X(i)=x;
i=i+1;
end
plot(X,Y,'-b','linewidth',2.5), hold on
plot(P,0,'.r','markersize',30)
title('THe curve y=f(x)');
grid on
hold off

I can do all things in Christ who strengthens me

Page 37

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 38

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 2, Part 2: Nonlinear Equations (System of


Nonlinear Equations)

Newton-Raphson Method for


Solving Systems of Nonlinear
Equations

Jacobi Method for Solving


Systems of Nonlinear Equations

Gauss-Siedel Method for Solving


Systems of Nonlinear Equations

I can do all things in Christ who strengthens me

Page 39

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

ff1 ppk ,, qqk


2 k k

F P
k

Step 2. Evaluate the Jacobian

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

Step 4. Compute the next point:

P
P P
k 1
k

I can do all things in Christ who strengthens me

Page 40

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 41

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SOURCE CODE

clc; clear all; format long;


disp('NEWTON RAPHSON METHOD IN 2D')
syms x y
f1= input('Enter the function f1(x,y) =0 ;','s');
f2= input('Enter the function f2(x,y)=0: ','s');
p0=input('Enter starting value p0:');
q0= input('Enter starting value q0:');
tol= input('Enter tolerance:');
m_ter=input('Enter the maximum number od iterations:');
J11= diff(f1,x);
J21=diff(f2,x);
J12=diff(f2,y);
J22=diff(f2,y);
J11=inline(J11);
J12=inline(J12);
J22=inline(J22);
f1=inline(f1);
f2=inline(f2);
P0=[p0 q0];
P=P0;
Y0=[feval(f1,p0,q0); feval(f2,p0,q0)];
for k=1:m_ter,
dF=[feval(J11,p0) feval(J12,q0);feval(J21,p0) feval(J22,q0)]
if det(dF)==0,
dP=[0 0];
else
dP=([dF dFF;dFF1 dFF2]\Y0)';
end
P1=P0-dP;
p0=P1(1);

I can do all things in Christ who strengthens me

Page 42

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)

disp(''), disp('The solution is:')


disp(''), disp('p='), disp(p0)
disp(''), disp('q='), disp(q0)
disp(''), disp('The error estimate is:'), disp(err)

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 43

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

I can do all things in Christ who strengthens me

Page 44

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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.

I can do all things in Christ who strengthens me

Page 45

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 46

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SOURCE CODE


clc; clear all; format long;
disp('JACOBI ITERATION')
disp('Note: Coefficient matrix A must be diagonally dominant.')
A=input('Enter coefficient matrix A:');
B=input('Enter B as a column vector:');
P=input('Entre starting vector as a column vector:');
tol=input('Enter the convergence tolerance:');
m_iter=input('Enter the maximum number of iterations:');
H=P';
n=length(B);
Pnew=P;
for k=1:m_iter,
for r=1:n,
Sum1=B(r)-A(r,[1:r-1,r+1:n])*P([1:r-1,r+1:n]);
Pnew(r)=Sum1/A(r,r);
end
dP=abs(Pnew-P);
err=norm(dP);
P=Pnew;
H=[H;P'];
Ps=P;
if err<tol, break, end
end
disp('The coefficient matrix A='),disp(A)
disp('The vector matrix B='),disp(B)
disp('History of Iterations')
disp('

z'),

disp(H)
disp('The solution vector Ps is:'),disp(Ps)
disp('The estimated error is equal to'),disp(err)

I can do all things in Christ who strengthens me

Page 47

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 48

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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.

I can do all things in Christ who strengthens me

Page 49

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

I can do all things in Christ who strengthens me

Page 50

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SOURCE CODE


clc; clear all; format long;
disp('GAUSS-SEIDEL')
disp('Note: Coefficient matrix A must be diagonally dominant.')
A=input('Enter coefficient matrix A:');
B=input('Enter B as a column vector:');
P=input('Entre starting vector as a column vector:');
tol=input('Enter the convergence tolerance:');
m_iter=input('Enter the maximum number of iterations:');
H=P';
n=length(B);
Pold=P;
for k=1:m_iter,
for r=1:n,
Sum1=B(r)-A(r,[1:r-1,r+1:n])*P([1:r-1,r+1:n]);
P(r)=Sum1/A(r,r);
end
dP=abs(Pold-P);
err=norm(dP);
Pold=P;
H=[H;P'];
Ps=P;
if err<tol, break, end
end
disp('The coefficient matrix A='),disp(A)
disp('The vector matrix B='),disp(B)
disp('History of Iterations')
disp('

z'),

disp(H)
disp('The solution vector Ps is:'),disp(Ps)
disp('The estimated error is equal to'),disp(err)

I can do all things in Christ who strengthens me

Page 51

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 52

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 3 Part 2: The Solution of Linear Systems


AX=B

Upper Triangularization Followed


by Back Substitution
Gaussian Elimination/LU
Factorization with Pivoting
Jacobi Method for Solving Linear
Systems AX=B

Gauss-Siedel Method for Solving


Linear Systems AX=B

I can do all things in Christ who strengthens me

Page 53

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Upper Triangularization Followed by Back Substitution Method


If we are given a linear system AX = B where A is the coefficients matrix, X the variables matrix
and B the constants matrix, rules of matrix manipulations can be applied to convert A to an
upper triangular matrix U and consequently, matrix B to Y. Back substitution, in which the last
variable declared in the matrix will be the first to be solved while the first variable will be solved
last, can then be used to solve the whole system. In symbols, from modified n x n system UX = Y,
=

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.

I can do all things in Christ who strengthens me

Page 54

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 55

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB FILE SOURCE CODE


clc; clear all; format long;
disp('Upper Triangularization Follwoed by Back Substitutituion')
A= input('Enter the nxn coeeficient matrix A:');
B=input('Enter the constant vector B as a column vector:');
[n n]=size(A);
C=A;
A=[A';B']';
X=zeros(n,1);
for p=1:n-1,
[Max J]=max(abs(A(p:n,p)));
T=row(p);
row(p)=row(J+p-1);
row(J+p-1)=T;
if A(row(p),p)==0,break,end
for k=p+1:n,
M=A(row(k),p)/A(row(p),p);
A(row(k),p+1:n+1) = A(row(k),p+1:n+1)-M*A(row(p),p+1:n+1);
end
end
X(n)=A(row(n),n+1)/A(row(n),n);
for k=n-1;-1:1,
X(k)=(A(row(k),n+1)-A(row(k),k+1:n)*X(k+1:n))/A(row(k),k);
end
A=C;
R=B-A*X;
disp(''), disp('Upper-Triangularization follwed by back sustitution.')
disp('The matrix is A=')
disp(B')
disp('The solution to AX=B is displayed as X='),
disp(X'), disp('The residual R=B-A*X is displayed as R='), disp(R')

I can do all things in Christ who strengthens me

Page 56

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 57

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

LU Factorization with Pivoting


Using this method of solving linear systems, the upper and lower triangular matrices of the forms
11
= 0
0

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.

I can do all things in Christ who strengthens me

Page 58

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 59

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SOURCE CODE

clc; clear all; format long;


disp('LU Factorization with Pivotin')
A= input('Enter the nxn coefficient matrix A:');
B= input('Enter the constant vector as a column vector:');
C=A;
[n n] = size(A);
det1=1;
row=1:n;
for p=1:n-1,
[max1 prow]=max(abs(A(p:n,p)));
if p< prow+p-1,
temp= row(p);
row(p)=row(prow+p-1);
row(prow+p-1)=temp;
det1=-det1;
end
det1=det1*A(row(p),p);
if det1==0, break, end
for k=p+1:n,
mult=A(row(k),p)/A(row(p),p);
A(row(k),p)=mult;
A(row(k),p+1:n)=A(row(k),p+1:n)-mult*A(row(p),p+1:n);
end
end
det1=det1*A(row(n),n);
LU=A;
[n,n]=size(A);
Y=zeros(n,1);
Y(1)=B(row(1));
for k=2:n,
Y(k)=B(row(k))-A(row(k),1:k-1)*Y(1:k-1);

I can do all things in Christ who strengthens me

Page 60

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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')

I can do all things in Christ who strengthens me

Page 61

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 62

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Jacobi Method for Solving Linear Systems AX=B


In numerical linear algebra, the Jacobi method (or Jacobi iterative method) is an algorithm for
determining the solutions of a diagonally dominant system of linear equations. Each diagonal
element is solved for, and an approximate value is plugged in. The process is then iterated until it
converges. This algorithm is a stripped-down version of the Jacobi transformation method of
matrix diagonalization. The method is named after Carl Gustav Jacob Jacobi.
Given a square system of n linear equations:

Where:

Then A can be decomposed into a diagonal component D, and the remainder R:


The solution is then obtained iteratively via

where

is the kth approximation or iteration of

and

is the next or k + 1 iteration of

The element-based formula is thus:

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.

I can do all things in Christ who strengthens me

Page 63

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB CODES/SIMULATION

function [x, error, iter, flag] = jacobi(A, x, b, max_it, tol)


% [x, error, iter, flag] = jacobi(A, x, b, max_it, tol)
%
% jacobi.m solves the linear system Ax=b using the Jacobi Method.
%
% input A

REAL matrix

REAL initial guess vector

REAL right hand side vector

max_it INTEGER maximum number of iterations

tol

REAL error tolerance

%
% output x
%

error

iter

flag

REAL solution vector


REAL error norm
INTEGER number of iterations performed
INTEGER: 0 = solution found to tolerance

1 = no convergence given max_it

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

I can do all things in Christ who strengthens me

Page 64

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]
error = norm( x - x_1 ) / norm( x );

Julito B. Anora Jr. MEEE-CSO

% compute error

if ( error <= tol ), break, end

% check convergence

end
if ( error > tol ) flag = 1; end

% no convergence

% END jacobi.m

SAMPLE MATLAB RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 65

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Gauss-Siedel Method for Solving Linear Systems AX=B


The Gauss-Seidel method is a technique for solving the n equations of the linear system of
equations one at a time in sequence, and uses previously computed results as soon as they are
available,

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

where the matrices ,


, and
represent the diagonal, strictly lower triangular, and strictly
upper triangular parts of , respectively. The Gauss-Seidel method is applicable to strictly
diagonally dominant, or symmetric positive definite matrices .

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);

I can do all things in Christ who strengthens me

Page 66

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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]

I can do all things in Christ who strengthens me

Page 67

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE MATLAB RUN / SIMULATION

I can do all things in Christ who strengthens me

Page 68

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 4: Interpolation and Polynomial


Approximation

Taylor Polynomial Approximation

Lagrange Approximation

I can do all things in Christ who strengthens me

Page 69

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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 ) ...( )

( 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

Evaluating P(x) at x0 and x1 yields

Px0 y0 y1 y0 0 y0 and Px1 y0 y1 y0 1 y1

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,

I can do all things in Christ who strengthens me

Page 70

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 71

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SOURCE CODES


clc; clear all; format short;
disp('Lagrange Approximation')
n=input('Enter the degree of approximation:');
a=input('enter the left endpoint:');
b=input('Enter the right endpoint:');
f=input('Enter the string expression of f(x):','s');
h=(b-a)/n;
X=a:h:b;
x=X;
Y=eval(f);
n1=length(X);
n=n1-1;
L=zeros(n1,n1);
for k=1:n+1,
V=1;
for j=1:n+1
if k ~=j,
V=conv(V,poly(X(j)))/(X(k)-X(j));
end
end
L(k,:)=V;
end
C=Y*L;
h=(b-a)/150;
X1=a:h:b;
Y1=polyval(C,X1);
x=X1;
Y2=eval(f);
hold on;
grid on;
plot(X1,Y1,'r','linewidth',2.5);
plot(X1,Y2,'g','linewidth',2.5);

I can do all things in Christ who strengthens me

Page 72

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

I can do all things in Christ who strengthens me

Page 73

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

I can do all things in Christ who strengthens me

Page 74

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

I can do all things in Christ who strengthens me

Page 75

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 5: Curve Fitting

Least Squares Line

Least Squares Polynomial

Non Linear Curve Fitting

Fourier Series and Trigonometric


Polynomials

I can do all things in Christ who strengthens me

Page 76

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

LEAST SQUARE LINES


This lesson introduce the first method of curve fitting, the method is called as Least Squares
Line. Least Squares Line method determines the equation of the line best fit for scattered
data points and that line will be the solution of the given function.

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

where ek is the measurement error (also called deviations or residuals)

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

I can do all things in Christ who strengthens me

1/ 2

Page 77

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 78

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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'];

I can do all things in Christ who strengthens me

Page 79

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

I can do all things in Christ who strengthens me

Page 80

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

I can do all things in Christ who strengthens me

Page 81

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

LEAST SQUARE POLYNOMIALS


The least Square Polynomials algorithm is used to fit N points of data to an equation of a curve
whose degree is M. Suppose N data points( xi,yi) are given, the equation for the straight line is
y=c1+c2x, where c1 and c2 are unknown. The equation of the line can be determined using the
normal equations used in the least square line algorithm, If the equation deals with M( the
degree of the curve) greater than 1 then the linear equation can be generalized as:

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.

I can do all things in Christ who strengthens me

Page 82

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

MATLAB SOURCE CODE


clc; clear all; format short;
disp('Least Squares Polynomial')
X= input ('Enter the abscissas for th epoints in X:');
Y= input ('Enter the ordinates for the points in Y:');
m=input('Enter the degree of the least squares polynomial:');
n=length(X);
B=zeros(1:m+1);
F=zeros(n,m+1);
for k=1:m+1,
F(:,k)=X'.^(k-1);

I can do all things in Christ who strengthens me

Page 83

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)]';

I can do all things in Christ who strengthens me

Page 84

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]
Mx4='x(k)

y(k)

P(x(k))

Julito B. Anora Jr. MEEE-CSO

error';

disp(''),disp(Mx4),disp(points2)

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 85

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

NON LINEAR CURVE FITTING


Simple linear curve fitting are applied to functions that are linear in the parameters even when
they are nonlinear in variables. A given set of data points may also fit into a nonlinear curve such
as:

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.

I can do all things in Christ who strengthens me

Page 86

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 87

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SOURCE CODE


clc; clear all; format short;
disp('Nonlinear Curve Fitting')
X=input('Enter the abscissas for the points in X:');
Y=input('Enter the ordinaes for the points in Y:');
points=[X;Y];
disp('The given x-y points:'),...
disp('

y'),disp(points')

Mx1='

Available Curves are:';

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)';

(5) y=A ln(x)+B (6) y=C exp(Ax)';


(8) y=1/(Ax+B)^2';

Mx5='(9) y=C e exp(-Dx) (10) y=L/(1+Cexp(Ax))';


disp(''),disp(Mx1),disp(''), disp(Mx2),disp('')
disp(Mx3), disp(''),disp(Mx4),disp(''),disp(Mx5)
ct=input('Enter the curve type <1-10>');
if length (ct)==0,ct=2;end
Clinst=crvfit(X,Y,ct);
a=min(Y)-0.2;
b=max(X)+0.2;
c=min(Y)-1.0;
d=max(Y)+1.0;
h=(b-a)/200;
Xs=a:h:b;
Ys=crvfun(Clist,ct,Xs);
figure(2)
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');
title(crvnam(Clist,ct));

I can do all things in Christ who strengthens me

Page 88

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)

%% Matlab functions of the curve fitting


function CList=crvfit(x,y,ct)
if length(Ct)==0,ct=2;end
L=0;
if ct==0,
disp('

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

I can do all things in Christ who strengthens me

Page 89

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

if ct==8, X=x; Y=sqrt(y).^(-1); end


if ct==9, X=x; Y=log(L.*y.^(-1)-1); end
if ct==10, X=x; Y=log(L.*y.^(-1)-1); end
AB=polyfit(X,Y,1);
A=AB(1);
B=AB(2);
C=0;D=0;
if ct==2, C=-1/A; D=-B/A; end
if ct==6, C=exp(B); end
if ct==7, C=exp(B); D=-A;end
if ct==10, C=exp(B); end
Clist=[A B CD L];
%% Matlab Funcitons of the CRVFUN
A=Clist(1);
B=Clist(2);
C=Clsit(3);
D=Clist(4);
L=CLsit(5);
if ct==1,z=A./x+B;end
if ct==2,z= D./(x+C); end
if ct==3, z=(A.*x+B).^(-1); end
if ct==4, z= x./(A+B.*x);end
if ct==5, z= A*log(x)+Bl end
if ct==6, z=C*exp(A.*x); end
if ct==7, z=C.*x.^A; end
if ct==8, z=(A.*x+B).^(-2); end
if ct==9, z=C.*x.*exp(-D.*x); end
if ct==10, z=L./(1+C.*exp(A.*x)); end
end
%% Program source code for CRVNAM.M
function z=crvnam(Clist,ct)
A=Clist(1);
B=Clist(2);

I can do all things in Christ who strengthens me

Page 90

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

I can do all things in Christ who strengthens me

Page 91

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

FOURIER SERIES AND TRIGONOMETRIC EXPANSION

I can do all things in Christ who strengthens me

Page 92

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 6: Numerical Differentiation

Central Difference Formulas

Forward and Backward Difference


Formulas

I can do all things in Christ who strengthens me

Page 93

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

CENTRAL DIFFERENCE FORMULAS


Consider a function f(x) tabulated for equally spaced points x0, x1, x2, . . ., xn with step length hIn
this methodology, the central difference formulas simulate the Laplace Equation. In deriving the
formula for Central Difference Method, the following steps are considered.

For the first derivative:

where

To find the second derivative, first look at the first derivatives at the half intervals (x0+h/2) and (x0h/2):
and

The slope between these two points then is:


.

Continuing in this fashion, the first and second derivatives correct to fourth order in h are:

I can do all things in Christ who strengthens me

Page 94

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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);

%Number of steps in space(x)


%Number of steps in space(y)
%Width of space step(x)
%Width of space step(y)
%Range of x(0,2) and specifying the grid points
%Range of y(0,2) and specifying the grid points
%x=0 Dirichlet B.C
%x=L Dirichlet B.C
%y=0 Dirichlet B.C
%y=L Dirichlet B.C
%x=0 Neumann B.C (du/dn=UnW)
%x=L Neumann B.C (du/dn=UnE)
%y=0 Neumann B.C (du/dn=UnS)
%y=L Neumann B.C (du/dn=UnN)
%Pre-allocating u

%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

I can do all things in Christ who strengthens me

Page 95

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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')

I can do all things in Christ who strengthens me

Page 96

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

FORWARD DIFFERENCE METHOD FOR THE HEAT EQUATION

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')

a=input('Enter the width of the interval [0 a]:');

I can do all things in Christ who strengthens me

Page 97

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

b=input('Enter the width of the interval [0 b]:');


c=input('Enter the constant in the heat equation:');
n=input('Enter the number of grid points over [0 a]:');
m=input('Enter the number of grid points over[o b]:');
h=a/(n-1); k=b/(m-1);
r=c^2*k/h^2;s=1-2*r;
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
for j=2:m,
for i=2:(n-1)
U(i,j)=s*U(i,j-1)+r*(U(i-1,j-1)+U(i+1,j-1));
end
end
h=a/(m-1);k=b(n-1);
[X,T]=meshgrid(0:h:a,0:k:b);
surf(T,X,U);
xlabel('t'); ylabel('x');
zlabel('u'); view([1 1 1]);
Mx1='The forward difference solution to the heat equation.';
title(Mx1);
W=U'; points=W(:,2:n-1);
Mx2='The forward difference method is stable becasue r<=1/2.';
disp(Mx1),disp(''), disp(points), disp(Mx2)

I can do all things in Christ who strengthens me

Page 98

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 99

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE MATLAB RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 100

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 7: Numerical Integration

Composite Trapezoidal Rule

Composite Simpson's Rule

I can do all things in Christ who strengthens me

Page 101

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Composite Trapezoidal Rule


The Composite Trapezoidal Rule is a formula that gives a numerical approximation to the
value of a definite integral. Its name is derived from the fact that trapezoids are used to
approximate the area of the function. This rule is one of the Newton-Cotes quadrature formulas
which integrates the first degree polynomial y = P 1(x) over the interval [a, b], subdivided into M
intervals, with the formula

(, ) = (() + ()) + 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

I can do all things in Christ who strengthens me

Page 102

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Corollary (Trapezoidal Rule: Remainder term) Suppose that


into m subintervals

of width

Julito B. Anora Jr. MEEE-CSO

is subdivided

. The composite trapezoidal rule

is an numerical approximation to the integral, and


.
Furthermore, if
term

, then there exists a value c with a < c < b so that the error
has the form

.
This is expressed using the "big

" notation

Remark. When the step size is reduced by a factor of


reduced by approximately

.
the error term

should be

ALGORITHM FLOW
1.
2.
3.
4.
5.

At runtime, prompt the user to enter the function to be integrated in terms of x.


Prompt the user to enter limits and the number of subintervals.
Calculate step size. Initialize the definite integral T = 0.
Convert the function entered as string into a Matlab expression.
For M 1 number of times, evaluate the function at x = lower limit + step size. Store result
as T.
6. Calculate the final value of definite integral T using trapezoidal rule.
7. Display result and end the program.

I can do all things in Christ who strengthens me

Page 103

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 104

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SOURCE CODE


syms f(x)
clc; clear all; format long;
disp('COMPOSITE TRAPEZOIDAL RULE')
disp('the function y=f(x)=1+exp(-x).*sin(4*x)')
disp('will be integrated over th einterval [a,b]')
a=input('Enter the left end point of [a,b]:');
b=input('Enter the right end point of [a, b]:');
m=input('Enter the number of subintervals:');
h=(b-a)/m;s=0;
for k=1:(m-1),
x=a+h*k;
s=s+f(x);
end
s=h*(f(a)+f(b))/2+h*s;
Mx1='the trapezoidal rule applied with m=';
Mx2=[Mx1, num2str(m),'subintervals is:'];
c=0;d=2;X=a:h:b; Y=f(X);
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis([a b c d]); axis(axis);
hold on; plot(X,Y,'g');
xlabel('x');ylabel('y'); title('The Trapezoidal Rule.');
if m<=50,
n=1; h=(b-a)/m/n;
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];

I can do all things in Christ who strengthens me

Page 105

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)

I can do all things in Christ who strengthens me

Page 106

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 107

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Composite Simpsons Rule


Simpsons Rule is a better approximation for the definite integral of a function. It works the
similar way as the trapezoidal rule except that the integrand is approximated to be a quadratic
rather than a straight line within each subinterval. This is also one of the Newton-Cotes
quadrature formula which integrates the 2nd degree polynomial y = P2(x) over the interval [a, b].
The formula is expressed as

(, ) = (() + ()) +

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

I can do all things in Christ who strengthens me

Page 108

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

S f , h

h
f a f b 2h
3
3

M 1

k 1

f x2 k

Julito B. Anora Jr. MEEE-CSO

4h
3

M 1

f x
k 1

2 k 1

This is an approximation to the integral of f(x) over [a,b] and we write

x dx

S f , h

Corollary (Simpson's Rule: Remainder term) Suppose that


subintervals

of width

is subdivided into

. The composite Simpson's rule


.

is an numerical approximation to the integral, and


.
Furthermore, if
term
has the form

, then there exists a value

with

so that the error

.
This is expressed using the "big

" notation

Remark. When the step size is reduced by a factor of


be reduced by approximately

the remainder term

should

I can do all things in Christ who strengthens me

Page 109

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 110

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

ALGORITHM FLOW
1.
2.
3.
4.
5.

At runtime, prompt the user to enter the function to be integrated in terms of x.


Prompt the user to enter limits and the number of subintervals.
Calculate step size. Initialize the subtotals T1 and T2 to zero.
Convert the function entered as string into a Matlab expression.
For M number of times, evaluate the function at x = lower limit + twice of step size - 1.
Store result as T1.
6. For M 1 number of times, evaluate the function at x = lower limit + twice of step size.
Store result as T2.
7. Calculate the final value of definite integral using Simpsons rule. Store as T2.
8. Display result and end the program.

MATLAB SOURCE CODE FILE


syms f(x)
clc; clear all; format long;
disp('COMPOSITE TRAPEZOIDAL RULE')
disp('the function y=f(x)=1+exp(-x).*sin(4*x)')
disp('will be integrated over th einterval [a,b]')
a=input('Enter the left end point of [a,b]:');
b=input('Enter the right end point of [a, b]:');
m=input('Enter the number of subintervals:');
h=(b-a)/m;s=0;
for k=1:(m-1),
x=a+h*k;
s=s+f(x);
end
s=h*(f(a)+f(b))/2+h*s;
Mx1='the trapezoidal rule applied with m=';
Mx2=[Mx1, num2str(m),'subintervals is:'];
c=0;d=2;X=a:h:b; Y=f(X);
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis([a b c d]); axis(axis);
hold on; plot(X,Y,'g');
xlabel('x');ylabel('y'); title('The Trapezoidal Rule.');
if m<=50,
n=1; h=(b-a)/m/n;

I can do all things in Christ who strengthens me

Page 111

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)

I can do all things in Christ who strengthens me

Page 112

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE RUN/MATLAB

I can do all things in Christ who strengthens me

Page 113

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 9, Part 1: Solution of Differential


Equations

Euler's Method

Heun's Method
Taylor Method of Order 4
Runge-Kutta Method of Order 4

I can do all things in Christ who strengthens me

Page 114

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

EULERS METHOD

I can do all things in Christ who strengthens me

Page 115

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 116

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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')

I can do all things in Christ who strengthens me

Page 117

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SAMPLE RUN

I can do all things in Christ who strengthens me

Page 118

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

The predictor step obtains a preliminary estimate,


, for y(xi+1) based on the simple Euler
method, and then the corrector step uses this value in place of the actual value of y(xi+1) to
obtain a better estimate of the slope at the end of the step. In evaluating the mean value of
the slope over the step, the use of the average based on the two endpoint values is
equivalent to the use of the trapezoid integration rule, so that the error in this average value
simply due to the integration involved in the averaging process is of order h3. Since the use of
a step size of h over some interval in x requires a number of steps proportional to 1/h, the
global error in this case will be of order h2. Thus for a given interval for x, the global error, or
accumulated error at the end of that integration interval will be of order h2, so that halving h
should improve the global accuracy.

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.

I can do all things in Christ who strengthens me

Page 119

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]
3.
4.
5.
6.

Julito B. Anora Jr. MEEE-CSO

Calculate step size.


Calculate fk1, fk2 and y at t = left endpoint. Display results.
Repeat step 4 for M number of times but increment t by the step size for each repetition.
End the program.

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 120

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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')

I can do all things in Christ who strengthens me

Page 121

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SAMPLE RUN

I can do all things in Christ who strengthens me

Page 122

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

TAYLOR METHOD OF ORDER 4

Theorem 18.2 (Precision of Taylors Method of Order N).


Assume that y(t) is the solution to the IVP.
If {y } is the sequence of approximations generated by Taylors method
k

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

I can do all things in Christ who strengthens me

Page 123

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 124

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)';

I can do all things in Christ who strengthens me

Page 125

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

disp(''),disp(Mx1)
disp(''),disp(Mx2),disp(points')

MATLAB SAMPLE RUN

I can do all things in Christ who strengthens me

Page 126

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Runge-Kutta Method (RK4)


This method has several versions. The fourth-order Runge-Kutta Method (RK4), which algorithm
will be developed in latter part of this section, is one of the most widely used methods for solving
ordinary differential equations. This version is said to be the most practical (solution/algorithm will
not be very long), yet significantly accurate. If more precise results are desired, the number of
steps (number of iterations) and the step size (h) can be adjusted. The equations used are shown
below.

+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.

I can do all things in Christ who strengthens me

Page 127

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 128

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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;

I can do all things in Christ who strengthens me

Page 129

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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<=32,
plot(T,Y,'or');
end;
xlabel('t');ylabel('y');
title('Runge-Kutta solution to y=f(t,y).');
grid; hold off;
Mx1='Range-Kutta solutiion to y=f(t,y),';
Mx2='

t(k)

y(k)';

disp(''), disp(Mx1)
disp(''),disp(Mx2), disp(points')

I can do all things in Christ who strengthens me

Page 130

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SAMPLE RUN

I can do all things in Christ who strengthens me

Page 131

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 9, Part 2: Solution of Differential


Equations

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

I can do all things in Christ who strengthens me

Page 132

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

1. Prompt the user to enter the function y in terms of y and t.


2. Prompt the user to enter the initial and final time, the initial value for y and the intended
number of subintervals.
3. 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.
4. Calculate for intermediate results using the RK4 method.
5. Extract the 1st, 3rd, 5th and 7th elements of Ys and Ts and store as matrices Y and T,
respectively.
6. Calculate for the predictor and the corrector. Repeat as needed.
7. Display results and end the program.

I can do all things in Christ who strengthens me

Page 133

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 134

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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');

I can do all things in Christ who strengthens me

Page 135

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)

MATLAB SAMPLE RUN

I can do all things in Christ who strengthens me

Page 136

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

( ).

This modified value can be used to form a new equation

+ 1 = 1 + ( 1 + 4 + ( + 1 , + 1 ).
3

I can do all things in Christ who strengthens me

Page 137

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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.

I can do all things in Christ who strengthens me

Page 138

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 139

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB CODES

clc; clear all; format long;


disp('Milne Simpson 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 end point of [a b]:');
ya=input('Enter the initial values 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);
a=T(1);pold=0;yold=0;
for k=4:n-1,
pnew=Y(k-3)+4*h*(2*f1-f2+2*f3)/3;
pmod=pnew+28*(yold-pold)/29;
T(k+1)=a+h*k; f4=f(T(k+1),pmod);
pold-pnew;yold=Y(k+1);f1=f2;
f2=f3;f3=f(T(k+1),Y(k+1));
end

I can do all things in Christ who strengthens me

Page 140

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)';

Mx3='The Milne-Simpson Method is stable';


Mx4['for n=',num2str(n),' and h='];
Mx5=[Mx4, num2str(h),'because'];
Mx6='

h<0.45/|f(t,y)|';

disp(''), disp(Mx1),disp(''), disp(Mx2), disp(points')


disp(Mx3),disp(Mx5),disp(''), disp(Mx6), disp(Mx7)

I can do all things in Christ who strengthens me

Page 141

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE MATLAB RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 142

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Systems of Ordinary Differential Equations/Predator-Prey Model


The LotkaVolterra equations, also known as the predatorprey equations, are a pair of firstorder, non-linear, differential equations frequently used to describe the dynamics of biological
systems in which two species interact, one as a predator and the other as prey. The populations
change through time according to the pair of equations:

where

x is the number of prey (for example, rabbits);

y is the number of some predator (for example, foxes);

and

represent the growth rates of the two populations over time;

t represents time; and

, , , are positive real parameters describing the interaction of the two species.

The LotkaVolterra system of equations is an example of a Kolmogorov model which is a more


general framework that can model the dynamics of ecological systems with predator-prey
interactions, competition, disease, and mutualism.

Physical meanings of the equations


The LotkaVolterra model makes a number of assumptions about the environment and evolution
of the predator and prey populations:
1. The prey population finds ample food at all times.
2. The food supply of the predator population depends entirely on the size of the prey
population.
3. The rate of change of population is proportional to its size.
4. During the process, the environment does not change in favour of one species and
genetic adaptation is inconsequential.
5. Predators have limitless appetite.
As differential equations are used, the solution is deterministic and continuous. This, in turn,
implies that the generations of both the predator and prey are continually overlapping

I can do all things in Christ who strengthens me

Page 143

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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,

represents the growth of the predator population. (Note the

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

emigration; it leads to an exponential decay in the absence of prey.


Hence the equation expresses the change in the predator population as growth fueled
by the food supply, minus natural death.

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.

I can do all things in Christ who strengthens me

Page 144

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

I can do all things in Christ who strengthens me

Page 145

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

% 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

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

% 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

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE MATLAB RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 149

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

As can be seen in the figure, the graph of the predators increases as the number of population
of the prey decreases.

I can do all things in Christ who strengthens me

Page 150

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Higher Order Differential Equations/Pendulum Model

The figure at the right shows an idealized


pendulum, with a "massless" string or rod
of length L and a bob of mass m. The open circle
shows the rest position of the bob. When the bob is
moved from its rest position and let go, it swings
back and forth. The time it takes the pendulum to
swing from its farthest right position to its farthest left
position and back to its next farthest right position is
the period of the pendulum.
The primary forces acting on the bob are the
gravitational force that makes it move in the first
place and the force exerted by the string to keep it
moving along a circular path. In addition, there
may be a damping force from friction at the pivot
or air resistance or both. We will construct a model
to describe how the angle theta of the pendulum
varies as a function of time t.
Let s(t) be the distance along the arc from the
lowest point to the position of the bob at time t,
with displacement to the right considered positive.
Let theta(t) be the corresponding angle with respect to the vertical. The figure shows tangential
and radial components of gravitational force on the pendulum bob. The radial component is
exactly balanced by the force exerted by the string, so the only relevant force producing the
motion is the tangential component of the gravitational force. For the moment, we ignore the
damping force, if any.
The gravitational force is directed downward and has magnitude mg (mass x acceleration),
where g is the gravitational acceleration constant, 32.17 feet/sec 2 or 9.807 meters/sec2 near sea
level. Thus, the force acting in the tangential direction is -mg sin(theta). (The negative sign is
because this force is in the negative direction when theta is positive and vice versa.) Since this
force is mass x acceleration, it follows that

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:

I can do all things in Christ who strengthens me

Page 151

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

This equation is similar to the damped, unforced spring equation

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.

I can do all things in Christ who strengthens me

Page 152

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB CODES
%
%
%
%
%
%
%
%
%
%
%

AnimatePendulum.m simulates and animates a simple pendulum using both


linear and nonlinear dynamic models. A larger difference is observed
between linear and nonlinear models when maximum angular position is
large. This condition can be achieved by specifying either a large
initial angle or velocity.
After simulation of both the linear and nonlinear models the code steps
through each time step and plots the pendulum as well as its position and
velocity trajectories. These plots are recorded using the getframe
command, and then used to produce a video of the animation using both the
movie2avi and VideoWriter commands.

%% INITIALIZATION:
clear,clc
% system parameters
m = 5;
L = 1;
d = 0.002;
a = 0.3;
b = 0.2;

%
%
%
%
%

pendulum point mass (kg)


pendulum length (m)
pendulum rod diameter (m)
mass width (m) (for animation)
mass height (m) (for animation)

% 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

I can do all things in Christ who strengthens me

Page 153

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

I can do all things in Christ who strengthens me

Page 154

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

figure(fignum); clf; subplot(1,3,1);


subplot('Position',[0.025 0.05 0.30 1.0]); hold on
axis equal; axis([-L L -b L])
% Plot velocity vector
% linear
vv_L = [0 0
0 Vt_L(ti)/(2*L*Vtmax_L)]; vv_L = R_L*R90*vv_L;
vv_L = vv_L + repmat([x1_L y1_L]',1,2);
acolor = [200 255 200]/255;
plot_arrow(vv_L(1,1),vv_L(2,1),vv_L(1,2),vv_L(2,2),'edgecolor',acolor,...
'facecolor',acolor,'color',acolor,'linewidth',2);
% nonlinear
vv = [0 0
0 Vt(ti)/(2*L*Vtmax)]; vv = R*R90*vv;
vv = vv + repmat([x1 y1]',1,2);
plot_arrow(vv(1,1),vv(2,1),vv(1,2),vv(2,2),'edgecolor','g',...
'facecolor','g','color','g','linewidth',2);
% Plot mass shape
% linear
scolor = [150 150 150]/255;
patch(Vrs_L(1,:),Vrs_L(2,:),scolor);
% plot rod
plot([0,x1_L],[L,y1_L],'k-','LineWidth',4,'Color',scolor)
% plot vertical position marker line
plot([-L L],[y1_L y1_L],'r-')
% plot cg
plot(x1_L,y1_L,'o','MarkerFaceColor','w','MarkerSize',10)
% nonlinear
patch(Vrs(1,:),Vrs(2,:),'k');
% plot rod
plot([0,x1],[L,y1],'k-','LineWidth',4)
% plot vertical position marker line
plot([-L L],[y1 y1],'r-')
% plot pivot
plot(0,L,'o','MarkerFaceColor','r','MarkerSize',12)
% plot cg
plot(x1,y1,'o','MarkerFaceColor','w','MarkerSize',10)
% Display time on plot
tc = T(ti);
% current time
text(-0.95*L,0.8*L+0.1,'\fontsize{10}\fontname{Arial Black}elapsed
time:')
text(-0.85*L,0.70*L+0.1,['\fontsize{10}\fontname{Arial Black}' ...
num2str(tc,'%1.1f') ' sec'])
title('\fontsize{12}\fontname{Arial Black}Pendulum Animation')
% Plot position trajectory
subplot(1,3,2);
subplot('Position',[0.40 0.34 0.125 0.42]); hold on
axis([-dt dt -b L])
% obtain time history for position to current time
% linear

I can do all things in Christ who strengthens me

Page 155

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

I can do all things in Christ who strengthens me

Page 156

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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));

I can do all things in Christ who strengthens me

Page 157

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

end
end
if aflag
close(vidObj);
movie2avi(F,moviename,'fps',length(T)/(tf-t0),'compression','none')
end

MATLAB RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 158

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

FINITE DIFFERENCE EQUATION FOR WAVE EQUATION

I can do all things in Christ who strengthens me

Page 159

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB CODES

clc; clear all; format short;


disp('Finite-Difference Solution for the Wave Equation')
disp('

2')

disp('

u (x,t)

disp('

tt

= c

u (x,t)')

xx')

a=input('Enter width of the interval[0 a]:');


b=input('Enter width of the interval [0 b]:');
c=input('ENtr the constant in the wave equation:');
n=input('enter the number of grid points over [0, a]:');
m=input('Enter the number of grid points over [0 b]:');

I can do all things in Christ who strengthens me

Page 160

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)

I can do all things in Christ who strengthens me

Page 161

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 162

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE MATLAB RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 163

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

Chapter 10: Solution of Partial


Differential Equations

Wave Equation
Heat Equation

Crank-Nicholson Method for Heat


Equation
Laplace Equation

I can do all things in Christ who strengthens me

Page 164

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

WAVE EQUATION

The wave equation is an important second-order linear partial differential equation for the
description

of waves

as

they

occur

in

physicssuch

as sound waves, light waves

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

where 2 is the (spatial) Laplacian and c is a fixed constant.


Solutions of this equation describe propagation of disturbances out from the region at a fixed
speed in one or in all spatial directions, as do physical waves from plane or localized sources; the
constant c is identified with the propagation speed of the wave. This equation is linear.
Therefore, the sum of any two solutions is again a solution: in physics this property is called
the superposition principle.
The wave equation alone does not specify a physical solution; a unique solution is usually
obtained by setting a problem with further conditions, such as initial conditions, which prescribe
the amplitude and phase of the wave. Another important class of problems occurs in enclosed
spaces specified by boundary conditions, for which the solutions represent standing waves,
or harmonics, analogous to the harmonics of musical instruments.
The

wave

equations,

and

modifications

of

it,

are

also

found

in elasticity, quantum

mechanics, plasma physics and general relativity.

I can do all things in Christ who strengthens me

Page 165

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

I can do all things in Christ who strengthens me

Page 166

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)

I can do all things in Christ who strengthens me

Page 167

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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 --->>
%
%
%

I can do all things in Christ who strengthens me

Page 168

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

% *************************************** %
%
% 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

I can do all things in Christ who strengthens me

Page 169

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB SIMULATION

I can do all things in Christ who strengthens me

Page 170

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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

More generally in any coordinate system:

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.

I can do all things in Christ who strengthens me

Page 171

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

I can do all things in Christ who strengthens me

Page 172

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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.

function [u,err,x,t] = heat1(t_0,t_f,M,N)


% define the mesh in space
dx = 2*pi/(N-1);
x = 0:dx:2*pi;
x = x';
% define the mesh in time
dt = (t_f-t_0)/M;
t = t_0:dt:t_f;
% define the ratio r
r = dt/dx^2
for i=1:N
u(i,1) = cos(x(i));
end
err(:,1) = u(:,1) - exp(t_0-t(1))*cos(x);
%
%
%
%
%
%
%

for internal points, have


u_new(j) = u_old(j) + r*(u_old(j+1)-2*u_old(j)+u_old(j-1))
for the two end-points, have
u_new(1) = u_old(1) + r*(u_old(2)-2*u_old(1)+u_old(N-1))
u_new(N) = u_old(N) + r*(u_old(2)-2*u_old(N)+u_old(N-1))
clearly the endpoints are redundant: u(1)= u(N) at all times.
kept them around for plotting convenience.

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

I can do all things in Christ who strengthens me

Page 173

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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:

I can do all things in Christ who strengthens me

Page 174

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

For a boundary condition with a depths of 0, 5 10, 15 and 20 m.

I can do all things in Christ who strengthens me

Page 175

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

I can do all things in Christ who strengthens me

Page 176

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

CRANK NICHOLSON METHOD FOR HEAT EQUATION

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')

a=input('enter the width of the interval [0 a]:');


b=input('Enter the width of the interval [0, b:');
c=input('Enter the constant in the heat equation:');
n=input('Enter the number of grid points over [0 a]:');
m= input('Enter the number of grid points over [0 b]:');

I can do all things in Christ who strengthens me

Page 177

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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);

I can do all things in Christ who strengthens me

Page 178

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)

I can do all things in Christ who strengthens me

Page 179

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

PROGRAM FLOW

I can do all things in Christ who strengthens me

Page 180

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE MATLAB RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 181

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

DIRICHLET METHOD FOR LAPLACEs EQUATION

I can do all things in Christ who strengthens me

Page 182

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

MATLAB CODES

clc; clear all; format short;


disp('Dirichlet Mehod for Laplace Equation')
disp('

2')

disp('

u(x,t)

disp('

= c u(x,t)')
xx')

disp(' given boundary conditions over a rectangel')


a=input('Enter width of interval [0 a]:');
b=input('ENter width of interval [0 b]:');
h=input('Enter the step size:');
tol=input('Enter the convergence tolerance:');
max1=input('Enter maximum number of iterations:');
n=fix(a/h)+1; m=fix(b/h)+1;
ave=(a*(f1(0)+f2(0))+b*(f3(0)+f4(0)))/(2*a+2*b);
U=ave*ones(n,m);
for j=1:m,
U(1,j)=f3(h*(j-1));
U(n,j)=f4(h*(j-1));

I can do all things in Christ who strengthens me

Page 183

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

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)

I can do all things in Christ who strengthens me

Page 184

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

SAMPLE MATLAB RUN/SIMULATION

I can do all things in Christ who strengthens me

Page 185

[SIMPLIFIED ENGINEERING APPROACH TO NUMERICAL METHODS:


COMPILATION OF ALGORITHMS USING MATLAB]

Julito B. Anora Jr. MEEE-CSO

I can do all things in Christ who strengthens me

Page 186

You might also like