Modul Kombio 2019 PDF
Modul Kombio 2019 PDF
Practicum Module
BIOMEDICAL COMPUTATION
2019
TABLE OF CONTENT
Contents
Preface: Rules of the Class ______________________________________________________________________ ii
Chapter 1. Introduction to Python: Core Python ______________________________________________ 1
Chapter 2. Finding-Root Method: Bisection _________________________________________________ 12
Chapter 3. Newton-Raphson__________________________________________________________________ 16
Chapter 4. System of Linear Equation: Gauss Elimination _________________________________ 18
Chapter 5. Jacobi & Gauss Seidel Iteration___________________________________________________ 25
Chapter 6. Regression: Linear & Polynomials _______________________________________________ 29
Chapter 7. Interpolation Lagrange ___________________________________________________________ 35
Chapter 8. Numerical Derivative: Finite Difference Approximations ______________________ 37
Chapter 9. Numerical Integration: Trapezoid & Simpson’s Rule___________________________ 39
Chapter 10. Richardson Extrapolation: Derivatives ________________________________________ 45
Chapter 11. Richardson Extrapolation: Integration ________________________________________ 48
Chapter 12. Ordinary Differential Equation: Euler & Heun Method _______________________ 53
Chapter 13. Ordinary Differentia Equation: Runge-Kutta Method_________________________ 58
Chapter 14. Partial Differential Equation ____________________________________________________ 64
i
PREFACE
Requirement
1. The students should hand in the preliminary task that is given in this module before
the practical class starts for every chapter.
2. All the gadgets should be turned off or in silent mode.
Practice
1. Before the practical starts, the student should prepare themselves by reading the
designated module for each chapter.
2. The students should follow the explanation and algorithm in the module and
implement it in the python programming software.
3. The students are prohibited to make noise, inappropriate behavior, going in and out
the class without permission from lecturers or lab assistants.
4. The students may ask to the lecturer and lab assistants about their obstacles to
understand the algorithm or tasks.
Additional Information
1. The students must not change their group/class/time without the permission of
the lecturer.
2. Plagiarism would be taken into account seriously, such as copy-pasting from the
other students, including senior students, putting information without citation and
references, etc.
3. The students should wear polite clothes, no sandals, no T-shirts etc.
4. Another additional information would be announced in the class by the lecturer.
ii
PREFACE
Grading System
o Report
• The completeness of the report (10%)
• Source code (15%)
• Result (15%)
• Discussion and Analysis (25%)
• Final Tasks (35%)
o Final Grade
• Preliminary Task (10%)
• Activity in the Practical Class (20%)
• The Report (40%)
• Final Practical Test (30%)
Report Structure
1. The practical report should contain cover page and main content.
2. The cover page should contain the chapter title and code, student’s name student
number (NIM), the date of the practical class, and the name of the lecturer.
3. The main content should contain literature review, source code, result, discussion
and analysis, final tasks, conclusion, references.
4. Literature review: It contains brief explanation about the basic theory of the
chapter and all information that could be used to explain the result obtained from
the practical class, including the formula, algorithm etc. All of information should
be followed by citations and mentioned the references in the last part of the report.
5. Source code: It contains the source code from python programming (copy-paste)
and keep it in structural way to make it neat.
6. Result: It contains the figure, plot, or screen-capture of the python console with
high-resolution quality.
7. Discussion and analysis: It contains the explanation about the result of the running
source code and their comparison with the literature. It should also explain about
the advantages and disadvantages of the method used in this chapter.
8. Final tasks: Every chapter has several final tasks that should be solved by
iii
PREFACE
9. the students by using the method in each chapter in this module. Each task should
be answered thoroughly with analysis as well.
10. Conclusion: After finishing the main content of the report, the students should tell
the take home message from each chapter, such as the final statement about the
method.
11. References: Only the references that are used in the report are allowed to be put in
this part. It could be a book, scientific paper, proceeding from international
conference, etc. References from untrusted website or blog is not encouraged.
iv
INTRODUCTION TO PYTHON
10.0
All characters after the pound sign (#) will be ignored by the interpreter or denotes the
beginning of a comment. In Python 3 and higher, print ( ) is a command to bring out the
value of variables to the Python Console. While in Python 2.7 (and before) it prints the
arguments with a space in between.
Strings
A string is a sequence of characters that is located inside the single or double quotation
marks. Here is an example:
Biomedical Engineers
1
INTRODUCTION TO PYTHON
Biomedical Engineers
Strings also could be concatenated with the plus operator (+) and sliced using (:) to extract
a portion of character.
Biomedical
Lists
A list is a sequence of arbitrary objects separated by commas and enclosed in brackets.
Lists are mutable, so that its elements and length can be changed or modified. Here is the
example that can be performed on lists:
Since list is a mutable object, if we want to create a new reference to the previous variable,
such as b = a, any changes made to b will be reflected in a. However, an independent copy
2
INTRODUCTION TO PYTHON
of the original list of a can be create use the statement c = a [:]. Thus, any changes made to
c will not affected in a.
A nested list also can be implemented in matrices. Here is the example of 2 x 3 matrix d
and how to print a certain element in the form of a list:
Operators
Python support some operators, such as arithmetic operators, comparison operators:
Augmented
Arithmetic Operators Comparison Operators
Assignment Operators
+ Addition a +=b a=a+b < Less than
- Subtraction a -= b a=a-b > Greater than
* Multiplication a *= b a=a*b <= Less than or equal to
/ Division a /= b a=a/b >= Greater than or equal to
** Exponentiation a **= b a = a ** b == Equal to
% Modular division a %= b a=a%b != Not equal to
3
INTRODUCTION TO PYTHON
Some of these operators are also used for strings and sequences, as shown below:
Conditionals
If statements are used to define a condition. If the condition returns true, then a block f
statement will be execute. If the condition return false, the block is skipped or continued
to next condition (else if). Here is the example of the function if_statement that illustrated
a conditional statement:
a = -2.5
print ('a is '+ if_statement (a))
a is negative
4
INTRODUCTION TO PYTHON
Loops
Looping can be execute using the While construct or the For construct. The while construct
will execute a block of statements if the condition is true, then evaluate it. If it is still true,
the block continuous to execute until the condition becomes false. The example of the while
construct is shown as:
Target = 5
n=0
a=[] # Create empty list
while n < Target :
a.append ((2*n)/10) # Append element to list
n=n+1
print (a)
The for statement requires a target and a sequence over which the target loops. The
example of the for construct is shown as:
Target = 5
a=[] # Create empty list
for n in range (1, Target) :
a.append ((2*n)/10) # Append element to list
print (a)
The break statement can terminate loops if the previous condition has been fulfilled, so the
next condition will not be execute. Here is the example of break in looping:
5
INTRODUCTION TO PYTHON
The result of running the program twice using the different name:
Type a name: ‘Dona’
Dona is number 4 on the list
Loops can also skip a portion of the statements in an iterative loop by the continue
statement. The continue statement will immediately returns to the beginning of the loop
without executing the statements afterwards.
6
INTRODUCTION TO PYTHON
Modules contain of useful functions which can be loaded into a program by the statement:
Python provided various modules contain function and methods for various task, including
arithmetic operations and matrices. Some of modules that frequently used in this
practicum are math module, NumPy module and SciPy module. Beside these module, there
are a lot of other module that provided by Python. The contents of a module can be printed
by write dir (module_name). Here is the example of how to obtain a list of Math and NumPy
module:
[’__doc__’, ’__name__’, ’acos’, ’asin’, ’atan’, ’atan2’, ’ceil’, ’cos’, ’cosh’, ’e’, ’exp’, ’fabs’, ’floor’,
’fmod’, ’frexp’, ’hypot’, ’ldexp’, ’log’, ’log10’, ’modf’, ’pi’, ’pow’, ’sin’, ’sinh’, ’sqrt’, ’tan’, ’tanh’]
Creating an Array
Array is a function that located in NumPy module. To create an array we need to import the
array function from NumPy module. Here is the statement:
7
INTRODUCTION TO PYTHON
[[1 2 3]
[1 2 3]]
Other way to create an array are using zeros (dim1, dim2) that creates a dim1 x dim2 array
filled with zeroes and ones (dim1, dim2) which fills the array with ones.
Operating on Array
Arithmetic operators work differently on arrays than they do on list or list. In arrays, the
operation is applied to each element in the array. Here are the illustration:
8
INTRODUCTION TO PYTHON
Array Function
NumPy provides numerous function that perform in array operations. The examples are
shown as below:
NumPy also allow us to compute array product from its function, such as Dot product, Inner
product, and outer product. Here are the illustration:
>>> from numpy import *
>>> x = array ( [ 2, 3 ] )
>>> y = array ( [ 5, 2 ] )
>>> A = array ( [ [ 1, 2 ] , [ 3, 4 ] ] )
9
INTRODUCTION TO PYTHON
>>> B = array ( [ [ 1, 1 ] , [ 2, 2 ] ] )
>>> print ( "dot (x , y) = \n ", dot (x , y) ) # Dot product {x} . {y}
dot (x , y) =
16
>>> print ( "dot (A , x) = \n ", dot (A , x)) # Dot product [A] . {x}
dot (A , x) =
[ 8 18 ]
>>> print ( "dot (A , B) = \n ", dot (A , B) ) # Dot product [A] . [B]
dot (A , B) =
[[ 5 5 ]
[ 11 11 ] ]
>>> print ( "inner (x , y) = \n ", inner (x , y) ) # Inner product {x} . {y}
inner (x , y) =
16
>>> print ( "inner (A , x) = \n ", inner (A , x)) # Inner product [A] . {x}
inner (A , x) =
[ 8 18 ]
>>> print ( "inner (A , B) = \n ", inner (A , B) ) # Inner product [A] . [B]
inner (A , B) =
[[ 3 6]
[ 7 14 ] ]
>>> print ( "outer (x , y) = \n ", outer (x , y) ) # Outer product {x} . {y}
outer (x , y) =
[ [ 10 4]
[ 15 6 ] ]
>>> print ( "outer (A , x) = \n ", outer (A , x)) # Outer product [A] . {x}
outer (A , x) =
[[2 3]
[4 6]
[6 9]
[ 8 12 ] ]
10
INTRODUCTION TO PYTHON
>>> print ( " outer (A , B) = \n ", outer (A , B) ) # Outer product [A] . [B]
outer (A , B) =
[[1 1 2 2]
[2 2 4 4]
[3 3 6 6]
[4 4 8 8] ]
NumPy comes with a linear algebra module called linalg that contains routine tasks such
as matrix inversion and solution of simultaneous equations. Here is the example:
Task
1. Display the even number from zero to 100 and calculate the sum of it !
References
1. Kiusalaas J, 2013, ”Numerical Methods in Engineering with Python 3. Vol. 51”,
Cambridge University Press, New York.
11
FINDING-ROOT METHOD: Bisection & Regula-Falsi
Preliminary Task
If there is a function as follows:
Please Find the roots in the given intervals by using Bisection and Regula-Falsi method.
Literature Review
Root-finding problem is one of the most basic problems in numerical analysis. If we have a
function f(x), the process to find the roots of this function involves finding the value of x
while f(x) = 0. When the function equals zero, the x value for that function is its root, so-
called zero of the function f(x). One of the method to find this root is bisection method or
interval halving method or binary search method or dichotomy method.
If the function equals zero, x is the root of the function. A root of the equation f(x) = 0 is
also called a zero of the function f(x). The Bisection method also called the interval halving
method, the binary search method, or the dichotomy method. This method is based on the
Bolzano’s theorem for continuous functions. Theorem (Bolzano): If a function f(x) is
continuous on an interval [a, b] and f(a)·f(b) < 0, then a value c ∈(a, b) exist for which f(c)
= 0.
This method is started by choosing two numbers, make interval between them by taking
half of them, and make it smaller and smaller. Those two number should have result of the
function with opposite sign. As example, there are two numbers, a and b. f(a) and f(b)
should have opposite sign so that f(a).f(b)<0. Then, the median number r is taken from
(a+b)/2. If f(r) should be tested by using one of f(a) or f(b). If the result of f(r).f(a)<0 then
they would be used as the new interval. This process is repeated until the interval reach 0
or near to 0 according to the error. This method is illustrated in Figure 1.
12
FINDING-ROOT METHOD: Bisection & Regula-Falsi
The other method is Regula-Falsi. This method starts with defining initial interval [a, b] so
that f(a) . f(b) < 0. In this method, the r point is not exactly in the middle. The r point is
slightly close to a or b. The r point is calculated by using equation below:
𝑓(𝑎)(𝑎 − 𝑏)
𝑟=𝑎−
𝑓(𝑎) − 𝑓(𝑏)
After that, the new interval is determined by using the same method as bisection method
which is by checking whether f(a) . f(r) < 0 or f(b) . f(r) < 0. If f(a) . f(r) < 0 then the new
interval is a and r, if not, then the new interval is r and b. This process is repeated until the
closest value is found by having the smallest error.
13
FINDING-ROOT METHOD: Bisection & Regula-Falsi
Algorithm
1. Bisection method
a. Define the function f(x), interval [a, b], max iteration (maxit), and max error
(maxer).
b. Define n=1
c. Repeat step d-h if n ≤ maxit
d. r = (a+b)/2
e. Print n, r, and f(r)
f. If |f(r) ≤ maxer| or |a-b| ≤ maxer, then the iteration stop and exit.
g. If f(r)*f(a) < 0 , then b=r, otherwise a = r.
h. n = n+1
i. Print “algorithm fails: no convergence” and exit.
2. Regula-Falsi method
a. Define the function f(x), interval [a, b], max iteration (maxit), and max error
(maxer).
b. Define n = 1
c. Repeat step d-h if n ≤ maxit
d. r = a - (f(a)(a - b))/(f(a) - f(b))
e. Print n, r, and f(r)
f. If |f(r) ≤ maxer| or |a-b| ≤ maxer, then the iteration stop and exit.
g. If f(r)*f(a) < 0, then b=r, otherwise a = r.
h. n = n+1
i. Print “algorithm fails: no convergence” and exit.
Tasks
1. Find the root of equation system in following problem!
Packed bed column. A column packed with spherical particles provides high surface
area geometry that is useful in isolating specific protein(s) or other biological molecules
from a cell lysate mixture. The Ergun equation relates the pressure drop through a
packed bed of spheres to various fluid and geometric parameters of the bed:
14
FINDING-ROOT METHOD: Bisection & Regula-Falsi
∆𝒑 (𝟏 − 𝜺)𝟐 𝝁𝒖 (𝟏 − 𝜺)𝝆𝒖𝟐
= 𝟏𝟓𝟎 + 𝟏. 𝟕𝟓
𝒍 𝒅𝟐𝒑 𝒅𝒑
Where Δp is the pressure drop, l is the length of the column, ε is the porosity, μ is the
fluid viscosity, u is the fluid velocity, dp is the diameter of the spherical particles, and ρ
is the fluid density. For a 20 cm column, packed with 1 mm spheres and perfused with
a buffer of equal viscosity and density to water (μ = 0.01 P, ρ = 1 g/cm3). By using the
bisection and Regula-Falsi method, determine the column porosity if the pressure drop
for a fluid flowing is 810.5 dyn/cm2 with velocity u = 0.75 cm/s. Make sure that you use
consistent units throughout your calculation.
Using a starting interval of 0.1 < ε < 0.9, report the number of iterations necessary to
determine the porosity to within 0.01 (tolerance).
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
15
FINDING-ROOT METHOD: Newton-Raphson
Preliminary Task
If there is a function as follows:
𝑓(𝑥) = 𝑥 3 + 𝑥 2 + 𝑥 + 1
Please find the roots in the given intervals by using Newton-Raphson method.
Literature Review
Root-finding problem is one of the most basic problems in numerical analysis. This method
uses an approximation method by using one initial point and derive it by taking the slope
or gradient on that point. The approximation point is defined as follows:
𝑓(𝑥 )
𝑥𝑛+1 = 𝑥𝑛 − 𝑓′(𝑥𝑛 ) (3.1)
𝑛
Algorithm
1. Define the function f(x) and the first derivative f’(x)
4. Calculate f(x0)
𝑓(𝑥 )
5. Calculate 𝑥𝑛+1 = 𝑥𝑛 − 𝑓′(𝑥𝑛 )
𝑛
6. Calculate f(xn+1)
7. If |f(xn)| < tolerance, then the process is stopped and the root is xn, otherwise repeat step
5.
16
FINDING-ROOT METHOD: Newton-Raphson
8. If |x2 – x1| < tolerance or |f(x0)| < tolerance, then the process is stopped and the root is
x0, otherwise repeat step 5.
Task
1. Find the root of equation system in following problem!
Where the SOS is expressed in unit of m/s. The SOS for one research subject is measured
to be 3850 m/s. Use the Newton-Raphson to find the root of the above equation! Take
Y=45 years as initial guess!
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
17
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION
Preliminary Task
If there is a function as follows:
𝑎+𝑏+𝑐 =6
𝑎 + 2𝑏 − 𝑐 = 2
2𝑎 + 𝑏 + 2𝑐 = 10
Please Find a, b, and c!
Literature Review
The solution of linear equation system is by solving each variable. Several method has been
used to solve linear equation system, such as:
1. Gauss Elimination
2. Invers of Matrix
There are three operations that could be performed towards linear equation systems
without changing the real solution, which are:
All of them could be applied in the full matrix and is called Elementary Row Operation
(ERO). By using ERO, the full matrix is changed to a matrix based on linear equation system
that is easier to solve. A matrix that has this characteristic is called Echelon Matrix. A
matrix is called an echelon matrix if it fulfills two characteristics, which are:
18
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION
1. If there is a row that all of its element are zero, then that row should be placed under
the row that has non-zero elements.
2. In the row that has non-zero element, that non-zero element should be placed on the
right side of the non-zero element of the previous row (this non-zero element is called
the principle element)
The method to solve linear equation system by using Gauss elimination method could be
done as follow.
2. Changing the full matrix to echelon matrix with several elementary row oerations
z = b3’
19
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION
Algorithm
1. Define matrix a, matrix b and the order of the matrix (n).
Do for k = 1 to n
Do for i = k+1 to n
Do for j = k+1 to n
3. Forward elimination:
Do for k = 1 to n
Do for i = k+1 to n
bi = bi – aik aik
4. Backward solve:
Do for i = n down to 1 do
s = bi
Do for j = i + 1 to n
s = s − aij xj
xi = s/ aii
Task
Find the solution for linear equation system in following problem!
20
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION
The material balance diagram for naphthalene epoxide (NO) is shown in Figure 4.1. Since
we are dealing with a multicomponent system, we use superscripts N, NO, and NOH for
naphthalene, naphthalene epoxide, and naphthol, respectively, to differentiate between the
concentration terms in various compartments.
A mass balance of NO is performed over the two chambers – lung and liver. This yields two
linear equations in the unknowns 𝐶𝑙𝑢𝑛𝑔
𝑁𝑂
and 𝐶𝑙𝑖𝑣𝑒𝑟
𝑁𝑂
. Note that simplifications have been made
21
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION
to the original equations (Quick and Shuler, 1999) by assuming that 𝐶𝑙𝑢𝑛𝑔
𝑁𝑂
and 𝐶𝑙𝑖𝑣𝑒𝑟
𝑁𝑂
are small
in comparison to relevant constants present in the equations.
Lung compartment:
𝑁𝑂
𝑁𝑂 𝑁𝑂 𝑣𝑚𝑎𝑥,𝑃450−𝑙𝑢𝑛𝑔 𝐶𝑙𝑢𝑛𝑔
𝑅(𝑄𝑙𝑖𝑣𝑒𝑟 𝐶𝑙𝑖𝑣𝑒𝑟 + 𝑄𝑜𝑡 𝐶𝑙𝑢𝑛𝑔 ) + 𝑣𝑚𝑎𝑥,𝑃450−𝑙𝑢𝑛𝑔 𝑉𝑙𝑢𝑛𝑔 − 𝑉𝑙𝑢𝑛𝑔 −
𝐾𝑚,𝐸𝐻−𝑙𝑢𝑛𝑔
𝑁𝑂 𝐺𝑆𝐻
𝑣𝑚𝑎𝑥,𝐺𝑆𝑇 𝐶𝑙𝑢𝑛𝑔 𝐶𝑙𝑢𝑛𝑔 𝑁𝑂 𝑁𝑂
𝑉𝑙𝑢𝑛𝑔 𝐾𝑙 𝐺𝑆𝐻 − 𝑘𝑁𝑂𝐻 exp(𝑙𝑁𝑂𝐻 𝑇𝑃𝑙𝑢𝑛𝑔 ) 𝐶𝑙𝑢𝑛𝑔 𝑉𝑙𝑢𝑛𝑔 − 𝑄𝑙𝑢𝑛𝑔 𝐶𝑙𝑢𝑛𝑔 =0 (1)
𝑙𝑢𝑛𝑔 +𝐾2𝑙𝑢𝑛𝑔 𝐶𝑙𝑢𝑛𝑔
Liver compartment:
𝑁𝑂
𝑁𝑂 𝑣𝑚𝑎𝑥,𝑃450−𝑙𝑖𝑣𝑒𝑟 𝐶𝑙𝑖𝑣𝑒𝑟 𝑣 𝐶 𝑁𝑂 𝐶 𝐺𝑆𝐻
𝑄𝑙𝑖𝑣𝑒𝑟 𝐶𝑙𝑢𝑛𝑔 + 𝑣𝑚𝑎𝑥,𝑃450−𝑙𝑢𝑛𝑔 𝑉𝑙𝑖𝑣𝑒𝑟 − 𝑉𝑙𝑖𝑣𝑒𝑟 − 𝑉𝑙𝑖𝑣𝑒𝑟 𝐾𝑙𝑚𝑎𝑥,𝐺𝑆𝑇 𝑙𝑖𝑣𝑒𝑟 𝑙𝑖𝑣𝑒𝑟
−
𝐾𝑚,𝐸𝐻−𝑙𝑖𝑣𝑒𝑟 +𝐾2
𝑙𝑖𝑣𝑒𝑟 𝐶 𝐺𝑆𝐻
𝑙𝑖𝑣𝑒𝑟 𝑙𝑖𝑣𝑒𝑟
𝑁𝑂 𝑁𝑂
𝑘𝑁𝑂𝐻 exp(𝑙𝑁𝑂𝐻 𝑇𝑃𝑙𝑖𝑣𝑒𝑟 ) 𝐶𝑙𝑖𝑣𝑒𝑟 𝑉𝑙𝑖𝑣𝑒𝑟 − 𝑄𝑙𝑖𝑣𝑒𝑟 𝐶𝑙𝑖𝑣𝑒𝑟 =0 (2)
NO balance assumptions
The parametric values and definitions are provided below. The modeling parameters
correspond to naphthalene processing in mice.
Flowrates
Compartment volumes
22
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION
Vliver: volume of liver compartment = 3.5 mm ×4.6 mm ×20 μm = 3.22 × 10-7l = 0.322 μl.
Reaction constants
kNOH: rate constant for rearrangement of epoxide to naphthol = 0.173 μM/μM of NO/min;
lNOH: constant that relates naphthol formation rate to total protein content = − 20.2 ml/g
protein
vmax, GST: maximum reaction velocity for binding of naphthalene epoxide to GSH catalyzed
by GST (glutathione S-transferase) = 2750 μM/min;
23
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION
Protein concentrations
Your goal is to vary the recycle fraction from 0.6 to 0.95 in increasing increments of 0.05 in
order to study the effect of reduced excretion of toxicant on circulating concentration
values of naphthalene and its primary metabolite naphthalene epoxide.
b. Plot the concentration values of epoxide in the liver and lung chambers as a function of
R.
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
24
SYSTEM OF LINEAR EQUATION: Jacobi & Gauss Seidel Iteration
Preliminary Task
There are several linear equations, as follows:
2x1 + x2 - 5x3 = 9
x1 - 5x2 - x3 = 14
7x1 - x2 - 3x3 = 26
Define x1, x2, and x3 by using Jacobi iteration and Gauss-Seidel Method (x1(0)=1,
x2(0)=x3(0)=2, do the iteration until the third iteration)
Literature Review
To minimize the error in the rounding process in Gauss elimination method, the linear
equation system could be solved by using iteration method. There are two types of
iteration method, which are Jacobi iteration method and Gauss-Seidel iteration method.
………………………………………………
with akk ≠ 0 , k=1,2,……,n then the iteration equation could be written as follows.
25
SYSTEM OF LINEAR EQUATION: Jacobi & Gauss Seidel Iteration
As a stop iterative condition, the relative error equation below could be used.
𝑥𝑖𝑘+1 −𝑥𝑖𝑘
| |<𝜀 (5.2)
𝑥𝑖𝑘+1
𝑏𝑖 −∑𝑛 𝑘
𝑗=𝑖,𝑗≠𝑖 𝑎𝑖𝑗 𝑥𝑗
𝑥𝑖𝑘+1 = , k=0, 1, 2, … (5.3)
𝑎𝑖𝑖
In the Gauss-Seidel iteration method, every new x that is just obtained is used for the
next equation immediately. The iteration procedure is defined by using the following
equation.
𝑏𝑖 −∑𝑛 𝑘+1
𝑗=𝑖,𝑗≠𝑖 𝑎𝑖𝑗 𝑥𝑗 −∑𝑛 𝑘
𝑗=𝑖+1 𝑎𝑖𝑗 𝑥𝑗
𝑥𝑖𝑘+1 = , k=0, 1, 2, … (5.4)
𝑎𝑖𝑖
Algorithm
1. Input the dimension of the matrix.
2. Input matrix A and B.
3. Define the error limit.
4. Define the initial value of xi, for i=1 to n
5. K=1
𝑘+1 𝑘
6. As long as (|𝑥𝑖 𝑥𝑘+1
−𝑥𝑖
| > 𝑒𝑟𝑟𝑜𝑟) Do step 6 to 15
𝑖
26
SYSTEM OF LINEAR EQUATION: Jacobi & Gauss Seidel Iteration
8. Sum=0
9. For j = 1 to n, do step 9 to 10
10. If j ~= I, do step 10
11. sum = sum + ai,j * xj
12. xi = (bi - sum) /ai,i
𝑥𝑖𝑘+1 −𝑥𝑖𝑘
13. Calculate | |
𝑥𝑖𝑘+1
Task
1. Define a biomedical problem of “Drug development and toxicity studies: animal-on-a-
chip“. (Reference: King M.R and Mody N.A , 2010, “Numerical and Statistical Methods for
Bioengineering”, Cambridge University Press, New York , page 48) by using Jacobi
iteration method and Gauss-Seidel Iteration method! Analyze the advantages and
disadvantages of both methods!
2. Construction of Diet
A doctor suggest a patient to follow a diet program based on the table below.
27
SYSTEM OF LINEAR EQUATION: Jacobi & Gauss Seidel Iteration
3. Electrical Circuits
Please define i12, i52, i32, i65, i54, i13, V2, V3, V4, V5, if the
following information is known.
R12 = 5 Ω; R23 = 10 Ω; R34 = 5 Ω;
R45 = 15 Ω; R52 = 10 Ω; R65 = 20 Ω;
V1 = 200 V; V6 = 0 V
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A , 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York
28
REGRESSION: Linear & Polynomials
Preliminary Task
There are data of variable x and y, such as:
If y = f(x) is a linear function, define the equation of that function by using linear
regression method!
Literature Review
Regression is a method to obtain an approximate mathematical model (function) of two
dependent variables. It could produce a linear, polynomial and exponential function
regarding the assumption of the user as shown in Figure 6.1.
29
REGRESSION: Linear & Polynomials
Figure 6.1. The example of regression method (a) the data was accustomed to Lagrangian
polynomial function fourth order (b) the data was accustomed to a linear line, and (c) the
comparison of both graphs
𝑓(𝑥) = 𝑎 + 𝑏𝑥
30
REGRESSION: Linear & Polynomials
Thus, we need to find a and b that could be determined by solving normal equations shown
below.
𝑛 ∑ 𝑥𝑖 𝑎 ∑ 𝑦𝑖
[ ] = [ ][ ]
𝑏
∑ 𝑥𝑖 ∑ 𝑥𝑖 2 ∑ 𝑥𝑖 𝑦𝑖
By solving that matrix equation, we could obtain a and b, like the equation shown below.
𝑛 ∑ 𝑥𝑖 𝑦𝑖 − ∑ 𝑥𝑖 ∑ 𝑦𝑖
𝑏=
𝑛 ∑ 𝑥𝑖 2 − (∑ 𝑥𝑖 )2
𝑎 = 𝑦 − 𝑏𝑥
Algorithm
1. Input data x and y
3. Calculate xi
4. Calculate yi
5. Calculate x 2
i
6. Calculate x y
i i
𝑛 ∑ 𝑥𝑖
7. Make matrix 𝑀 = [ ]
∑ 𝑥𝑖 ∑ 𝑥𝑖 2
yi
8. Make matrix N =
xi yi
a
9. Calculate = M −1 N
b
31
REGRESSION: Linear & Polynomials
Task
1. Make a linear regression program based on data as shown below.
X 8 17 20 25 31 42 50 59 65 72 80
Y 100 130 209 276 330 359 420 487 550 645 700
32
REGRESSION: Linear & Polynomials
from hemoglobin, and conversion of bicarbonates into CO2 and H2O, resulting in CO2
leaving the blood and entering into the lungs.
The Hill equation describes a mathematical relationship between the extent of oxygen
saturation of hemoglobin and the partial pressure of O2 in blood. This equation is
derived from the application of the law of mass action to the state of chemical
equilibrium of the reaction
𝐻𝑏(𝑂2 )𝑛 ↔ 𝐻𝑏 + 𝑂2
[𝐻𝑏(𝑂2 )𝑛 ] (𝑝𝑂2 )𝑛
𝑆= = 𝑛 (1)
[𝐻𝑏(𝑂2 )𝑛 ] + [𝐻𝑏] 𝑃50 + (𝑝𝑂2 )𝑛
Equation (1) is the Hill equation, and accounts for the observed cooperative binding that
produces the characteristic sigmoidal shape of the hemoglobin–oxygen dissociation
curve.
The partial pressure of oxygen that results in occupancy of half of the oxygen binding
sites of hemoglobin is denoted by P50.
Biochemical engineers at several universities have explored the use of polymerized
bovine hemoglobin as a possible blood substitute (Levy et al., 2002). Monomeric
hemoglobin is small enough to leak out of capillary pores; however, by chemically
attaching several hemoglobin units together, the end product is large enough to be
retained within the circulatory system. Data collected for tetrameric bovine hemoglobin
binding to oxygen are given in Table 2.1.
We first examine the shape of the oxygen dissociation curve by plotting the data in
Figure 2.5.
We wish to determine the best-fit values of P50 and n in the Hill equation for this data
set and compare with values measured for human hemoglobin. Equation (1) is
nonlinear but can be converted to linear form by first rearranging to:
𝑆 (𝑝𝑂2 )𝑛
= 𝑛 (2)
1−𝑆 𝑃50
𝑆
𝑙𝑛 = 𝑛 𝑙𝑛(𝑝𝑂2 ) − 𝑛 ln 𝑃50 (3)
1−𝑆
33
REGRESSION: Linear & Polynomials
Plotting ln (S/(1-S)) as a function of ln(pO2) produces a line with slope n and intercept
-n ln P50.
Equation (3) is the functional form for the dependency of oxygen saturation of
hemoglobin as a function of the oxygen partial pressure, and is linear in the regression
parameters.
Question:
a. Please derive equation (1) to equation (3).
b. Make a program of linear regression based on the data above by following the given
information. (Remember: the equation is non-linear at first, so it is needed to be
linearized first. Then, input the data in the linearized equation to get the unknown
variables).
c. Find what is n and P50
d. Plot the data before and after linearization.
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
34
INTERPOLATION
Preliminary Task
Make an Lagrange interpolation of the following data: (0,1), (1,2), (3,4), (6,-1)
Literature Review
Lagrange interpolation is used to find several connecting dots of n dots P1(x1, y1), P2(x2,y2),
P3(x3, y3), … , PN(xN, yN) by using polynomial function approximation that is arranged in a
row combination and defined as follows.
𝑁
(𝑥 − 𝑥𝑗 )
𝑦 = ∑ 𝑦𝑖 ∏
(𝑥𝑖 − 𝑥𝑗 )
𝑖=1 𝑗≠𝑖
Algorithm
1. Define the number of dots (n)
5. Print (x, y)
35
INTERPOLATION
Task
There is a data as shown below.
n 1 2 3 4 5 6 7
x(n) 0.1 0.3 0.5 0.7 0.9 1.1 1.3
y(n) 0.030 0.067 0.148 0.248 0.320 0.518 0.697
1. Define the order of the polynomial Lagrange interpolation that exactly goes through the
seven dots shown above and Plot the function!
2. Guess the y for each dot in the table below and show them as well in the graph that has
been made!
x(n) y(n)
0.365
0.512
0.621
0.715
3. What does happen if the x value is not in the range of the data of Lagrange interpolation?
Elaborate your answer and show it in a plot!
5. Give one example of biomedical case that could use Lagrange interpolation to solve it!
Provide the data and the result of the Lagrange interpolation!
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
36
NUMERICAL DERIVATIVE
Preliminary Task
Define the analytical value of f '(1) from a function f(x) = x sin(x)!
Literature Review
If there are some points of x at x0 - h, x0, and x0 + h and also their result of a function as well,
several points would be obtained, such as (x-1, f-1), (x0, f0) and (x1, f1), with x-1= x0 - h and x1
= x0 + h. There are three approximations to calculate the f'(x0) as follows.
37
NUMERICAL DERIVATIVE
Figure 8.1. Three type of derivative approximation (a) forward difference approximation
(b) backward difference approximation, and (c) central difference approximation
Algorithm
1. Define the function that would be derived, such as f(x)
Task
1. Explain the effect of the change of h towards the error of numerical derivative
calculation and give the reason to that!
2. Modify the program that you make to solve the following problem. Calculate f ’(1.5) if
the available points are (1.2, 0.8333), (1.4, 0.7143), (1.6, 0.6250), and (1.8, 0.5556).
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
38
NUMERICAL INTEGRATION
Preliminary Task
If there is function as follows:
Calculate the following integration by using trapezium and Simpson 1/3 method!
2
∫ 𝑓(𝑥)𝑑𝑥
0
Literature Review
The analytical integration calculation was performed by using discrete points that were
divided into several parts called “segment”. There are several method that use segment
method, such as trapezoid method and rectangular method. The rectangular method
divide the area below the curve into several rectangle segments as shown in Figure 9.1.
39
NUMERICAL INTEGRATION
The trapezoid method is almost the same as rectangular method. The shape that is used
for dividing the area below the curve is a trapezium. By using this method, it is expected to
minimize the error produced by the shape of rectangle in rectangular method. The
equation for trapezoid method is illustrated in Figure 9.2.
40
NUMERICAL INTEGRATION
Besides the “segment” method, there is another method to calculate the integration of a
function, such as Newton Cotes method. This method is used polynomial interpolation,
such as trapezoid method, Simpson 1/3 method, and Simpson 3/8 method.
Simpson 1/3 method needs at least 3 points to determine the approximation of the
integration of a function, as example (0, f(0)), (h, f(h)), dan (2h, f(2h)) as shown in Figure
9. 3.
41
NUMERICAL INTEGRATION
Task
1. A sky-diver falls from a plane with a velocity as shown in equation below.
v = velocity (m/s)
g = gravitational acceleration = 9.8 m/s2
m = sky-diver mass = 78.5 kg
c = air resistive coefficient = 12.5 kg/s
Please, calculate how far the sky diver is fell after 12 seconds with several numerical
integration methods and compare the result of each method to each other. Define the
error compared to the analytical result. Which method is the most accurate? Explain!
2. IV Drip
A cylindrical bag of radius 10 cm and height 50 cm contains saline solution (0.9% NaCl)
that is provided to a patient as a peripheral intravenous (IV) drip. The method used to
infuse saline into the patient’s bloodstream in this example is gravity drip. The solution
flows from the bag downwards under the action of gravity through a hole of radius 1
mm at the bottom of the bag. If the only resistance offered by the flow system is the
viscous resistance through the tubing, determine the time it will take for the bag to
empty by 90%. The length of the tubing is 36″ (91.44 cm) and its ID (inner diameter) is
1 mm. The viscosity of the saline solution μ = 0.01 Poise, and the density ρ = 1 g/cm3.
Let L be the length of the tubing, d the diameter of the tubing, and R the radius of the
cylindrical bag. Then, L = 91.44 cm; d = 0.1 cm; R = 10 cm.
Mechanical energy balance
The mechanical energy balance is given by the Bernoulli equation corrected for loss of
mechanical energy due to fluid friction. The mechanical energy at the liquid level in the
bag is equal to the mechanical energy of the fluid flowing out of the tubing minus the
frictional loss. We ignore the friction loss from sudden contraction of the cross-sectional
area of flow at the tube entrance. The height at which the drip fluid enters the patient is
considered the datum level. The height of the fluid in the bag is z cm with respect to the
bottom of the bag, and is (z + L) cm with respect to the datum level (see
Figure 6.12).
42
NUMERICAL INTEGRATION
The pressure drop in the tubing due to wall friction is given by the Hagen–Poiseuille
equation:
32𝐿𝑢𝜇
∆𝑝 = = 2926.08𝑢
𝑑2
The Bernoulli equation for this system is as follows:
𝑢2 ∆𝑝 𝑢2 32𝐿𝑢𝜇
𝑔(𝑧 + 𝐿) = + = + 2 ,
2 𝜌 2 𝑑 𝜌
Where ∆𝑝/𝜌 is the term for mechanical loss due to fluid
friction and has units of energy per mass.
Steady state mass balance
There is no reaction, accumulation, or depletion within the
system, and we ignore the initial unsteady flow when
initiating the IV drip. The mass flow rate in the bag is equal
to the mass flow rate in the tubing:
𝑑𝑧 𝜋
−𝜌𝜋𝑅 2 = 𝜌 𝑑 2 𝑢.
𝑑𝑡 4
On rearranging, we get
4𝑅 2
𝑑𝑡 = − 2 𝑑𝑧.
𝑑 𝑢
We can express u in terms of z by solving Equation (6.24)
for u.
Letting a=64L𝜇/d2𝜌
−𝑎 + √𝑎2 + 8𝑔(𝑧 + 𝐿)
𝑢= .
2
Substituting the above into the steady state mass balance,
8𝑅 2
𝑑𝑡 = − 𝑑𝑧.
(−𝑎 + √𝑎2 + 8𝑔(𝑧 + 𝐿))𝑑 2
Integrating the differential equation
We wish to integrate z from 50 cm to 5 cm. Integrating the left-hand side from 0 to
t, we obtain:
5
8𝑅 2
𝑡=∫ − 𝑑𝑧
50 (−𝑎 + √𝑎2 + 8𝑔(𝑧 + 𝐿))𝑑 2
8𝑅 2 50 1
= 2 ∫ 𝑑𝑧
𝑑 5 (−𝑎 + √𝑎2 + 8𝑔(𝑧 + 𝐿))
43
NUMERICAL INTEGRATION
Question:
a. Calculate the time that the IV Drip needed to be changed! (When the z = 5 cm).
Use all integration methods for this problem!
If N = 20 links and b = 1, calculate the probability the chain ends come within a
distance b of each other. Use a two-segment composite trapezoidal rule and then a
four-segment composite trapezoidal rule. Use these two approximations to
extrapolate to an even more accurate solution by eliminating the O(h2) error term,
where h is the panel width.
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A , 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York
44
RICHARDSON EXTRAPOLATION: Derivatives & Integration
Preliminary Task
Given the data points:
Literature Review
Richardson extrapolation is a numerical method for boosting accuracy. Richardson
extrapolation can be applied in numerical derivatives to obtain a more accurate solution.
Suppose that D(h) and D(2h) are the approximation of f'(x0) by taking the points
respectively a distance of h and 2h. For example, to calculate f '(x0), the formula for center-
difference approximation O(h2) is used:
( 𝑓1 −𝑓−1 )
𝐷(ℎ) = 2ℎ
+ 𝑂 (ℎ2 )
= 𝑓0′ + 𝐶(2ℎ)2 + ⋯
45
RICHARDSON EXTRAPOLATION: Derivatives & Integration
or
Where n is the error order of the formula used. For example the formula used for the
center-difference order O(h2) in calculating D(h) and D(2h), then n = 2, so the extrapolation
formula for Richardson is as in equation (10.6).
Also note that each expansion of Richardson extrapolation will increase the error order
from O(hn) to O(hn+2).
Algorithm
1. Define the data points (x), function f(x) and a desired value, which want to calculate the
derivative.
4. Define h.
( 𝑓1 −𝑓−1 )
5. Calculate 𝐷(ℎ) = 2ℎ
6. Define 2h
46
RICHARDSON EXTRAPOLATION: Derivatives & Integration
( 𝑓2 −𝑓−2 )
7. Calculate 𝐷(2ℎ) = 2(2ℎ)
Task
Let assume D(2h) and D(4h) are the approximate derivation of f '(x0) with the interval 2h
and 4h using the formula of the order of center-order O(h4). By using the Richardson
extrapolation, calculate the better estimate of f '(x0):
[𝐷(2ℎ ) − 𝐷 (4ℎ)]
𝑓0′ = 𝐷(2ℎ) +
15
Determine the approximate derivation of f '(1.2) if the function is 𝑓(𝑥) = 𝑒 𝑥 in the
interval [0.8, 1.6] with h = 0.1.
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
47
RICHARDSON EXTRAPOLATION: Derivatives & Integration
Preliminary Task
1 1
Calculate the result of ∫0 𝑑𝑥 using the Richardson extrapolation and Romberg
1+𝑥
integration. (the total of segment is 8)
Literature Review
Richardson extrapolation can also be applied in numerical integration to obtain a more
accurate solution. Recall the Trapezoidal rule:
𝑏 ℎ (𝑏−𝑎)𝑓′′(𝑡)
∫𝑎 𝑓(𝑥)𝑑𝑥 = 2 (𝑓0 + 2 ∑𝑛𝑖=1 𝑓𝑖 + 𝑓𝑛 ) − 12
ℎ2 (11.1)
if it assumes that 𝐶 is constant regardless of step size h, then q is determined from the
integration method’s error order. For the example:
48
RICHARDSON EXTRAPOLATION: Derivatives & Integration
I1 and I2. The higher-order integral approximation I3 obtained by combining the two low-
accuracy approximations of the trapezoidal rule so it has an error order O(h4). Thus, we
can reduce the error efficiently in a single step using Richardson extrapolation.
Suppose the trapezoidal rule is used to solve an integral over the interval [a, b]. If J is the
better approximate value of the integral, I(h) is the approximation from n-segment of
trapezoidal rule with the step size h1 = h and h2 = 2h, and the error is C(h), then the
numerical integration will be:
Thus, we obtain:
𝐼(ℎ)−𝐼(2ℎ)
𝐶= (2𝑞 −1)ℎ𝑞
(11.7)
𝑂(ℎ2𝑁 ) → 𝑂(ℎ2𝑁+2 )
49
RICHARDSON EXTRAPOLATION: Derivatives & Integration
Recall the Richardson extrapolation (11.8), if I is the exact value of the integral then it can
be written as:
Ak is the approximate value of the integral using Trapezoidal method with total of segment
n = 2k and the error order O(h2).
A0, A1, … Ak from the Richardson extrapolation then will be used to determine the sequences
of Bk, which is:
𝐴𝑘 −𝐴𝑘−1
𝐵𝑘 = 𝐴𝑘 + (11.10)
22 −1
So, from now on the better I will be 𝐼 = 𝐵𝑘 + 𝐷′ℎ4 + 𝐸′′ℎ6 + ⋯ the error order O(h4).
Hereafter, B0, B1, … Bk will be used for obtaining the sequences of C0, C1, … Ck
𝐵𝑘 −𝐵𝑘−1
𝐶𝑘 = 𝐵𝑘 + (11.11)
24 −1
This process will be continue until the n-segment (n = 2k) is fulfilled, Thus the order of
error O(h) and the layer of Richardson extrapolation (A, B, C, … , k+1)will increase depends
on k.
The better
integration
result
50
RICHARDSON EXTRAPOLATION: Derivatives & Integration
Algorithm
Richardson Extrapolation
1. Define the data points (x), function f(x) and number of segment
3. Calculate the integral results (I(h)) using integration method (such as Trapezoidal
method) for step size h
5. Calculate the integral results (I(2h)) using integration method (such as Trapezoidal
method) for step size 2h
𝐼(ℎ)−𝐼(2ℎ)
6. Calculate the result of integration using Richardson extrapolation 𝐽 = 𝐼(ℎ) + 22 −1
Romberg Integration
1. Define the data points (x), function f(x) and number of segment (n)
ln(𝑛)
2. Determine the total of layer (k) , since n-segment (n = 2k) then 𝑘 = ⁄ln(2)
3. Calculate the integral results (I(h)) using integration method (such as Trapezoidal
method) for step size h
5. Calculate the integral results (I(2h)) using integration method (such as Trapezoidal
method) for step size 2h
𝐼(ℎ)−𝐼(2ℎ)
6. Calculate the result of integration using Richardson extrapolation 𝐽 = 𝐼(ℎ) + 22 −1
51
RICHARDSON EXTRAPOLATION: Derivatives & Integration
Task
Using the same problem as in Chapter 9, a cylindrical bag of radius 10 cm and height 50
cm contains saline solution (0.9% NaCl) that is provided to a patient as a peripheral
intravenous (IV) drip. The method used to infuse saline into the patient’s bloodstream in
this example is gravity drip. The solution flows from the bag downwards under the action
of gravity through a hole of radius 1mm at the bottom of the bag. If the only resistance
offered by the flow system is the viscous resistance through the tubing, determine the
time it will take for the bag to empty by 90%. The length of the tubing is 36″ (91.44 cm)
and its ID (inner diameter) is 1 mm. The viscosity of the saline solution μ = 0.01 Poise, and
the density ρ = 1 g/cm3. Let L be the length of the tubing, d the diameter of the tubing, and
R the radius of the cylindrical bag. Then, L =91.44 cm; d =0.1 cm; R =10 cm. (g = 981 cm2/s)
8𝑅 2 50 1
𝑡= 2
∫ 𝑑𝑧
𝑑 5 (−𝑎 + √𝑎2 + 8𝑔(𝑧 + 𝐿))
Use Romberg integration four level (layer k =3, n-segment = 23 = 8) and compare the error
with the previous Trapezoidal method! (Hint: The exact value of integral =
0.574948166362027)
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
52
ORDINARY DIFFERENTIAL EQUATION (ODE)
Preliminary Task
Use Euler’s method and Heun’s method to integrate y’ = 4e0.8t – 0.5y from t = 0 to 4 with a
step size of one. The initial condition at t = 0 is y = 2. Note that the exact solution can be
4
determined analytically as: 𝑦(𝑡) = 1.3 (𝑒 0.8𝑡 − 𝑒 −0.5𝑡 ) + 2𝑒 −0.5𝑡
Literature Review
Euler’s Method
𝑑𝑦⁄
A differential equation ( 𝑑𝑡) is expressed in a function𝑓(𝑡, 𝑦), where 𝑦(𝑡) is an original
equation.
𝑑𝑦
= 𝑓(𝑡, 𝑦), 𝑎 ≤ 𝑡 ≤ 𝑏, 𝑦(𝑎) =∝ (12.1)
𝑑𝑡
It is known that the value of t is in range a and b and if the initial conditions is t = a then y
is α. However, we do not know the form of the original equation formulation at all y (t). So
the challenge is how we can get a solution of differential equations for each value of y (t),
which t is in interval [a, b]. The initial step of the numerical approach is to specify points
within the step size in the interval [a, b] as:
𝑏−𝑎
ℎ= (12.2)
𝑁
Euler method is derived from Taylor. For example, the function y(t) is a continuous function
and has derivatives in the interval [a, b]. In the Taylor series, the function y(t) is formulated
as:
(𝑡𝑖+1 −𝑡𝑖 )2
𝑦(𝑡𝑖+1 ) = 𝑦(𝑡𝑖 ) + (𝑡𝑖+1 − 𝑡𝑖 )𝑦 ′(𝑡𝑖 ) + 𝑦′′(𝜉𝑖 ) (12.3)
2
53
ORDINARY DIFFERENTIAL EQUATION (ODE)
(𝑡𝑖+1 −𝑡𝑖 )2
𝑦(𝑡𝑖+1 ) = 𝑦(𝑡𝑖 ) + (𝑡𝑖+1 − 𝑡𝑖 )𝑦 ′(𝑡𝑖 ) + 𝑦′′(𝜉𝑖 ) (12.4)
2
Euler method is built based on equation (12.4), which ignored the second derivative.
Besides, in general notation for 𝑦(𝑡𝑖 ) is replaced by 𝑤𝑖 . So, the Euler method is formulated
as:
Where i = 0, 1, 2, …, N-1.
Heun’s Method
Heun’s method is used to improve the estimation of differential solution by determining
then averaging two derivatives for the entire interval. Recall the Euler’s method (12.1 to
12.3):
0
𝑤𝑖+1 = 𝑤𝑖 + ℎ 𝑓(𝑡𝑖 , 𝑤𝑖 )
In Heun’s method the 𝑤𝑖+1 is not the final answer, but a predictor equation. It provides an
estimate that allows the calculation of a slope at the end of the interval:
′ 0
𝑤𝑖+1 = 𝑓(𝑡𝑖+1 , 𝑤𝑖+1 )
Then, this predictor equation corrected by averaging two slopes, which is called a corrector
equation:
0
𝑓(𝑡𝑖 , 𝑤𝑖 ) + 𝑓(𝑡𝑖+1 , 𝑤𝑖+1 )
𝑤𝑖+1 = 𝑤𝑖 + ℎ
2
Algorithm
Euler’s Method
54
ORDINARY DIFFERENTIAL EQUATION (ODE)
Heun’s Method
Task
1. A non-charged capacitor is connected in series with a resistor and battery (Figure 12.1).
It is known that Q = 12 Volts, C = 5.00 μF and R = 8.00 × 105 Ohm. When the switch is
connected (t = 0), the charge does not yet exist (q = 0).
𝑑𝑞 𝜖 𝑞
= −
𝑑𝑡 𝑅 𝑅𝐶
The exact solution is:
55
ORDINARY DIFFERENTIAL EQUATION (ODE)
If t0 = 0 then a = 0 and at that time q0 = 0.0. Step size h = 0.1 then t1 = 0.1. Calculate the
charge (q) at time t = 2 using the Euler’s and Heun’s methods and plot the curve of
charging the q in t intervals 0 to 2. Compare the results!
2. HIV–1 virus particles attack lymphocytes and hijack their genetic machinery to
manufacture many virus particles within the cell. Once the viral particles have been
prepared and packaged, the viral genome programs the infected host to undergo lysis.
The newly assembled virions are released into the blood stream and are capable of
attacking new cells and spreading the infection. The dynamics of viral infection and
replication in plasma can be modeled by a set of differential equations (Perelson et
al.,1996). If T is the concentration of target cells, T* is the concentration of infected cells,
and V1 is the concentration of infectious viral RNA in plasma, and VX is the
concentration of noninfectious viral particles in plasma, we can write:
𝑑𝑇 ∗
= 𝑘𝑉1 𝑇 − 𝛿𝑇 ∗
𝑑𝑡
𝑑𝑉1
= −𝑐𝑉1
𝑑𝑡
𝑑𝑉𝑥
= 𝑁𝛿𝑇 ∗ − 𝑐𝑉𝑥
𝑑𝑡
56
ORDINARY DIFFERENTIAL EQUATION (ODE)
The experimental study yielded the following estimates of the reaction rate parameters
(Perelson et al., 1996):
𝛿 = 0.5/𝑑𝑎𝑦
𝑐 = 03.0/𝑑𝑎𝑦
V1(t = 0) =100/μl;
VX (t = 0) = 0/μl;
Calculate the T*, V1, VX at time t = 5 days using the Euler’s methods and plot the curve of
T*, V1, VX in t intervals 0 to 5.
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
57
ORDINARY DIFFERENTIAL EQUATION (ODE)
Preliminary Task
Solve the following equation by using the thord order Runge-Kutta method.
dy
= − 2 x 3 + 12 x 2 − 20 x + 8,5.
dx
from x = 0 to x = 4 by using an interval of x = 0,5. The initial condition at x = 0 is y = 1.
Literature Review
Euler method ends up with a non-thorough result. Because of that, we need to consider
higher order of Taylor series or by using small interval or x. Both of methods is not
beneficial. The calculation of higher order needs higher derivative of a function while the
use of small interval needs longer time of computation.
The Runge-Kutta method gives more precise result and does not need the derivative of the
function. The common form of Runge-Kutta method is shown in equation (13.1).
yi + 1 = yi + Φ ( xi , y i , Δx) Δx (13.1)
Where (xi, yi, x) is a function of addition that is an average slope in the interval. The
function of addition could be written as a common form shown in equation (2)
58
ORDINARY DIFFERENTIAL EQUATION (ODE)
The equation shows that the k value has a sequential relationship. The value of k1 appears
in the equation to calculate k2 that also appears in the equation to calculate k3 and so on.
This sequantial relationship makes the RungeKutta method becomes efficient in the
calculation process.
There are several type of Runge-Kutta method that depends on the value of n that is used
in the equation.
Φ = a1k1 = a1 f ( xi , yi ) (13.7)
yi + 1 = yi + f ( x i , yi ) Δx (13.8)
In the Runge-Kutta method, after the value of n is defined, the value of a, p and q are
calculated by using Equation (1) by using the member of Taylor series.
The second order Runge-Kutta method has a form shown in Equation (13.9).
where:
k1 = f ( xi , yi ) (13.10)
The value of a1, a2, p1 and q11 were evaluated by equaling Equation (13.10) with the second
order Taylor series that has form as shwon in Equation (13.12).
Δx Δx
yi + 1 = yi + f ( xi , yi ) + f ' ( xi , yi ) (13.12)
1 2
59
ORDINARY DIFFERENTIAL EQUATION (ODE)
where f ' ( xi , yi ) could be defined from the chain rule as shown in Equation (13.13).
f f dy
f ' ( xi , yi ) = + (13.13)
x y dx
Δx f f dy Δx
yi + 1 = yi + f ( xi , yi ) +( + ) (13.14)
1 x y dx 2
The value of a1, a2, p1 and q11 was searched for so that the Equation (13.9) is equaivalent
to Equation (13.14). Thus, the Taylor series is used to explpore Equation (13.11). The
Taylor series as a function of two variables has a form of equatino shown below.
g g
g ( x + r , y + s ) = g ( x, y ) + r +s + ... (13.15)
x y
f f
f ( xi + p1 Δx, yi + q11 k1 Δx) = f ( xi , y i ) + p1 Δx + q11 k1 Δx + 0 ( Δx 2 ) (13.15)
x y
The previous form and Equation (13.10) is substituted to Equation (13.9) and resulted in
Equation below.
f
yi + 1 = yi + a1 Δx f ( xi , yi ) + a2 Δx f ( xi , yi ) + a2 p1 Δx 2
x
(13.16)
f
+ a2 q11 Δx f ( xi , yi )
2
+ 0( Δx 3 )
x
or
y1 + 1 = yi + a1 f ( xi , yi ) + a2 f ( xi , yi )Δx
f f (13.17)
+ a2 p1 + a2 q11 f ( xi , yi ) Δx 2 + 0( Δx 3 )
x x
By comparing Equation (13.14) and Equation (13.15), it could concluded that both
equations would be equavalent if:
a1 + a2 = 1. (13.18)
60
ORDINARY DIFFERENTIAL EQUATION (ODE)
1
a 2 p1 = . (13.19)
2
1
a2 q11 = . (13.20)
2
The equation system above that has 3 equations with four unknown variables is
unsolvable. Thus, one of the unknown variables should be defined and the other three
variavbles could be calculated. If a2 is stated, then Equation (9a), (9b), and (9c) could
solved and produces:
a1 = 1 − a 2 (13.21)
1
p1 = q11 = (13.22)
2a 2
Because the value of a2 is chosen randomly, the second order Runge-Kutta method would
be numerous. One of them is Heun method.
The fourth order Runge-Kutta method is derived by using the same method as the second
order Runge-Kutta method with n = 3. The result is six equations with eight unknown
variables. Thus, two variables need to be defined first to obtain the other six unknown
variables. The common result is shown in Equation (13.23).
1
𝑦𝑖+1 = 𝑦𝑖 + 6 (𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4 ) (13.23)
Where
𝑘1 = ∆𝑥 . 𝑓(𝑥𝑖 , 𝑦𝑖 ) (13.24)
1 1
𝑘2 = ∆𝑥 . 𝑓(𝑥𝑖 + 2 ∆𝑥, 𝑦𝑖 + 2 𝑘1 ∆𝑥) (13.25)
1 1
𝑘3 = ∆𝑥 . 𝑓(𝑥𝑖 + 2 ∆𝑥, 𝑦𝑖 + 2 𝑘2 ∆𝑥) (13.26)
61
ORDINARY DIFFERENTIAL EQUATION (ODE)
Algorithm
1. Import module and define the function.
𝑘1 = ℎ ∗ 𝑓(𝑥𝑖 , 𝑦𝑖 )
1 1
𝑘2 = ℎ ∗ 𝑓(𝑥𝑖 + 2 ∆𝑥, 𝑦𝑖 + 2 𝑘1 ∆𝑥)
1 1
𝑘3 = ℎ ∗ 𝑓(𝑥𝑖 + 2 ∆𝑥, 𝑦𝑖 + 2 𝑘2 ∆𝑥)
Task
1. By using the same problem as shown in Chapter 12, please calculate the load (q) at t=2
by using the fourth order Runge-Kutta method and plot the curve of loading process
(charging) towards t between t = 0s and t = 2s. Compare the result!
2. A spherical cell or particle of radius a and uniform density 𝜌𝑠 , is falling under gravity in
a liquid of density 𝜌𝑓 and viscosity 𝜇. The motion of the sphere, when wall effects are
minimal, can be described by the equation:
4 3 𝑑𝑣 4
𝜋𝑎 𝜌𝑠 + 6𝜇𝜋𝑎𝑣 − 𝜋𝑎3 (𝜌𝑠 − 𝜌𝑓 )𝑔 = 0
3 𝑑𝑡 3
With initial boundary conditions
𝑑𝑧
𝑧 = 0; 𝑣= = 0,
𝑑𝑡
Where z is the displacement distance of the sphere in the downward direction.
Downward motion v is along the positive z direction. On the left-hand side of the
62
ORDINARY DIFFERENTIAL EQUATION (ODE)
equation, the first term describes the acceleration, the second describes viscous drag,
which is proportional to the sphere velocity, and the third is the force due to gravity
acting downwards from which the buoyancy force has been subtracted. Two initial
conditions are included to specify the problem completely. This initial value problem
can be converted from a single second-order ODE into a set of two coupled first-order
ODEs. Set y1 = z and y2 = dz/dt. Then:
𝑑𝑦1
= 𝑦2 , 𝑦1 = 0
𝑑𝑡
𝑑𝑦2 (𝜌𝑠 − 𝜌𝑓 ) 9𝜇
= 𝑔 − 2 𝑦2, 𝑦2 = 0
𝑑𝑡 𝜌𝑠 2𝑎 𝜌𝑠
The constants are set to:
a = 10-4 cm; 𝜌𝑠 = 1.1 g/cm3; 𝜌𝑓 = 1 g/cm3; g = 981 cm/s2; 𝜇 = 3.5 x 10-2 g/cm.s
Plotting the numerically calculated displacement z and the velocity dz/dt with time
interval 0 to 0.001 s and find the minimum step size to obtain the steady state velocity
(which it does not increase or decrease).
References
1. Capra, Steven C and Canale, 1991, “Numerical Methods for Engineers with Personal
Computer Applications”, MacGraw-Hill Book Company.
2. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
63
PARTIAL DIFFERENTIAL EQUATION (PDE)
Preliminary Task
It is known one-dimensional heat distribution (1D) as a function of time (t) on a metal
meets the following equation:
𝜕𝑢 𝜕 2𝑢
(𝑥, 𝑡) − (𝑥, 𝑡) = 0, 0<𝑥<1 0≤𝑡
𝜕𝑡 𝜕𝑥 2
𝑢(𝑥, 0) = 𝑠𝑖𝑛(𝜋𝑥), 0 ≤ 𝑥 ≤ 1,
Solve that PDP parabolic equation by using finite difference method if the vertical axis
shows changes over time with intervals k = 0,0005. Because α = 1, h = 0, 1 and k = 0,0005.
Literature Review
Previously, we have learn to solve the ordinary differential equation (ODE) and now we
will determine the solution of partial differential equation (PDE) by reduce it to ODE’s
system so we could obtain the result easily. One important technique for achieving this, is
based on finite difference discretization of spatial derivatives.
We shall focus on one of the most widely encountered partial differential equations: the
diffusion equation, which in one dimension looks like:
𝜕𝑢 𝜕2 𝑢
=𝛽 +𝑔 (14.1)
𝜕𝑡 𝜕𝑥 2
64
PARTIAL DIFFERENTIAL EQUATION (PDE)
The space between two mesh points xi and xi+1, i.e. the interval [xi, xi+1], is call a cell. It could
be assume that each cell has the same length (∆𝑥 = 𝑥𝑖+1 − 𝑥𝑖 , 𝑖 = 0, … 𝑁 − 1).
The partial differential equation is valid at all spatial points x ∈ Ω, but we may relax this
condition and demand that it is fulfilled at the internal mesh points only, x1, … , xN−1:
Now, at any point xi we can approximate the second-order derivative by a finite difference:
It is common to introduce a short notation 𝑢𝑖 (𝑡) for 𝑢(𝑥𝑖 , 𝑡), i.e., u approximated at some
mesh point xi in space. By inserting (14.3) to (14.2), an approximation to the partial
differential equation at mesh point (𝑥𝑖 , 𝑡) can be written as:
𝜕𝑢𝑖 (𝑡) 𝑢𝑖+1 (𝑡)−2𝑢𝑖 (𝑡)+𝑢𝑖−1 (𝑡)
=𝛽 + 𝑔𝑖 (𝑡), 𝑖 = 1, … , 𝑁 − 1 (14.4)
𝜕𝑡 ∆𝑥 2
Note that we have adopted the notation 𝑔𝑖 (𝑡) for 𝑔(𝑥𝑖 , 𝑡).
Equation (14.4) is an ODE’s system of PDE in equation (14.2) with aid of the finite
difference approximation.
The initial condition 𝑢(𝑥, 0) = 𝐼(𝑥) translates to an initial condition for every unknown
function 𝑢𝑖 (𝑡): 𝑢𝑖 (0) = 𝐼(𝑥𝑖 ), 𝑖 = 0, … , 𝑁. At the boundary x = 0 we need an ODE in our
ODE system, which must come from the boundary condition at this point. The boundary
condition reads 𝑢(𝑥, 0) = 𝑠(𝑡). We can derive an ODE from this equation by differentiating
both sides: 𝑢0′ (𝑡) = 𝑠′(𝑡). The ODE system above cannot be used for 𝑢0′ since that equation
involves some quantity 𝑢−1 ′
outside the domain. Instead, we use the equation 𝑢0′ (𝑡) = 𝑠′(𝑡)
derived from the boundary condition. For this particular equation we also need to make
sure the initial condition is 𝑢0 (𝑡) = 𝑠(0). The reason for including the boundary values in
the ODE system is that the solution of the system is then the complete solution at all mesh
points, which is convenient, since special treatment of the boundary values is then avoided.
65
PARTIAL DIFFERENTIAL EQUATION (PDE)
𝜕𝑢
The condition 𝜕𝑥 = 0 at x = L is a bit more complicated, but we can approximate the spatial
derivative by a centered finite difference:
𝜕𝑢 𝑢𝑁+1 −𝑢𝑁−1
| ≈ =0 (14.5)
𝜕𝑥 𝑖=𝑁 2∆𝑥
This approximation involves a fictitious point xN+1 outside the domain. A common trick is
to use (14.4) for i = N and eliminate uN+1 by use of the discrete boundary condition
(𝑢𝑁+1 =𝑢𝑁−1 ):
𝜕𝑢𝑁 (𝑡) 2𝑢𝑁−1 (𝑡)−2𝑢𝑁 (𝑡)
=𝛽 + 𝑔𝑁 (𝑡) (14.6)
𝜕𝑡 ∆𝑥 2
𝑑𝑢𝑖 𝛽
𝑑𝑡
=
∆𝑥 2
(𝑢𝑖+1 (𝑡) − 2𝑢𝑖 (𝑡) + 𝑢𝑖−1 (𝑡)) + 𝑔𝑖 (𝑡) , 𝑖 = 1, … , 𝑁 − 1 (14.8)
𝑑𝑢𝑁 2𝛽
𝑑𝑡
=
∆𝑥 2
(𝑢𝑁+1 (𝑡) − 𝑢𝑁 (𝑡)) + 𝑔𝑖 (𝑡) (14.9)
Algorithm
1. Discretize solution u(x, t) into discrete values
4. Compute g(xi, ti) using finite differences than compute u(xi, ti) based on g(xi, ti)
66
PARTIAL DIFFERENTIAL EQUATION (PDE)
Task
The diffusion equation is written in one spatial dimension as:
𝜕𝑢 𝜕 2𝑢
=𝐷 2
𝜕𝑡 𝜕𝑥
Let us assume an initial concentration:
1 (𝑥 − 𝑥𝑚𝑒𝑎𝑛 )2
𝑢(𝑥, 𝑡0 ) = exp (− )
𝜎√2𝜋 𝜎2
Where 𝑥𝑚𝑒𝑎𝑛 = 0 and width 𝜎 = 0.5. By using finite difference method, plot the solution of
the diffusion equation in interval -5 to 5 with step size h = 0.1.
References
1. King M.R and Mody N.A, 2010, “Numerical and Statistical Methods for Bioengineering”,
Cambridge University Press, New York.
2. Langtangen Hans Petter and Linge Svein, 2017, “Finite Difference Computing with PDEs
- A Modern Software Approach”. Springer International Publishing.
67