0% found this document useful (0 votes)
89 views72 pages

Modul Kombio 2019 PDF

This document provides an introduction to core Python concepts including variables, strings, lists, and operators. It gives examples of how to define and manipulate variables of different data types, concatenate and slice strings, append and modify elements of lists, and access elements of nested lists. The key points covered are data types in Python like integers, floats, and strings; basic operations on variables and lists; and how to represent matrices using nested lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views72 pages

Modul Kombio 2019 PDF

This document provides an introduction to core Python concepts including variables, strings, lists, and operators. It gives examples of how to define and manipulate variables of different data types, concatenate and slice strings, append and modify elements of lists, and access elements of nested lists. The key points covered are data types in Python like integers, floats, and strings; basic operations on variables and lists; and how to represent matrices using nested lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

PREFACE

Practicum Module

BIOMEDICAL COMPUTATION

Endah Purwanti, S.Si., M.T.


Osmalina Nur Rahma, S.T., M.Si.
Alfian Pramudita Putra, S.T., M.Sc.

BIOMEDICAL ENGINEERING STUDY PROGRAM


DEPARTMENT OF PHYSICS
FACULTY OF SCIENCE AND TECHNOLOGY
UNIVERSITAS AIRLANGGA

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

Preface: Rules of the Class


Presence
1. The practical class should be followed by all student with 0% absence from all
classes. If someone does not meet this requirement, so he/she failed this class.
2. The absence due to ill or leave should be equipped by an official letter and it should
be handed in to the lecturer or lab assistants beforehand.
3. All the students should be in the class, by the latest 15 minutes after the schedule
starts.

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

Chapter 1. Introduction to Python: Core Python


Variables
Variable represent a value of given type in most computer languages, including Python.
The following are examples in inputting variables in the Python (>>> is the Python
prompt):

>>> a = 5 # a in integer type

>>> print (a)

>>> b = a*2.0 # b is flow type

>>> print (b)

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.

>>> print a # for Python 2.7 (and before)

Strings
A string is a sequence of characters that is located inside the single or double quotation
marks. Here is an example:

>>> text1 = ‘Biomedical Engineers’ #using a single quote

>>> text1 = “Biomedical Engineers” #using a double quote

>>> print (text1)

Biomedical Engineers

or it could be written as:

>>> print (‘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.

>>> text2 = ‘Universitas Airlangga’

>>> print (text1 + ‘ ’ + text2) # Concatenation

Biomedical Engineers Universitas Airlangga

>>> print (text1 [0:10]) # Slicing

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:

>>> a = [4.0, 5.0, 6.0] # Create a list


>>> a.append (7.0) # Append 4.0 to list
>>> print (a)
[4.0, 5.0, 6.0, 7.0]
>>> a.insert (0, 3.0) # Insert 0.0 in position 0
>>> print (a)
[3.0, 4.0, 5.0, 6.0, 7.0]
>>> print len (a) # Determine length of list
5
>>> a [2:4] = [2.0, 2.0, 2.0] # Modify selected elements
>>> print a
[3.0, 2.0, 2.0, 2.0, 2.0, 7.0]

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 = [4.0, 5.0, 6.0]


>>> b = a # ‘b’ is new reference of ‘a’
>>> b [0] = 7.0 # Change ‘b’
>>> print (a)
[7.0, 4.0, 5.0, 6.0] # the change is reflected in ‘a’
>>> c = a [:] # ‘c’ is an independent copy of ‘a’
>>> c [0] = 5.0 # Change ‘c’
>>> print (a)
[7.0, 4.0, 5.0, 6.0] # ‘a’ is not affected by the change

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:

>>> a = [[1, 2, 3], [4, 5, 6]]


>>> print a [1] # Print second row (element 1)
[1, 2, 3]
>>> print a [0][1] # Print second element of first row
2

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:

>>> text1 = ‘ Biomedical’


>>> print (3*text1) # Repetition
Biomedical Biomedical Biomedical
>>> num = [1, 2, 3]
>>> print (2*num) # Repetition
[1, 2, 3, 1, 2, 3]
>>> print (num + [7, 8]) # Append elements
[1, 2, 3, 7, 8]

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:

def if_statement (a): # Defining function


if a < 0.0 :
text = 'negative'
elif a > 0.0:
text = 'positive'
else:
text = 'zero'
return text

a = -2.5
print ('a is '+ if_statement (a))

The result of running the program is:

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 output of the program is


[ 0.0, 0.2, 0.4, 0.6, 0.8 ]

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

list = [ ‘Ana , ‘Bona’, ‘Caca’, ‘Dona’ ]


name = eval (input ( 'Type a name: ' )) # Python input prompt
for i in range (len (list)):
if list [i] == name:
print (name, 'is number' , i + 1, 'on the list' )
break
else:
print (name, 'is not on the list' )

The result of running the program twice using the different name:
Type a name: ‘Dona’
Dona is number 4 on the list

Type a name: ‘Toni’


Toni is not 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.

num = [ ] # Create an empty list


for i in range (1,10):
if i % 2 != 0 : continue # if not an even number, skip the rest of loop
num.append (i) # Append I to the list
print (num)

The printout from the program:


[ 2, 4, 6, 8 ]

6
INTRODUCTION TO PYTHON

Functions and Modules


Defining a function had been appeared in the previous example by using def (param1,
param2, …) statement, where param1, param2, … are the parameters.

Modules contain of useful functions which can be loaded into a program by the statement:

from module_name import * (* ) means to import all function in the module

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:

>>> import math

>>> dir (math)

[’__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’]

>>> import numpy

>>> dir (numpy)

[’complex’, ’float’, ’abs’,’append’,‘arccos’, ’arccosh’, ’arcsin’, ’arcsinh’, ’arctan’, ’arctan2’,


’arctanh’, ’argmax’, ’argmin’, ’cos’, ’cosh’, ’diag’, ’diagonal’, ’dot’, ’e’, ’exp’, ’floor’, ’identity’,
’inner, ’inv’, ’log’, ’log10’, ’max’, ’min’, ’ones’, ’outer’, ’pi’, ’prod’ ’sin’, ’sinh’, ’size’, ’solve’, ’sqrt’,
’sum’, ’tan’, ’tanh’, ’trace’, ’transpose’, ’zeros’, ’vectorize’, …more]

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:

>>> from numpy import array

>>> a = array ( [ [ 1, 2, 3 ], [ 1, 2, 3 ] ] ) # Creating an array

7
INTRODUCTION TO PYTHON

>>> print (a)

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

>>> from numpy import *


>>> a = ones ( ( 3, 3), dtype = int )
>>> print (a)
[[1 1 1]
[1 1 1]
[ 1 1 1 ]]
>>> a [ 0 ] = [ 2, 5, 7] # Change a row
>>> a [ 1, 1 ] = 3 # Change an element
>>> a [ 2, 0 : 2 ] = [ -2, 6 ] #change part of a row
>>> print (a)
[[ 2 5 7 ]
[1 3 1]
[-2 6 1 ]]

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:

>>> from numpy import *


>>> a = array ([ 1.0, 2.0, 3.0, 4.0 ])
>>> print (a/2)
[ 0.5 1. 1.5 2. ]
>>> print (sqrt (a))
[ 1. 1.41421356 1.73205081 2. ]
>>> print (cos (a))

8
INTRODUCTION TO PYTHON

[ 0.54030231 -0.41614684 -0.9899925 -0.65364362 ]


>>> print (sqrt ( a [3] )
2.0

Array Function
NumPy provides numerous function that perform in array operations. The examples are
shown as below:

>>> from numpy import *


>>> a = array ( [ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ] , dtype = float )
>>> b = array ( [ 1, 2, 3 ], dtype = float )
>>> print (diagonal (a)) # Principal diagonal
[ 1. 2. 3. ]
>>> print (diagonal ( a, 1 )) # First sub diagonal
[ 2. 3. ]
>>> print (trace (a)) # Sum of diagonal elements
6.0
>>> print (argmax (b)) # Index of largest element
2
>>> print (argmin (a, axis = 0)) # Index of smallest col. Elements
[0 0 0]
>>> print (identity (3)) # Identity matrix
[ [ 1. 0. 0. ]
[ 0. 1. 0. ]
[ 0. 0. 1. ] ]

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:

>>> from numpy import array


>>> from numpy.linalg import inv,solve
>>> A = array ( [ [ 4.0, -2.0, 1.0], \
[-2.0, 4.0, -2.0], \
[ 1.0, -2.0, 3.0] ] )
>>> b = array ( [ 1.0, 4.0, 2.0 ] )
>>> print inv (A) # Matrix inverse
[ [ 0.33333333 0.16666667 0. ]
[ 0.16666667 0.45833333 0.25 ]
[ 0. 0.25 0.5 ] ]
>>> print solve ( A, b ) # Solve [A]{x} = {b}
[ 1. , 2.5, 2. ]

Task
1. Display the even number from zero to 100 and calculate the sum of it !

2. Calculate the value of 10! (10 factorial) !

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

Chapter 2. Finding-Root Method: Bisection & Regula-Falsi

Aims and Objectives


To determine the roots of function by using Bisection and Regula-Falsi method.

Preliminary Task
If there is a function as follows:

𝑓(𝑥) = 𝑥 − 2 cos 𝑥 [0, 𝜋/2]

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

Figure 1. Bisection Method for Finding Roots of a Function

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.

Figure 2. Regula-Falsi method for Finding Root of a Function

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

2. Analyze the advantages and disadvantages of both methods!

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.

3. Patel VA, 1994, “Numerical Analysis”, Saunders College Publishing.

15
FINDING-ROOT METHOD: Newton-Raphson

Chapter 3. Finding-Root Method: Newton-Raphson

Aims and Objectives


To determine the roots of function by using Newton-Raphson method.

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)

2. Define the error tolerance (e)

3. Define the initial approximation x0

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!

Osteoporosis in Chinese Woman. Wu te la. (2008) studied the variations in age-


related speed of sound (SOS) at the tibia and the prevalence of osteoporosis in native
Chinese women. They obtained the following relationship between the SOS and the age
in year, Y.

𝑆𝑂𝑆 = 3383 + 39.9𝑌 − 0.78𝑌 2 + 0.0039𝑌 3

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.

3. Patel VA, 1994, “Numerical Analysis”, Saunders College Publishing.

17
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION

Chapter 4. System of Linear Equation: Gauss Elimination

Aims and Objectives


To determine the result of linear equation system by using Gauss Elimination method.

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

Elementary Row Operations

There are three operations that could be performed towards linear equation systems
without changing the real solution, which are:

1. Rearrange the order of the equation.

2. Multiplication an equation by a non-zero number.

3. Change an equation by adding that equation with a multiplication of another equation.

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.

1. Forming a full matrix of linear equation system

2. Changing the full matrix to echelon matrix with several elementary row oerations

3. Obtaining the solution of linear equation system.

As example, if several equations like below are known, then:

a11x + a12y + a13z = b1

a21x + a22y + a23z = b2

a31x + a32y + a33z = b3

The initial matrix:


𝑎11 𝑎12 𝑎13 𝑥 𝑏1
[𝑎21 𝑎22 𝑎23 ] [𝑦] = [𝑏2 ]
𝑎31 𝑎32 𝑎33 𝑧 𝑏3

The linear equation system:


𝑎11 𝑎12 𝑎13 𝑏1
𝑎
( 21 𝑎22 𝑎23 |𝑏2 )
𝑎31 𝑎32 𝑎33 𝑏3

By using elementary row operations, an echelon matrix is obtained.

z = b3’

y + a23z = b2’ → y = b2’ - a23z

x + a12y + a13z = b1’ → x = b1’ - a12y - a13z

19
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION

Algorithm
1. Define matrix a, matrix b and the order of the matrix (n).

2. Take the coefficients of the linear equation as:

Do for k = 1 to n

Do for i = k+1 to n

aik = aik / akk

Do for j = k+1 to n

aij = aij – aik akj

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!

Pharmacokinetic modelling for ”animal-on-chip”

A material balance is performed for naphthalene epoxide (NO) generation, consumption,


and transport in the μCCA device described in Figure 4.1; NO is an intermediate formed
during the metabolism of naphthalene.

20
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION

Routes of generation of naphthalene epoxide:

(1) conversion of naphthalene into its epoxide.

Routes of consumption of naphthalene epoxide:

(1) conversion of epoxide to naphthalene dihydrodiol;

(2) binding to GSH to form epoxide–GSH conjugates;

(3) rearrangement to naphthol.

Figure 4.1. Material balance diagram for naphtalene epoxide

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

1. Binding of naphthalene epoxide to proteins is comparatively less important and can be


neglected.
2. The concentration of GSH in cells is constant. It is assumed that GSH is resynthesized
at the rate of consumption.
3. Production of the RS enantiomer of the epoxide (compared to SR oxide) is dominant,
and hence reaction parameters pertaining to RS production only are used.
4. The total protein content in the cells to which the metabolites bind remains constant.

The parametric values and definitions are provided below. The modeling parameters
correspond to naphthalene processing in mice.

Flowrates

Qlung: flowrate through lung compartment = 2 μl/min;

Qliver: flowrate through liver compartment = 0.5 μl/min;

Qot: flowrate through other tissues compartment = 1.5 μl/min.

Compartment volumes

Vlung: volume of lung compartment = 2 mm ×2mm × 20 μm = 8 ×10l = 0.08μl;

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

(1) Naphthalene → naphthalene epoxide

vmax,P450-lung: maximum reaction velocity for conversion of naphthalene into napthalene


epoxide by cytochrome P450 monooxygenases in lung cells = 8.75 μM/min;

vmax,P450-liver: maximum reaction velocity for conversion of naphthalene into napthalene


epoxide by cytochrome P450 monooxygenases in liver cells = 118 μM/min.

(2) Naphthalene epoxide → naphthalene dihydrodiol

vmax,EH-lung: maximum reaction velocity for conversion of naphthalene epoxide to


dihydrodiol by epoxide hydrolase in the lung = 26.5 μM/min;

Km,EH-lung: Michaelis constant = 4.0 μM;

vmax,EH-liver: maximum reaction velocity for conversion of naphthalene epoxide to


dihydrodiol by epoxide hydrolase in the liver = 336 μM/min;

Km,EH-lung: Michaelis constant = 21 μM

(3) Naphthalene epoxide → naphthol

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

(4) Naphthalene epoxide → epoxide–GSH conjugates

vmax, GST: maximum reaction velocity for binding of naphthalene epoxide to GSH catalyzed
by GST (glutathione S-transferase) = 2750 μM/min;

K1lung: constant in epoxide–GSH binding rate = 310 000 μM2;

K2lung: constant in epoxide–GSH binding rate = 35 μM;

K1liver: constant in epoxide–GSH binding rate = 150 000 μM2;

K2liver: constant in epoxide–GSH binding rate = 35 μM.

23
SYSTEM OF LINEAR EQUATION: GAUSS ELIMINATION

Protein concentrations

TPlung: total protein content in lung compartment = 92 mg/ml;

TPliver: total protein content in liver compartment = 192 mg/ml;


𝐺𝑆𝐻
𝐶𝑙𝑢𝑛𝑔 : GSH concentration in lung compartment = 1800 μM;
𝐺𝑆𝐻
𝐶𝑙𝑖𝑣𝑒𝑟 : GSH concentration in liver compartment = 7500 μM;

R: fraction of the exiting stream that reenters the microcircuit.

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.

a. Use the Gaussian elimination method to determine the napthalene epoxide


concentrations at the outlet of the lung and liver compartments of the animalon-a-chip
for the range of R specified.

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.

3. Patel VA, 1994, “Numerical Analysis”, Saunders College Publishing.

24
SYSTEM OF LINEAR EQUATION: Jacobi & Gauss Seidel Iteration

Chapter 5. System of Linear Equation: Jacobi & Gauss Seidel Iteration

Aims and Objectives


To determine the solutions of linear equation system by using Jacobi iteration and Gauss-
Seidel iteration method.

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.

The general form of linear equations are shown below.

a11 x1 + a12 x2…………. + a1n xn = b1

a21 x1+ a22 x2………….. + a2n xn = b2

a21 x1+ a32 x2………….. + a3n xn = b3

………………………………………………

an1 x1 + an2 x2…………. + ann xn = bn

with akk ≠ 0 , k=1,2,……,n then the iteration equation could be written as follows.

𝑏𝑛 −(𝑎𝑛1 𝑥1𝑘 +𝑎𝑛2 𝑥2𝑘 +⋯+𝑎𝑛𝑛−1 𝑥𝑛−1


𝑘 )
𝑥𝑛𝑘+1 = (5.1)
𝑎𝑛𝑛

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

The requirement to get a convergent iteration is |𝑎𝑖𝑖 | > ∑𝑛𝑗=1,𝑗≠𝑖 |𝑎𝑖𝑗 |

1. Jacobi Iteration Method

If the initial guess is X (0),

X (0) = (x1(0), x2(0), … , xn (0))

then the iteration procedure is defined by using the following equation.

𝑏𝑖 −∑𝑛 𝑘
𝑗=𝑖,𝑗≠𝑖 𝑎𝑖𝑗 𝑥𝑗
𝑥𝑖𝑘+1 = , k=0, 1, 2, … (5.3)
𝑎𝑖𝑖

2. Gauss-Seidel Iteration Method

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
𝑖

7. For i = 1 to n, Do the steps 7 to 13

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

14. Print (k, x1, x2, … , xn)


15. k=k+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.

Amounts (gr) supplied per 100 gr of ingredients


Amounts (gr) supplied by
Non-fat Soy
Nutrient Whey Cambridge Diet in One Day
milk flour
Protein 36 51 13 33
Carbohydrate 52 34 74 45
Fat 0 7 11 3
Please find how much non-fat milk-soy flour, and whey that are needed to fulfill the
amounts of protein, carbohydrate, and fat each day ideally?

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

3. Munir R, 2003, “Metode Numerik”, Informatika Bandung.

28
REGRESSION: Linear & Polynomials

Chapter 6. Regression: Linear & Polynomials

Aims and Objectives


To define the correlation of variables in a function by using regression method.

Preliminary Task
There are data of variable x and y, such as:

A. x=[1, 3, 5, 7, 10, 12, 13, 16, 18, 20]


y=[3, 2, 6, 5, 8, 7, 10, 8, 12, 10]

B. x=[0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3]


y=[0.003, 0.067, 0.148, 0.248, 0.370, 0.518, 0.697]

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

Linear regression is a regression method that produces a function as a correlation between


two linear dependent variables. As example, xi and yi are the data from measurement. We
would track those data by using a straight line. That straight line was made so that the
error is relatively small. That process was illustrated in Figure 6.2.

Figure 6.2. Linear Regression

Mathematically, the linear function is written as follows.

𝑓(𝑥) = 𝑎 + 𝑏𝑥

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

2. Define the length of the data n of 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 

10. Plot the linear regression with a function = ax + 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

2. Using hemoglobin as a blood substitute: hemoglobin–oxygen binding


Hemoglobin (Hb) is a protein present in red blood cells that is responsible for the
transport of oxygen (O2) from lungs to individual tissues throughout the body and
removal of carbon dioxide (CO2) from the tissue spaces for transport to the lungs. The
hemoglobin molecule is a tetramer and consists of four subunits, two α chains, and two
β chains. Each α or β polypeptide chain contains a single iron atom containing heme
group that can bind to one O2 molecule. Thus, a hemoglobin molecule can bind up to
four O2 molecules. The subunits work cooperatively with each other (allosteric binding),
such that the binding of one O2 molecule to one of the four subunits produces a
conformational change within the protein that makes O2 binding to the other subunits
more favorable. The binding equation between hemoglobin and oxygen is as follows:
𝐻𝑏(𝑂2 )𝑛 ↔ 𝐻𝑏 + 𝑂2 ; where n = 1, …, 4
The exchange of O2 and CO2 gases between the lungs and tissue spaces via the blood occurs
due to prevailing differences in the partial pressures of pO2 and pCO2, respectively. The
atmospheric air contains 21% O2. The O2 in inspired air exerts a partial pressure of 158
mm Hg (millimeters of mercury), which then reduces to 100 mm Hg when the inspired
air mixes with the alveolar air, which is rich in CO2. Venous blood that contacts the
alveoli contains O2 at a partial pressure of 40 mm Hg. The large difference in partial
pressure drives diffusion of O2 across the alveolar membranes into blood. Most of the
oxygen in blood enters into the red blood cells and binds to hemoglobin molecules to
form oxyhemoglobin. The oxygenated blood travels to various parts of the body and
releases oxygen from oxyhemoglobin. The partial pressure of oxygen in the tissue
spaces depends on the activity level of the tissues and is lower in more active tissues.
The high levels of CO2 in the surrounding tissue drive entry of CO2 into blood and
subsequent reactions with water to produce bicarbonates (HCO3-). Some of the
bicarbonates enter the red blood cells and bind to hemoglobin to form
carbaminohemoglobin. At the lungs, the oxygenation of blood is responsible for the
transformation of carbaminohemoglobin to oxyhemoglobin or the dissociation of CO2

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

And then taking the logarithm of both sides to obtain:

𝑆
𝑙𝑛 = 𝑛 𝑙𝑛(𝑝𝑂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.

Tabel 1. Fractional saturation of hemoglobin as a function of partial pressure of oxygen


in blood
pO2 10 20 30 40 50 60 70 80 90 100 110 120
(mmHg)
S 0.18 0.40 0.65 0.80 0.87 0.92 0.94 0.95 0.95 0.96 0.96 0.97

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.

3. Make a polynomial regression program to solve a nonlinear data problem.

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

Chapter 7. Interpolation Lagrange

Aims and Objectives


To determine an interpolation based on a given data.

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)

2. Define the dots Pi(xi,yi) with i=1, 2, 3, …, n

3. Define the input x

4. Calculate the y by using the Lagrange interpolation equation:


𝑁
(𝑥 − 𝑥𝑗 )
𝑦 = ∑ 𝑦𝑖 ∏
(𝑥𝑖 − 𝑥𝑗 )
𝑖=1 𝑗≠𝑖

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!

4. What is the difference of linear regression and Lagrange interpolation?

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

Chapter 8. Numerical Derivative: Finite Difference Approximations

Aims and Objectives


To determine a numerical derivative using forward, backward and central difference
approximation.

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.

a. Forward difference approximation


𝑓(𝑥0 + ℎ) − 𝑓(𝑥0 ) 𝑓1 − 𝑓0
𝑓 ′ (𝑥0 ) = =
ℎ ℎ
b. Backward difference approximation
𝑓(𝑥0 ) − 𝑓(𝑥0 − ℎ) 𝑓0 − 𝑓1
𝑓 ′ (𝑥0 ) = =
ℎ ℎ
c. Central difference approximation
𝑓(𝑥0 + ℎ) − 𝑓(𝑥0 − ℎ) 𝑓1 − 𝑓−1
𝑓 ′ (𝑥0 ) = =
2ℎ 2ℎ

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)

2. Define the value of h

3. Define the value of x that would be calculated, like x0

4. Calculate f(x0 - h), f(x0), and f(x0 + h)

5. Calculate f '(x0) by using forward, backward and central difference approximation

6. Show the derivative value of every approximation.

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

Chapter 9. Numerical Integration: Trapezoid & Simpson’s Rule

Aims and Objectives


To determine a numerical integration using Trapezium and Simpson 1/3 method!

Preliminary Task
If there is function as follows:

𝑓(𝑥) = 𝑥 2 cos 𝑥 2 , 1.5 ≤ 𝑥 ≤ 2.5 and h =0,1.

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.

Figure 9.1. Rectangular method for numerical integration

39
NUMERICAL INTEGRATION

The area of one rectangle is

Those equations could be simplified as follows.

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.

Figure 9.2. Trapezoid method for numerical integration

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.

Figure 9.3. Simpson 1/3 Method for numerical integration

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!

3. Polymer cyclization (Jacobson– Stockmayer theory)


Polymer chains sample a large number of different orientations in space and time.
The motion of a polymer chain is governed by stochastic (probabilistic) processes.
Sometimes, the two ends of a linear polymer chain can approach each other within
a reactive distance. If a bond forms between the two polymer ends, the reaction is
termed as cyclization. By studying the probability with which this occurs one can
estimate the rate at which a linear chain is converted to a circular chain. Consider a
linear polymer with N links. The probability that the two ends of the chain come
within a bond distance b of each other is given by the following integral:
3⁄ 𝑏
3 2 −3𝑟 2
[ ] ∫ exp ( ) 4𝜋𝑟 2 𝑑𝑟.
2𝜋𝑁𝑏 2 0 2𝑁𝑏 2

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

Chapter 10. Richardson Extrapolation: Derivatives

Aims and Objectives


To determine a numerical derivative using Richardson extrapolation and knowing its
strengths and weaknesses compare to other numerical derivative methods.

Preliminary Task
Given the data points:

x 2.0 2.1 2.2 2.3 2.4 2.5


f(x) 0.42298 0.40051 0.37507 0.34718 0.31729 0.28587

x 2.6 2.7 2.8 2.9 3.0


f(x) 0.25773 0.22008 0.18649 0.15290 0.11963
Determine f '(2.5) by extrapolating Richardson if D(h) and D(2h) are calculated by the
formula of the center-difference order O(h2) to 5 significant figure!

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 + ⋯ (10.1)


( 𝑓2 −𝑓−2 )
𝐷(2ℎ) = 2(2ℎ)
+ 𝑂((2ℎ)2 )

= 𝑓0′ + 𝐶(2ℎ)2 + ⋯

= 𝑓0′ + 4𝐶ℎ2 + ⋯ (10.2)

45
RICHARDSON EXTRAPOLATION: Derivatives & Integration

From equation (10.1) and (10.2), we could obtain:

𝐷(ℎ) − 𝐷(2ℎ) = −3𝐶ℎ2 (10.3)


𝐷(ℎ)−𝐷(2ℎ)
𝐶= (10.4)
−3ℎ2

Distribute (10.4) into equation (10.1):


[𝐷(ℎ)−𝐷(2ℎ)]ℎ2
𝐷(ℎ) = 𝑓0′ + −3ℎ2

= 𝑓0′ − 1⁄3 [𝐷(ℎ) − 𝐷(2ℎ)] (10.5)

or

𝑓0′ = 𝐷(ℎ) + 1⁄3 [𝐷(ℎ) − 𝐷(2ℎ)] (10.6)

Richardson extrapolation can be expanded to improve derivative functions. Based on


equation (10.6) the above rules can be written:
1
𝑓0′ = 𝐷(ℎ) + 2𝑛−1 [𝐷(ℎ) − 𝐷(2ℎ)] (10.7)

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.

2. Determine the length of data n.

3. Determine the order of desired value in the data x.

4. Define h.
( 𝑓1 −𝑓−1 )
5. Calculate 𝐷(ℎ) = 2ℎ

6. Define 2h

46
RICHARDSON EXTRAPOLATION: Derivatives & Integration

( 𝑓2 −𝑓−2 )
7. Calculate 𝐷(2ℎ) = 2(2ℎ)

8. Calculate the length of D(2h)

9. for k in range 0 to length D(h)-1


1
Calculate 𝑓0′ = 𝐷(ℎ)𝑘 + 2𝑛−1 [𝐷(ℎ)𝑘 − 𝐷(2ℎ)𝑘+1 ]

10. Repeat step 4-9, depend on the order error O(hn)

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

Chapter 11. Richardson Extrapolation: Integration

Aims and Objectives


To determine a numerical integration using Richardson extrapolation and knowing its
strengths and weaknesses compare to other numerical integration methods.

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)

which can be written as:


𝑏
∫𝑎 𝑓(𝑥)𝑑𝑥 = 𝐼(ℎ) + 𝐶ℎ2 (11.2)
(𝑏−𝑎)𝑓′′(𝑡)
where I(h) is the integration result using trapezoidal rule with 𝐶 = .
12

Generally, integration method can be written as:


𝑏
∫𝑎 𝑓(𝑥)𝑑𝑥 = 𝐼(ℎ) + 𝐶ℎ𝑞 (11.3)

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:

Trapezoidal method has the error order O(h2), so the q is 2

Midpoint method has the error order O(h2), so the q is 2

Simpson 1/3 method has the error order O(h4), so the q is 4

48
RICHARDSON EXTRAPOLATION: Derivatives & Integration

Richardson’s extrapolation technique is possible to combine the numerical results of


integration method for two different step sizes, h1 and h2, to obtain a third numerical result,
I3 = f (I1, I2). Extrapolation technique is much more accurate than the two approximations

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:

𝐽 = 𝐼(ℎ) + 𝐶(ℎ𝑞 ) (11.4)

𝐽 = 𝐼(2ℎ) + 𝐶(2ℎ𝑞 ) (11.5)

Eliminate C from both equation

𝐼(ℎ) + 𝐶(ℎ𝑞 ) = 𝐼(2ℎ) + 𝐶(2ℎ𝑞 ) (11.6)

Thus, we obtain:
𝐼(ℎ)−𝐼(2ℎ)
𝐶= (2𝑞 −1)ℎ𝑞
(11.7)

If we substitute (11.7) into (11.4), the Richardson extrapolation formula becomes:


𝐼(ℎ)−𝐼(2ℎ)
𝐽 = 𝐼(ℎ) + (11.8)
2𝑞 −1

When Richardson extrapolation technique is applied to numerical integration repeatedly


to improve the accuracy of each successive layer of approximation, the scheme is called
Romberg integration.

Romberg Integration is an extended of Richardson extrapolation combined with


Trapezoidal method to obtain a better integration result. In every application of
Richardson will increase the order of errors in the solution by two:

𝑂(ℎ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:

𝐼 = 𝐴𝑘 + 𝐶ℎ2 + 𝐷ℎ4 + 𝐸ℎ6 + ⋯ (11.9)

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.

Overall, the Romberg integration is illustrated as below:

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

2. Define the step size (h1) = h

3. Calculate the integral results (I(h)) using integration method (such as Trapezoidal
method) for step size h

4. Define the different step size (h2) = 2h

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)

2. Define the step size (h1) = h

3. Calculate the integral results (I(h)) using integration method (such as Trapezoidal
method) for step size h

4. Define the different step size (h2) = 2h

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

7. Repeat step 3 to 6 to as many as the number of layer (k)

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)

Chapter 12. ODE: Euler’s method & Heun’s Method


Aims and Objectives
To determine the solution of ordinary differential equation using Euler and Heun’s Method.

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

where N is a positive integer and 𝑡𝑖 = 𝑎 + 𝑖ℎ, i = 0, 1, 2, …, N.

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

By entering ℎ = (𝑡𝑖+1 − 𝑡𝑖 )𝑟 , then

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:

𝑤𝑖+1 = 𝑤𝑖 + ℎ 𝑓(𝑡𝑖 , 𝑤𝑖 ), with initial condition 𝑤0 = 𝛼 (12.5)

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

1. Import module and define the function.

2. Determine the interval and the initial condition.

3. Define the step size h

4. Calculate the Euler’s equation: 𝑤𝑖+1 = 𝑤𝑖 + ℎ 𝑓(𝑡𝑖 , 𝑤𝑖 )

54
ORDINARY DIFFERENTIAL EQUATION (ODE)

5. Calculate the exact value

6. Determine the error value

Heun’s Method

1. Import module and define the function.

2. Determine the interval and the initial condition.

3. Define the step size h

4. Calculate the predictor equation:


0
𝑤𝑖+1 = 𝑤𝑖 + ℎ 𝑓(𝑡𝑖 , 𝑤𝑖 )
′ 0
𝑤𝑖+1 = 𝑓(𝑡𝑖+1 , 𝑤𝑖+1 )
0
𝑓(𝑡𝑖 ,𝑤𝑖 )+𝑓(𝑡𝑖+1 ,𝑤𝑖+1 )
5. Calculate the corrector equation: 𝑤𝑖+1 = 𝑤𝑖 + ℎ 2

6. Calculate the exact value

7. Determine the error value

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:

𝑞𝑒𝑥𝑎𝑐𝑡 = 𝑞(𝑡) = 𝐶𝜖(1 − 𝑒 −𝑡/𝑅𝐶 )

55
ORDINARY DIFFERENTIAL EQUATION (ODE)

Figure 12.1 RC Circuit

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/𝑑𝑎𝑦

The initial concentrations of T, T*, V1, and VX are as follows:

V1(t = 0) =100/μl;

VX (t = 0) = 0/μl;

T (t = 0) = 250 non-infected cells/μl (Haase et al., 1996);

T*(t = 0) = 10 infected cells/μl (Haase et al., 1996).

Based on a quasi-steady state analysis (dT*/dt = 0, dV/dt = 0) before time t = 0, we


calculate the following:

k = 2𝑥10−4 μl/day/virions, and

N = 60 virions produced per cell.

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)

Chapter 13. ODE: Runge-Kutta’s Method

Aims and Objectives


To determine the solution of ordinary differential equation using Runge-Kutta’s method
and knowing its strengths and weaknesses compare to the previous methods.

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)

Φ = a1k1 + a2 k 2 + ... + an k n (13.2)

where a is a constant and k is defined in equation (13. 3) to (13.6)

k1 = f (xi, yi) (13.3)

k2 = f (xi + p1x, yi + q11 k1x) (13.4)

k3 = f (xi + p2x, yi + q21 k1x + q22 k2x) (13.5)

58
ORDINARY DIFFERENTIAL EQUATION (ODE)

kn = f (xi + pn – 1x, yi + qn – 1, 1 k1x + qn – 1, 2 k2x + + qn – 1, n – 1 kn – 1x) (13.6)

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.

For n = 1, so-called first order Runge-Kutta, Equation (13.1) becomes

Φ = a1k1 = a1 f ( xi , yi ) (13.7)

For a1 = 1, Equation (13.1) becomes

yi + 1 = yi + f ( x i , yi ) Δx (13.8)

That is similar with Euler method.

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.

Second Order Runge-Kutta Method

The second order Runge-Kutta method has a form shown in Equation (13.9).

yi + 1 = yi + (a1k1 + a2 k2 )Δx (13.9)

where:

k1 = f ( xi , yi ) (13.10)

k 2 = f ( xi + p1 Δx, yi + q11k1Δx) (13.11)

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

The substitution of Equation (13.13) to Equation (13.12) produces Equation (13.14).

Δ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

Equation (13.11) could be re-written as follows.

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

Fourth Order Runge-Kutta 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)

𝑘4 = ∆𝑥 . 𝑓(𝑥𝑖 + ∆𝑥, 𝑦𝑖 + 𝑘3 ∆𝑥) (13.26)

61
ORDINARY DIFFERENTIAL EQUATION (ODE)

Algorithm
1. Import module and define the function.

2. Determine the interval and the initial condition.

3. Define the step size h

4. Calculate the predictor equation:

𝑘1 = ℎ ∗ 𝑓(𝑥𝑖 , 𝑦𝑖 )
1 1
𝑘2 = ℎ ∗ 𝑓(𝑥𝑖 + 2 ∆𝑥, 𝑦𝑖 + 2 𝑘1 ∆𝑥)
1 1
𝑘3 = ℎ ∗ 𝑓(𝑥𝑖 + 2 ∆𝑥, 𝑦𝑖 + 2 𝑘2 ∆𝑥)

𝑘4 = ℎ ∗ 𝑓(𝑥𝑖 + ∆𝑥, 𝑦𝑖 + 𝑘3 ∆𝑥)


1
5. Calculate the corrector equation: 𝑦𝑖+1 = 𝑦𝑖 + 6 (𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4 )

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)

Chapter 14. Partial Differential Equation: Forward

Aims and Objectives


To determine the solution of partial differential equation (PDE) by reduce it to a system of
ODE’s using finite difference method.

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

With boundary condition:

𝑢(0, 𝑡) = 𝑢(1, 𝑡) = 0, 0 < 𝑡,

and initial condition:

𝑢(𝑥, 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)

Introduce a spatial mesh in Ω with mesh points.

𝑥0 = 0 < 𝑥1 < 𝑥2 < ⋯ < 𝑥𝑁 = 𝐿

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:

𝜕𝑢(𝑥𝑖 ,𝑡) 𝜕2 𝑢(𝑥𝑖 ,𝑡)


=𝛽 + 𝑔(𝑥𝑖 , 𝑡) , 𝑖 = 1, … , 𝑁 − 1 (14.2)
𝜕𝑡 𝜕𝑥 2

Now, at any point xi we can approximate the second-order derivative by a finite difference:

𝜕2 𝑢(𝑥𝑖 ,𝑡) 𝑢(𝑥𝑖+1 ,𝑡)−2𝑢(𝑥𝑖 ,𝑡)+𝑢(𝑥𝑖−1 ,𝑡)


≈ (14.3)
𝜕𝑥 2 ∆𝑥 2

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

That is, we have a special version of (14.4) at the boundary i = N.

A summarize in approximating the partial differential equation problem (14.2) to (14.6)


by an ODE’s system is:
𝑑𝑢0
= 𝑠′(𝑡) (14.7)
𝑑𝑡

𝑑𝑢𝑖 𝛽
𝑑𝑡
=
∆𝑥 2
(𝑢𝑖+1 (𝑡) − 2𝑢𝑖 (𝑡) + 𝑢𝑖−1 (𝑡)) + 𝑔𝑖 (𝑡) , 𝑖 = 1, … , 𝑁 − 1 (14.8)

𝑑𝑢𝑁 2𝛽
𝑑𝑡
=
∆𝑥 2
(𝑢𝑁+1 (𝑡) − 𝑢𝑁 (𝑡)) + 𝑔𝑖 (𝑡) (14.9)

The initial conditions are

𝑢0 (0) = 𝑠(0) (14.10)

𝑢𝑖 (0) = 𝐼(𝑥𝑖 ) , 𝑖 = 1, … , 𝑁 − 1 (14.11)

Algorithm
1. Discretize solution u(x, t) into discrete values

2. Define the step size h

3. Define the initial and boundary conditions.

4. Compute g(xi, ti) using finite differences than compute u(xi, ti) based on g(xi, ti)

5. Plot the solution from i to i + 1.

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

You might also like