Professional Documents
Culture Documents
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
Stopping criterion:
a s Eq 3.4
where s is a stopping criterion.
v (ti1 ) v (t i ) R1
v ' (t i ) Eq 4.8
(t i 1 t i ) (t i 1 t 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
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!
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
14.75 - 14.5
a 100 1.6949%
14.75
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
3 14 15 14.5 3.448
xU xL
2 x xL
a 100% U 100% Eq 5.10
xU xL xU xL
2
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
Golden-section
a b
b a
Given golden-section: c
Answer:
Proof:
MEEM2036 Engineering Computing, part 7, ver 4 28
x U xL
a (1 - R) 100% Eq 8.11
xoptimal
MEEM2036 Engineering Computing, part 7, ver 4 29
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
Example 1 (cont)
i xL x2 f(x2) x1 f(x1) xU d a
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
5 5
MEEM2036 Engineering Computing, part 7, ver 4 46
Example 2 (cont)
To find the optimal point:
4y =0 Eq 6
Example 2 (cont)
4y =0
=-2 4y
4y
2
MBE2036 Engineering Computing, part 8, version 2
Example 2 (cont)
=
Example 3
Using Steepest Ascent method for finding the maximum
point of f(x,y)=2xy +2x x2 2y2
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.
Example 3(cont)
Multiplying the terms, we develop a one-dimensional
function along the gradient as:
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
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:
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
Example 3(cont)
y= 2(1.4) 4(1)=-1.2
MBE2036 Engineering Computing, part 9, Ver 3, Dr. B.L. Luk 9
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
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 (yi a0 a1 xi ) 0 Eq 9.12
2 (yi a0 a1 xi ) xi 0 Eq 9.13
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
yi xi a0 xi a1 xi2 0
2
yi xi a0 xi a1 xi 0 yi xi a0 xi a1 xi2 0
yi
a1 xi
yi xi ( ) ( xi ) a1 xi2 0
n n
yi xi a1 ( xi )2
yi xi a1 xi2 0
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
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
Example 1 (cont)
n= 7
yi
a1 xi 24 0.8392857(28)
a0 0.07142857
n n 7 7
Example 1 (cont)
The best straight line is
7
y=0.07142857 + 0.8392857x
6
4
Measured data
Y
0
0 1 2 3 4 5 6 7 8
X
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.
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
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
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
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
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
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
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
Example 2 (cont)
70
The best polynomial
60 y=2.47857 + 2.35929x +1.86071x2
50
40
Measured Data
Y
20
10
0
0 1 2 3 4 5 6
X
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
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 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
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
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
Answer A6
dy
x sin(x) dx Eq 9.27
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
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
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
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
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
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
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;
Selection
Matlab example:
if x<0
y=-10;
else if x>0
y=10;
else
y=0;
end
Iteration
Matlab example:
for i=0:2:20
x=x+i;
end
Another iteration
Matlab example:
while (1)
i=i+1;
y=x^2;
end
function mainprog
m=68.1;
g=9.8; function myvelocity(m,g,c,t)
c=0.25;
t=0:2:12;
v1=velocity_profile(m,g,c,t)
MATLAB Graphics
>> plot(t,v)
>> title('v versus t')
>> xlabel('value t')
>> ylabel('value v')
>> grid
>> axis([0 22 0 450])
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')
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