You are on page 1of 125

MBE2036 Engineering Computing, part 4, revision 5, Dr. Y.

Shen 4

Error
True error True Error = True value Approximation
True value - Approxima tion
Relative error t 100% Eq 3.1
True value

Bridge Screw

Measured length= 9,999 cm Measured length= 9 cm


True length= 10,000 cm True length = 10 cm
True error = 10,000-9,999=1cm True error = 10-9=1cm

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 4, revision 5, Dr. Y. Shen 5

Relative error in numerical computing

The approximate percent relative error:


present approximat ion - previous approximat ion
a 100% Eq 3.3
present approximat ion

Stopping criterion:
a s Eq 3.4
where s is a stopping criterion.

At least n significant figures, if

s (0.5 102n )% Eq 3.5


Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 4, revision 5, Dr. Y. Shen 18

Taylor series f '' ( x i ) 2 f ( 3 ) ( x i ) 3


f ( xi 1 ) f ( xi ) f ( xi )h
'
h h ...
2! 3!
Example
f ( n ) ( xi ) n
n!
h Rn (Eq 4.1)

where the step size h xi 1 xi


f(x) = -0.1x4 0.15x3 0.5x2 0.25x +1.2
Given f(0)=1.2, find f(1) based on taylor series

f(x) = -0.4x3 0.45x2 x 0.25 0-order: f(1) = f(0)=1.2


1st-order: f(1) = f(0)+ f(0)h=1.2-0.25=0.95
f(x) = -1.2x2 0.9x 1
2nd-order: f(1) = f(0)+ f(0)h+f(0)h2/2!=0.45
f 3(x) = -2.4x 0.9
3rd-order: f(1) = f(0)+ f(0)h+f(0)h2/2!+f 3(0)h3/3!=0.3
f 4(x) = -2.4 4th-order: f(1) = f(0)+ f(0)h+f(0)h2/2!+f 3(0)h3/3!
f 5(x) = 0 + f 4(0)h4/4!=0.2
5th-order: f(1) = f(0)+ f(0)h+f(0)h2/2!+f 3(0)h3/3!
Step size: h=1-0=1 + f 4(0)h4/4!+f 5(0)h5/5!=0.2

f(1) = -0.1 0.15 0.5 0.25 + 1.2 = 0.2


Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 4, revision 5, Dr. Y. Shen 29

Estimating Truncation Errors


The finite difference approximation of first derivative

v (ti1 ) v (t i ) R1
v ' (t i ) Eq 4.8
(t i 1 t i ) (t i 1 t i )

The truncation error R1 v ' ' ( )


( t i 1 t i ) Eq 4.9
of the approximation: ( t i 1 t i ) 2!
R1
Or written as: ( t t ) O ( t i 1 t i ) Eq 4.10
i 1 i

Eq4.10 suggests that the truncation error for the above derivative
approximation is proportional to the step size h=ti+1 ti

Halving the step size would in principle halve the error of the derivative
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 4, revision 5, Dr. Y. Shen 30

Numerical Differentiation
Forward finite difference approximation - Eq 4.8
is the forward finite difference equation which
can be expressed in a more general format:
f ( xi1 ) f ( x i )
f ' ( xi ) O(h ), Eq 4.11, h ( x i 1 x i )
h

Backward finite difference approximation


Taylor series can be expanded backward:
f " ( xi ) 2
f ( x i 1 ) f ( x i ) f ' ( x i )h h ... Eq 4.12
2!
minus since h is negative
and f(xi) can be expressed
f ( xi ) f ( x i 1 )
i
f ' ( xDepartment
) of Mechanical and
O(h ), Eq 4.13
h
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 4, revision 5, Dr. Y. Shen 31

Numerical Differentiation
Centered finite difference approximation of the
first derivative:
f" (x i ) 2 f (3) (x i ) 3
f(x i1) f(x i ) f' (x i )h h h ... Eq 4.14
2! 3!
(3)
f" (x i ) 2 f (x i ) 3
f(x i1) f(x i ) f' (x i )h h h ... Eq 4.12
2! 3!

Eq 4.14 subtract Eq 4.12 yield


2f (3) (x i ) 3
f(x i1) f(x i1) 2f' (x i )h h ... Eq 4.15
3!
Which can be solved for:
f ( xi 1) f ( xi 1) f (3) ( xi ) 2
f ' ( xi ) h ... Eq 4.16
2h 3!
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 28

Example 1 - Bisection Method


Step 1: Check the sign: xU
xL

xL=12, xU=16 f(c)


f(12)= 6.0669 > 0
f (16)= -2.2688 < 0
c

So, we have: f (xL)f(xU)<0

It mean the root is between xL (12) and xu (16)

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 29

Example 1 - Bisection Method


Step 2: Calculate xr :
xL xr xU

x L xU
xr Eq 5.6
=14 f(c)
2
Step 3: Check the root:
We check whether the root is actually in the
middle, upper subinterval or lower subinterval:
c
1st Iteration f(xr)=f(14)= 1.5687 > 0 f(12)f(14) > 0

It indicates that there is no sign change between xL and the midpoint xr


Therefore, the root must lies in the upper subinterval, i.e between 14 (xr) and 16 (xu)

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 30

Example 1 - Bisection Method (cont)


Repeat Step 2 to calculate new xr, but with xL=old
xr (14): xL= 14 and xU=16
xL x
Old U
2nd Iteration
xr =(14+16)/2=15
New
15 - 14 f(c)
a 100 6.6667%
15

a > s therefore we continue Step 3:


f(14)f(15) = 1.5687*(-0.4248)< 0
Therefore, the root lies between 14 and 15 c
Repeat Step 2 with xU=xr xL= 14 and
xU=15
14.5 - 15
xr =(14+15)/2=14.5 a 100 3.4483%
3rd Iteration 14.5
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 31

Example 1 - Bisection Method (cont)

a > s therefore we continue Step 3:


f(14)f(14.5) =1.5687*0.5523=0.8664 >0
Repeat Step 2 with xL=xr xL= 14.5 and xU=15
4th Iteration xr =(14.5+15)/2=14.75

14.75 - 14.5
a 100 1.6949%
14.75

a > s therefore we continue Step 3.

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 32

Example 1 - Bisection Method (cont)


xL=12, xU=16
1st Iteration
12 14 16 xr=14

f (xL)f(xr)>0

xL=14, xU=16
2nd Iteration
12 14 16 xr=15
f (xL)f(xr)<0

xL=14, xU=15
3rd Iteration
12 14 16 xr=14.5
f (xL)f(xr)>0

xL=14.5, xU=15
4th Iteration
12 14 16 xr=14.75

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 33

Example 1 - Bisection Method (cont)

The result is as Iteration xL xU xr a (%)


shown in the 1 12 16 14
table on the
right 2 14 16 15 6.667

3 14 15 14.5 3.448

4 14.5 15 14.75 1.695

5 14.75 15 14.875 0.840

a < s so it stops 6 14.75 14.875 14.8125 0.422

The true root is 14.7802085

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 37

Error Analysis for Bisection Method (cont)


xrnew xrold
a 100% Eq 5.7 and a s
xrnew

xU xL
2 x xL
a 100% U 100% Eq 5.10
xU xL xU xL
2

Equation 5.10 produce the same result as Eq


5.7 for bisection method
Unlike Eq 5.7, Eq 5.10 can be used to calculate
a for the initial guesses

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 38

Error Analysis for Bisection Method (cont)

Besides the ability to analysis the exact upper bound of the true error
The bisection method also allows us to calculate the number of iterations
required to obtain an absolute error before starting the iterations
Before starting the technique, the absolute error is:
EA0=xU0 xL0 = x0
After the first iteration, the error becomes:
EA1 = x0/2
Because each succeeding iteration halves the error, a general formula
relating the error and the number of iterations, n, is:
x 0 This is the maximum error
E An Eq 5.11
2n
If EA,d is the desired error, the above equation can be written as:
x 0
log

x 0 x 0
E A,d
2n n log 2n log n
EA E A,d log 2
x 0
n log 2
Eq 5.12
E A,d
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 31

Example 1
Problem statement: Use the Secant method to
estimate the root of f(x)=e-x - x, employing two
initial guesses of x-1=0 and x0=1. The true value
of the root is 0.56714329.
Solution:
First iteration:
x-1 = 0 f(x-1) = e0 -0 = 1.0

x0 = 1 f(x0) = e-1 -1 = 0.367879 1 = -0.63212

f(x0 )(x1 x0 ) - 0.63212(0 - 1)


Based on Eq 7.3, x1 = x0 = 1- = 0.61270
f(x1 ) f(x0 ) 1 - (-0.63212)

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 34

Modified Secant Method


Rather than using two arbitrary values to estimate the
derivate, an alternative approach involves a fractional
perturbation of the independent variable to estimate
f(x),
f(xi + xi ) f(xi )
f (xi )
'
Eq 7.4
xi
Substitute Eq 7.4 into Eq 6.2:
xi f(xi )
xi +1 = xi Eq 7.5
f(xi + xi ) f(xi )

Eq 7.5 is the formula for the Modified Secant Method

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MEEM2036 Engineering Computing, part 7, ver 4 15

Golden-section
a b

b a

Given golden-section: c
Answer:

Proof:
MEEM2036 Engineering Computing, part 7, ver 4 28

Golden-Section Search Error Estimate

Based on Eq 8.9 and Eq 8.10, the maximum


possible error for each iteration is (1-R)(xU xL).
Therefore a can be calculated as follows:

x U xL
a (1 - R) 100% Eq 8.11
xoptimal
MEEM2036 Engineering Computing, part 7, ver 4 29

Golden-Section Search - Summary

Continue on next slide


MEEM2036 Engineering Computing, part 7, ver 4 30

Golden-Section Search - Summary


MEEM2036 Engineering Computing, part 7, ver 4 31

Example 1
Problem Statement: Use the Golden-section
search method to find the maximum of:
x2
f(x) 2sinx
10
within the interval xL=0 and xU = 4. The true
optimal value xoptimum = 1.4276. s = 5%

Solution:
5 1
d (4 0) 2.472
2
x1 0 2.472 2.472 x2 4 2.472 1.528
MEEM2036 Engineering Computing, part 7, ver 4 32

Example 1
x2
f(x) 2sinx
10

xU
xL x2 x1
MEEM2036 Engineering Computing, part 7, ver 4 33

Example 1 (cont)
1.528 2
f(x2 ) f(1.528) 2sin(1.528) - 1.765
10
f(x1 ) f(2.472) 0.635
As f(x2) > f(x1 ), the maximum value is in the interval defined by xL, x2 and x1.
xLnew =xLold= 0 xUnew =x1old= 2.472 x1new =x2old= 1.528

f(x1new )=f(x2old)= 2.472

dnew = 0.61803 (2.472 -0) = 0.61803 dold = 1.528


x2new =xUnew dnew = 2.472 - 1.528 = 0.944
f(x2new )=f(0.944)= 1.531
MEEM2036 Engineering Computing, part 7, ver 4 34

Example 1 (cont)
i xL x2 f(x2) x1 f(x1) xU d a

1 0 1.5279 1.7647 2.4721 0.6300 4 2.4721 99.99

2 0 0.9443 1.5310 1.5279 1.7647 2.4721 1.5279 61.80

3 0.9443 1.5279 1.7647 1.8885 1.5432 2.4721 0.9443 38.20

7 1.3050 1.3901 1.7742 1.4427 1.7755 1.5279 0.1378 5.90

8 1.3901 1.4427 1.7755 1.4752 1.7732 1.5279 0.0851 3.64


MEEM2036 Engineering Computing, part 7, ver 4 35
Can we directly estimate how
many iterations we need?
MEEM2036 Engineering Computing, part 7, ver 4 36
Number of Iterations required
for Example 1
1.4276

i xL x2 f(x2) x1 f(x1) xU d a

So 8 times 1 0 1.527
9
1.764
7
2.472
1
0.630
0
4 2.472
1
99.
99

2 0 0.944 1.531 1.527 1.764 2.472 1.527 61.


3 0 9 7 1 9 80

3 0.944 1.527 1.764 1.888 1.543 2.472 0.944 38.


3 9 7 5 2 1 3 20

7 1.305 1.390 1.774 1.442 1.775 1.527 0.137 5.9


0 1 2 7 5 9 8 0

8 1.390 1.442 1.775 1.475 1.773 1.527 0.085 3.6


1 7 5 2 2 9 1 4
MEEM2036 Engineering Computing, part 7, ver 4 45

Example 2 - Newtons Method


Problem Statement: Use Newtons method
to find the maximum of:
x2
f(x) 2sinx
10

with an initial guess of x0 = 2.5. The true


optimal value xoptimum = 1.4276.
Solution:
x 1
f (x) 2cos(x)
'
f (x) 2sin(x)
"

5 5
MEEM2036 Engineering Computing, part 7, ver 4 46

Example 2 - Newtons Method (cont)

The above equations can be substituted


into Eq 8.12:
xi
2cos(xi )
xi 1 xi 5
1
2sin(xi )
5
Substituting the initial guess yields:
2.5
2cos(2.5)
x1 2.5 5 0.99508 f(x1) = 1.57859
1
2sin(2.5)
5
MEEM2036 Engineering Computing, part 7, ver 4 47

Example 2 - Newtons Method (cont)


xrnew xrold x1 x0 0.995 2.5
a 100% 100% 100
xrnew x1 0.995
151.2361

The second iteration gives:


0.995
2cos(0.995)
x2 0.995 5 1.46901
1
2sin(0.995)
5
f(x2) = 1.77385

xrnew xrold x2 x1 1.46901 0.995


a 100% 100% 100
xrnew x2 1.46901
32.2618
MBE2036 Engineering Computing, part 8, version 2

Example 2 (cont)
To find the optimal point:

4y =0 Eq 6

Solving Eq 5 and 6, we obtain the optimal point x=2, y =1


MBE2036 Engineering Computing, part 8, version 2

Example 2 (cont)
4y =0

=-2 4y

4y
2
MBE2036 Engineering Computing, part 8, version 2

Example 2 (cont)
=

As |H| > 0 and , function value f(2,1) is a


maximum.
The above example is pretty straight forward to find the
optimal point using first partial derivatives and Hessian.
However, this is not always that straight forward. So,
numerical method would be used instead!
MBE2036 Engineering Computing, part 8, version 2

Example 3
Using Steepest Ascent method for finding the maximum
point of f(x,y)=2xy +2x x2 2y2

Using initial guesses, x=-1 and y=1

2xy x2 2y2

2xy x2 2y2
Recall:
is the
= y Eq 4 max point
MBE2036 Engineering Computing, part 8, version 2

Example 3 (cont)
Using initial guesses, x=-1 and y=1

y= 2(1) 4(1)=6
Therefore the gradient vector is:
MBE2036 Engineering Computing, part 8, version 2

Example 3(cont)
To find the maximum, we could search along the
gradient direction, that is, along an h axis running
along the direction of the above gradient vector.

The function can be expressed along this axis as:


Using initial guesses, x=-1 and y=1

f(x,y)=2xy +2x x2 2y2


MBE2036 Engineering Computing, part 8, version 2

Example 3(cont)
Multiplying the terms, we develop a one-dimensional
function along the gradient as:

For finding the maximum point along the h axis:

Therefore
This means that If we travel along the h0-axis, g(h)
reaches maximum value when h=0.2
MBE2036 Engineering Computing, part 8, version 2

Example 3(cont)
Substitute h=0.2 into Eq 9 and Eq 10

The search moves from point 0 to point 1


The second iteration repeat the previous steps with the
new starting point of (0.2, -0.2)
MBE2036 Engineering Computing, part 8, version 2

Example 3(cont)

y= 2(0.2) 4(0.2)=1.2
MBE2036 Engineering Computing, part 8, version 2

Example 3(cont)
f(x,y)=2xy +2x x2 2y2
Substituting x2, y2 values into the functions yields:

For finding the maximum point along the h1 axis:

Therefore
This means that If we travel along the h1-axis, g(h)
reaches maximum value when h=1
MBE2036 Engineering Computing, part 8, version 2

Example 3(cont)
= 0.2+1.2(1)=1.4

The search moves from point 1 to point 2


The third iteration repeat the previous steps with the
new starting point of (1.4, 1), and in so doing move
closer to the maximum. The approach can be repeated
until approach to zero for the any new
starting point.
MBE2036 Engineering Computing, part 8, version 2

Example 3(cont)

y= 2(1.4) 4(1)=-1.2
MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 9

Linear Regression (cont)


To determine values for a0 and a1 of the
best fit line, Eq 9.9 is differentiated with
respect to each coefficient:
n
(yi a0 a1 xi )2
S r i 1
0 Eq 9.10
a0 a0

n
(yi a0 a1 xi )2
S r i 1 Eq 9.11
0
a1 a1
S r S
For finding minimal S r , 0 and r 0
a0 a1
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 10

Linear Regression (cont)


From Eq 9.10:

S r (yi a0 a1 xi )
2


a0 a0
(y1 a0 a1 x1 )2 (y 2 a0 a1 x2 )2
...
a0 a0
(y1 a0 a1 x1 )
(y n a0 a1 xn )2 1
a0
a0
(y1 a0 a1 x1 )2 (y1 a0 a1 x1 ) (yn a0 a1 xn )2 (yn a0 a1 xn )
...
(y1 a0 a1 x1 ) a0 (yn a0 a1 xn ) a0

2(y1 a0 a1 x1 ) (-1) ... 2(y n a0 a1 xn ) (-1)

2 (yi a0 a1 xi ) 0 Eq 9.12

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 11

Linear Regression (cont)


From Eq 9.11:
S r (yi a0 a1 xi )2

a1 a1
(y1 a0 a1 x1 )2 (y2 a0 a1 x2 )2
...
a1 a1
(yn a0 a1 xn )2 (y1 a0 a1 x1 )
x1
a1 a1
(y1 a0 a1 x1 )2 (y1 a0 a1 x1 ) (yn a0 a1 xn )2 (yn a0 a1 xn )
...
(y1 a0 a1 x1 ) a1 (yn a0 a1 xn ) a1

2(y1 a0 a1 x1 ) (-x1 ) ... 2(yn a0 a1 xn ) (-xn )

2 (yi a0 a1 xi ) xi 0 Eq 9.13

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 12

Linear Regression (cont)


From Eq 9.12

2 (yi a0 a1 xi ) 0
(yi a0 a1 xi ) 0
yi a0 (a1 xi ) 0

yi na0 a1 xi 0
yi a1 xi na0 Arithmetri c mean y
yi
n
yi
a1 xi xi
a0 Eq 9.14 Arithmetri c mean x
n
n n

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 13

Linear Regression (cont)


From Eq 9.13
2 (yi a0 a1 xi ) xi 0


yi xi a0 xi a1 xi2 0
2
yi xi a0 xi a1 xi 0 yi xi a0 xi a1 xi2 0

Substitute Eq 9.14 into the above equation

yi
a1 xi
yi xi ( ) ( xi ) a1 xi2 0
n n
yi xi a1 ( xi )2
yi xi a1 xi2 0
n n

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 14

Linear Regression (cont)


n yi xi yi xi a1 ( xi )2 a1n xi2
0
n n n n

n yi xi yi xi a1 ( xi )2 a1n xi2

n n n n

n yi xi yi xi a1 (n xi2 ( xi )2 )

n (yi xi ) yi xi
a1 Eq 9.15
n x ( xi )
2
i
2

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 15

Example 1
Measured Data
xi yi
7

6
1 0.5
5 2 2.5
4
Measured Data 3 2.0
Y

2 4 4.0
1
5 3.5
0
0 1 2 3 4
X
5 6 7 8
6 6.0
7 5.5

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 16

Example 1 (cont)
n= 7

xi yi 1 0.5 2 2.5 ... 7 5.5 119.5


2
xi 12 2 2 ... 7 2 140
xi 1 2 ... 7 28
yi 0.5 2.5 ... 5.5 24
n (yi xi ) yi xi 7(119.5) - 24(28)
a1 0.8392857
n xi2 ( xi ) 2
7(140) - 28 2

yi
a1 xi 24 0.8392857(28)
a0 0.07142857
n n 7 7

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 17

Example 1 (cont)
The best straight line is
7
y=0.07142857 + 0.8392857x
6

4
Measured data
Y

Best fit line


3

0
0 1 2 3 4 5 6 7 8
X

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 18

Polynomial Regression
In some engineering problems, data
cannot be represented accurately by a
straight line (heat current)
In such cases, polynomial line may be
needed to fit the data.
The approach in using the least-squares
method to fit a straight line through the
data can be extended to polynomial
equation Polynomial Regression.

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 19

Polynomial Regression (cont)


For example, the measured data exhibits the pattern
of a second-order polynomial equation.
We therefore apply the least-squares regression
method to find a best-fit second-order polynomial for
the data.
A second-order polynomial (quadratic equation) can
be expressed in the following general format:

y a0 a1 x a1 x 2

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 20

Polynomial Regression (cont)


We need to minimise the sum of the
squares of the residuals between the
measured data y and the calculated data
from the second-order polynomial model:
n n ^
S r e (yi y )2
2
i
i 1 i 1
n n
S r e (yi a0 - a1 xi - a2 xi2 )2
2
i Eq 9.16
i 1 i 1

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 21

Polynomial Regression (cont)


That is we take the derivate of Eq 9.16 with each of
the coefficients a0, a1 and a2 of the polynomial:
S r (yi a0 - a1 xi - a2 xi )
2 2

0
a0 a0
S r (y1 a0 - a1 x1 - a2 x12 )2 (y2 a0 - a1 x2 - a2 x22 )2 (yn a0 - a1 xn - a2 xn2 )2
...
a0 a0 a0 a0

S r (y1 a0 - a1 x1 - a2 x12 )2 (y1 a0 - a1 x1 - a2 x12 )


...
a0 (y1 a0 - a1 x1 - a2 x1 )2
a0
(yn a0 - a1 xn - a2 xn2 )2 (yn a0 - a1 xn - a2 xn2 )

(yn a0 - a1 xn - a2 xn )
2
a0

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 22

Polynomial Regression (cont)


S r
2(y1 a0 - a1 x1 - a2 x12 )(-1) ... 2(yn a0 - a1 xn - a2 xn2 )(-1)
a0

S r
2 (yi a0 - a1 xi - a2 xi2 ) 0 Eq 9.17
a0
Similarly

S r
2 xi (yi a0 - a1 xi - a2 xi2 ) 0 Eq 9.18
a1

S r
2 xi2 (yi a0 - a1 xi - a2 xi2 ) 0 Eq 9.19
a2

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 23

Polynomial Regression (cont)


y i na0 a1 xi a2 x 2
i Eq 9.20

i i 0 i 1 i 2 i
2
x y a x - a x - a x 3
Eq 9.21

i i 0 i 1 i 2 i )
x 2
y a x 2
- a x 3
- a x 4
Eq 9.22

To find the coefficients for the 2nd order polynomial, we need to solve the above
simultaneous equations. This can be achieved by expressing the equations in
matrix form and then apply Gauss elimination to solve the equations

n
xi i a0 yi
x 2

xi i
x 2
i a1 xi yi
x 3 Eq 9.23
x2 4 x2 y
i i i 2 i i
3
x x a

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 24

Example 2
Measured Data
xi yi
70

60 0 2.1
50

40
1 7.7
Measured Data
Y

30
2 13.6
20

10
3 27.2
0
0 1 2 3 4 5 6
X 4 40.9

5 61.1

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 25

n=6
Example 2 (cont)
xi 15 4
yi 15 2.6
xi 979
6 15 55 a0 152.6

2
xi 55
xi yi 585.6 15 55 225 a1 585.6
55 225 979 a2 2488.8

2
3
xi 225 xi yi 2488.8

6 15 55 a0 152.6

15 55 225 a1
585.6
55 225 979 a2 2488.8
To make it easier for our calculation, convert the above matrix into the
augmented matrix format.

6 15 55 152.6 L1
L2
15 55 225 585.6
55 225 979 2488.8 L3

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 26

Example 2 (cont)
Apply Gauss Elimination to find the parameters

6 15 55 152.6 L1
L2
15 55 225 585.6
55 225 979 2488.8 L3

6 15 55 152.6
L2 L1*15/6 -> L2
0 17.5 87.5 204.1
0 87.5 474.83 1089.967 L3- L1 *55/6 ->L3

6 15 55 152.6

0 17.5 87.5 204.1
0 L3 L2 * 87.5/17.5 ->L3
0 37.33 69.467

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 27

Example 2 (cont)
6 15 55 152.6

69.467 0 17.5 87.5 204.1
a2 1.86071 0
37.33 0 37.33 69.467

204.1 87.5 1.86071


a1 6 15

55 152.6

17.5 0 17.5 87.5 204.1
0 37.33 69.467
2.359286 0

152.6 - 55 1.860714 - 15 2.359286


a0 2.478571
6

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 28

Example 2 (cont)
70
The best polynomial
60 y=2.47857 + 2.35929x +1.86071x2
50

40
Measured Data
Y

Best Fit Polynomial


30

20

10

0
0 1 2 3 4 5 6
X

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 29

General Polynomial Regression

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 30

General Polynomial Regression

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 10, Ver 2

Example 2
The period of is

The period of is
We can guess and verify that the period of
is , and thus

33
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 10, Ver 2

Example 2

Then we know


All other coefficients should be zero

34
Department of Mechanical and Biomedical Engineering City University of Hong Kong
2

Exercise A1
The system equation describing the
change of depth of liquids in the tank is
shown in equation Eq 9.1. Apply analytical
method to find y
dy
3Ksin3 t K Eq 9.1
dt
K is the constant out-flow rate in term of the depth.
y=0 when t = 0 Answer: y k[-3cos t cos 3 t t 2] Eq 9.4

Hints: sin 2 x cos 2 x 1


d cos x
sin x d cos x - sin x dx
dx
3

Answer A1
To find y, we need to integrate both sides
of Eq 9.1. That is
t K)dt
3
dy (3Ksin Eq 9.2
dy k (3sin2 t sin t 1)dt
dy k [3(1 - cos 2 t) sin t 1]dt

dy k [3sin t - 3cos 2 t sin t 1]dt


dy k [ 3sin t dt - 3 cos 2 t sin t dt dt ]
4

Answer A1

dy k [ 3sin t dt 3 cos2 t d cos t dt ]

y k[-3cos t cos3 t t] c Eq 9.3

As t=0, y0=0
0 k[-3cos 0 cos3 0 0] c
0 k[-3 1 0] c
c 2k
Substitute C into Eq 9.3
y k[-3cos t cos3 t t] 2k
y k[-3cos t cos3 t t] 2k
y k[-3cos t cos 3 t t 2] Eq 9.4
5

Exercise A2
The system equation describing the change of
y is shown in equation Eq 9.5. Apply analytical
method to find y
dy
Ksin2 t K Eq 9.5
dt
K is a constant k k
y=0 when t = 0 Answer: y sin2t t Eq 9.10
4 2
Hints:
sin 2 cos 2 1
cos2 cos 2 sin 2
6

Answer A2
sin 2 cos 2 1
cos2 cos 2 sin 2
cos 2 1 - sin 2
cos2 1 - sin 2 sin 2
1
sin 2 1 cos2 Eq9.6
2
Substitute Eq 9.6 into Eq 9.5
dy 1
K (1 - cos 2t) K Eq 9.7
dt 2
7

Answer A2
To find y, we need to integrate both side of equation Eq 9.7:

1
dy k 2 (1 - cos 2t) 1 dt Eq 9.8

1 1
dy k - cos 2t dt
2 2
1

dy k - cos 2t dt - dt
2
d2t
dt
2 dt
d2t
2
1

dy k - cos 2t dt - dt
2

1 1
y k sin2t t c Eq 9.9
2 2
8

Answer A2
When t=0, y=0
1 1
0 k sin0 0 c
2 2
Therefore c=0
k k
y sin2t t Eq 9.10
4 2
9

Exercise A3
The system equation describing the change
of y is shown in equation Eq 9.11. Apply
analytical method to find y
dy
k1 - k 2 y Eq9.11
2

dt
k1 and k2 are constant
y=0 when t=0 k1
Answer: y t anh( k 2 k1 t)
k2
Hints: dx
1 x2
1
tanh (x)
10

Answer A3
To find y, we need to solve the differential equation Eq 9.11
dy
k1 - k2 y 2 dt Eq9.11

dy
k2 y 2 dt
k1 1 -
k1 k2
Let x y
k1
dy
k 2
dt Eq9.12
k2
k1 1 - 2 y d y
k1 dx k1 k
2
dy dy k1
11

Answer A3
k1
dx dy
k2
Substitute the above equation into Eq 9.12
k1
dx
k2 k1 dx
2
dt
k1 1 - x 2 dt k 2 k1 1 - x
2

1
tanh 1 x t c Eq9.13
k 2 k1
y =0 when t =0

Therefore c =0 and Eq 9.13 can be rewritten as in Eq 9.14


12

Answer A3
1
tanh 1 x t Eq9.14
k 2 k1
tanh 1 x k 2 k1 t

x tanh( k 2 k1 t)
k2
y tanh( k 2 k1 t)
k1
k2 k1
y tanh( k 2 k1 t) y t anh( k 2 k1 t)
k1 k2
13

Exercise A4
The system equation describing the change
of y is shown in equation Eq 9.15. Apply
analytical method to find y
dy
Ksin2 t cos3t K Eq 9.15
dt
K is a constant
1 1
y=0 when t=0 Answer: y k sin3 t - sin5 t - t Eq 9.20
3 5
Hints:
1 n 1

n
2 2
sin x cos x 1 x dx x c
n1
d sin x
cos x d sin x cos x dx
dx
14

Answer A4
To find y, we need to integrate both sides of Eq 9.15


dy k (sin2 t cos3 t 1)dt Eq 9.16

dy k[ sin t cos t dt - dt ] Eq 9.17


2 3


sin2 t cos3 t dt sin2 t cos2 t cos t dt


sin2 t (1 - sin2 t) cos t dt


sin2 t cos t dt - sin4 t cos t dt


2 4
sin t d sin t - sin t dsin t

1 1
sin3 t - sin5 t c Eq 9.18
3 5
15

Answer A4
Substitute Eq 9.18 into Eq9.17

1 3 1 5
y k sin t - sin t - t c Eq 9.19
3 5
As y=0 when t=0, therefore C =0. Eq 9.19 can be rewritten as:

1 1
y k sin3 t - sin5 t - t Eq 9.20
3 5
16

Exercise A5
The system equation describing the change of
y is shown in equation Eq 9.21. Apply analytical
method to find y
dy 2
k1 k 2 y Eq9.21
dt
k1 and k2 are constant
y=0 when t=0 k1
answer y tan( k2 k1t) Eq9.25
Hints: k2

dx 1 1 x
2
a x 2

a
tan ( )
a
17

Answer A5
To find y, we need to solve the differential equation Eq 9.11
dy
k 1 k2 y 2

dt Eq9.22

dy

k1 k 2 y
2 2

dt Let x k 2 y

dx d k 2 y
dy

k2
dt dy dy
k1 k 2 y
2 2
dx
dy
1 dx k2

k2 k1 x
2 2

dt
18

Answer A5
1 1 1 x
tan t c
k 2 k1 k1
1 1 k y
tan 1 2 t c Eq 9.23
k 2 k1 k1
As y=0 when t=0, therefore c =0 and Eq 9.23 can be re-written as:

1 1 k 2
tan y t Eq 9.24
k 2 k1 k1

1 k 2
tan y k 2 k1t
k1
19

Answer A5
k2
y tan( k 2 k1t)
k1
k1
y tan( k 2 k1t) Eq9.25
k2
20

Exercise A6
The system equation describing the change
of y is shown in equation Eq 9.26. Apply
analytical method to find y
dy
x sin(x) Eq 9.26
dx
y=0 when x=0 Answer : y x cos x sin x Eq 9.30

Integratio n by parts d cos x


Hints: sin x
dx
udv uv - vdu d cos x - sin x dx
21

Answer A6
dy
x sin(x) dx Eq 9.27

Let u=x and dv=sin(x) dx

Then du = dx and v = -cos x


x sin x dx xcos x cos x dx
x cos x sin x c Eq 9.28
Substitute Eq 9.28 into Eq9.27:
y x cos x sin x c Eq 9.29
As y=0 when x=0, therefore c =0
y x cos x sin x Eq 9.30
22

Exercise B1
Use the Taylor series expansion (Eq 9.31) to
prove the ex function as shown in Eq 9.32

' f '' (xi ) 2 f (3)(xi ) 3


f(xi 1 ) f(xi ) f (xi )h h h ...
2! 3!
f (n) (xi ) n
h Rn Eq 9.31
n!
where the step size h xi 1 xi

x 1 2 1 3
e 1 x x x ... Eq 9.32
2 6
Hints: de x
ex
dx
23

Answer B1
Let xi+1 = x , xi=0 and f(x)= ex, Equation 3.31 becomes

' f '' (0) 2 f (3)(0) 3


f(x) f(0) f (0)x x x ...
2! 3!
f (n)(0) n
x Rn Eq 9.33
n!
where the step size h x 0

x e0 2 e0 3
0 0
e e e x x x ...
2! 3!
As e0 = 1
x 1 2 1 3
e 1 x x x ... Eq 9.34
2 6
24

Exercise B2
Use the Taylor series expansion (Eq 9.31) to
prove the sinh function as shown in Eq 9.35
'' (3)
f (x ) f (xi ) 3
f(xi 1 ) f(xi ) f ' (xi )h i h2 h ...
2! 3!
f (n) (xi ) n
h Rn Eq 9.31
n!
where the step size h xi 1 xi
1 1 5
sinhx x x 3 x .... Eq9.35
6 120
Hints: d sinh x d cosh x
cosh x sinh x
dx dx
25

Answer B2
Let xi+1 = x , xi=0 and f(x)= sinh x , Equation 3.31 becomes
' f '' (0) 2 f (3)(0) 3
f(x) f(0) f (0)x x x ...
2! 3!
f (n)(0) n
x Rn Eq 9.33
n!
where the step size h x 0
sinh(0) 2 cosh(0) 3 sinh(0) 4
f(x) sinh(0) cosh(0)x x x x
2! 3! 4!
cosh(0) 5
x ... Eq 9.34
5!
As sinh(0) = 0 and cosh(0) =1, Eq 9.34 can be written as:
1 3 1 5
sinh x x x x ...
3! 5!
1 1 5
sinh x x x 3 x ... Eq 9.35
6 120
26

Exercise C1
f(x)=sin x

Figure 3.1

Use the Bisection method to calculate the root of f(x)=sin(x) as shown in


Figure 3.1. The initial search values for the lower limit, xL, and the upper
limit xU are 3 and 3.5 respectively. The stopping criterion s for terminating
the calculation is 1%.
27

Answer C1
Step 1: confirm xL and xU include the root
f(3)*f(3.5)=0.1411* -0.3508 < 0
Therefore xL and xU do include the root
Iteration 1:
Step 2:
Calculate xr
xr=(xL + xU)/2 = (3+3.5)/2= 3.2500
Step 3:
f(xL)=sin(3) = 0.1411
f(xr) =sin (3.25) = -0.1082
As f(xL)f(xr)<0, therefore the root lies in the LOWER sub-interval
Make the new xU = xr =3.25 and then repeat step 2
28

Answer C1
Iteration 2:
xr = (3+3.25)/2 = 3.1250
a = abs((new xr old xr)/(new xr))*100
= abs((3.125-3.25)/3.125)*100=4%
f(xL)=sin(3)= 0.1411
f(xr)=sin(3.125)= 0.0166
As f(xL)f(xr)>0, therefore the root lies in the
upper sub-interval
Make the new xL = xr=3.125 and then repeat
step 2
29

Answer C1
Iteration 3:
xr = (3.125+3.25)/2 = 3.1875
a = abs((new xr old xr)/(new xr))*100
= abs((3.1875-3.125)/3.1875)*100=1.9608%
f(xL)=sin(3.125)= 0.0166
f(xr)=sin(3.1875)= -0.0459
As f(xL)f(xr)<0, therefore the root lies in the
lower sub-interval
Make the new xU = xr=3.1875 and then repeat
step 2
30

Answer C1
Iteration 3:
xr = (3.125+3.1875)/2 = 3.1563
a = abs((new xr old xr)/(new xr))*100
= 0.9901%
As a < s, the final estimate for the root is
3.1563
f(3.1563) = -0.0147
31

Exercise C2
f(x)=sin x

Figure 4.1

Use Newton-Raphson method to estimate the root of f(x)=sin(x) as shown in


Figure 4.1. x0=3 is used as the initial guess and the stopping criterion s for
terminating the calculation is 1%.
32

Answer C2
Find the first derivative for f(x):
f ' (x) = d sin(x)/dx = cos(x)
f(xi )
xi 1 xi '
f (xi )
x1 = x0 sin(x0)/(cos (x0))
= 3.0 - (0.14112)/(-0.98999) = 3.1425
ea=abs((x1-x0)/x1) * 100%
=abs((3.1425-3)/3.1425)= 4.5360%
33

Answer C2
x2 = x1 sin(x1)/(cos (x1))
=3.1416
ea=abs((x2-x1)/x2) * 100%
=0.0304%
As a < s, the final estimate for the root is
3.1416
f(3.1416) = -7.3x10-6
34

Exercise D1

Find the continuous Fourier series for the above waveform

Hints:
35

Answer D1
36

Answer D1
37

Answer D1

Integration by parts
38

Answer D1
39

Answer D1
40

Answer D1
41

Answer D1
42

Answer D1
43

Answer D1
44

Answer D1
45

Answer D1
46

Answer D1
47

Exercise D2

Whenever you see the trigonometric


function, you can do something cleverer
48

Answer D2
49

Answer D2
50

Answer D2
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 40

Sequence

Matlab example:

x=3
y=x+5;
z=x*y;

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 41

Selection
Matlab example:

if x<0
y=-10;
else if x>0
y=10;
else
y=0;
end

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 42

Iteration
Matlab example:

for i=0:2:20
x=x+i;
end

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 43

Another iteration
Matlab example:

while (1)

i=i+1;

if i>=10, break, end

y=x^2;

end

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 44

function mainprog

m=68.1;
g=9.8; function myvelocity(m,g,c,t)
c=0.25;
t=0:2:12;

v=velocity(m,g,c,t) function velocity_profile(m,g,c,t)

v1=velocity_profile(m,g,c,t)

plot (t, v, t, v1) function

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 1, version 4, Dr. Yajing SHEN 41

MATLAB Graphics
>> plot(t,v)
>> title('v versus t')
>> xlabel('value t')
>> ylabel('value v')
>> grid
>> axis([0 22 0 450])

xmin xmax ymin ymax

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 1, version 4, Dr. Yajing SHEN 42

MATLAB Graphics

>> t=0:2:20;
>> for i=1:length(t)
vt(i)=t(i)^1.5;
v(i)=t(i)^2;
end
>> plot(t,v,'r',t,vt,'b')

Red color for v Blue color for vt

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 1, version 4, Dr. Yajing SHEN 47

Create a Function M-file (cont)


Filename is same as
the name of the
function

>> clear all


function myresult = mysquare(n) >> t=0:2:10;
for i=1:length(n) >> v=mysquare(t)
x(i)=n(i)^2; v=
end
0 4 16 36 64 100
myresult=x;
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 2, version 4, Dr. Yajing SHEN 5

Example Comments & M-Files


function velocity = free_fall_vel(m,c,t) g = 9.8; % acceleration of gravity
%Function name: free_fall_vel (m,c,t) k1 = g * m/c;
% k2 = -c*t/m;
%Author: Dr. B.L. Luk
%Date: 15 Sep 2004 % find the free falling velocity
%Version: 2.0 velocity =k1*(1-exp(k2));
%
%Description: The function computes the free-fall velocity
% (mps) of an object with a linear drag coefficient
In Lecture Handout 1, Eq 1.29 is
%
%Input: used to calculate the velocity
% m = mass (kg)
% c = drag coefficient (kg/s) c
% t = time (s)
m t
%Output:
% velocity = downward velocity (m/s) v g (1 - e m )
%
%Revision History:
c
% version 2: add temporary variables k1 and k2 to make
% the calculation of velocity easier to follow.
%Any Known Bugs:
% No
%
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 2, version 4, Dr. Yajing SHEN 9

Example M-File for structuring


software program
In MATLAB, a M-file can execute another m-files
You can use this feature to structure your program
function vel =free_fall_vel_profile(m, c, ts,tf,h)
% Function description placed here t=ts; % t is the temporary time variable
%Input: % for calculating the profile
% m = mass (kg) i=1; % i is the index for the array
% c = drag coefficient (kg/s)
% ts = start time for calculating the % A loop to calculate each velocity for the
% velocity profile(s) % profile from time ts to tf
% tf = final time for calculating the while(t <= tf)
% velocity profile(s) % call the free_fall_vel.m function
% h = step size (s) v(i) = free_fall_vel(m,c,t);
i=i+1; c
%Output: m t
% vel = downward velocity profile - t=t+h; v g(1 - e m )
% an array (m/s) end c
%
% Any other information or description vel = v;
% placed here

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 3, version 4, Dr. Yajing SHEN 5

M-File for calculating the velocity profile


c
v (t i +1) = v (t i ) + g v (t i )(t i +1 t i ) Eq 1.33
m
function vel = velocity_profile(dt, ti, tf, vi, m, c)
% velocity_profile(dt, ti, tf, vi, m , c)
% Calculate the velocity of an free-falling object
% using Euler's method

t = ti;
v(1) = vi;
i=2; Infinite loop
while (1)
if t+dt > tf break out from the loop
if t + dt > tf, break, end
dvdt = deriv(v(i-1), m, c);
v(i)=v(i-1) + dvdt * dt;
t=t+dt;
i=i+1;
end

vel = v;
Department of Mechanical and
Biomedical Engineering City University of Hong Kong

You might also like