CPE 332 Computer Engineering
Mathematics II Week 10
Part III, Chapter 8 Error, Transcendental Function and Power Series
Taylor Series and Function Approximation
Today Topics
• Numerical Methods • Errors • Transcendental Function • Series • Power Series • Taylor Series • MacLaurin Series • Function Approximation • HW VII • MT Exam and Cumulative Grades
Numerical Methods • ในวชาคณตศาสตรทเราเรยนทผานมา การหา
Solution จะประกอบดวยการแกสมการ ซงรวมถงการแกสมการพคณต การหา Derivative การ Integrate การแกสมการ Differential Equation รวมถงการใชเครองมอทางคณตศาสตรเพอแกสมการ เชน Fourier Transform, Laplace Transform หรอ Z-Transform วธการดงกลาวจดวาเปนการแกปญหาทเรยก Analytical Method อยางไรกตาม ไมใชวาสมการทกสมการจะหาคาตอบได(ทอยในรปของ Explicit Form) ดงนนเราจาเปนทจะตองหาวธอนในการแกปญหา นนกคอวธการทเรยก Numerical Method
Numerical Methods • วธการทาง Numerical ทเคยใชกนมาไดแกวธการ
Graphical Method และใชบวนการ Interpolation แตเมอมการประดษฐคอมพวเตอรขนมา กมการนาคอมพวเตอรเขามาใช และขยายขอบเขตของวชานออกเปนสาขาใหมทางคณตศาสตร มการศกษา Algorithm และวธการเขยนโปรแกรม รวมถงการวเคราะหคาความผดพลาด(Error Analysis) และลกษณะการ Convergence และ Complexity ของ Algorithm
Y=f(x)=2x3-4x2-7x+5 • หา f(k) เชน f(0), f(-2), f(7) งาย • แกสมการ f(x)=k จะยาก เชน หาคา x ททาให f(x) = 0
Quadratic Equation • Polynomial Degree = 1, Straight Line
– y = f(x) = mx+b; x = (y-b)/m • Polynomial Degree = 2 • y = f(x) = ax2+bx+c • แกสมการ y = f(x) = 3 เราได
– y’ = f’(x) = ax2+bx+(c-3) = 0 – x = [-b±√(b2-4a(c-3))]/2a
• Polynimial Degree = n, n > 2 – ไมมสมการโดยตรงในการแก, – แตม Algorithm ในวธของ Numerical Method
• Algorithm จะเปน Iterative Method – คาตอบของ Numerical Method จะใหแคคาประมาณ
• คาจะถกตองขนเรอยๆ ใน Iteration ทสงขน (คานวณนานขน) ถา Algorithm Converge
Iterative Technique
Start
Iteration = 0 Set et
Init. X0
[Iteration i] Iteration += 1 Calculate xi
Error ei < es Stop
Estimate ei
Y N
โปรแกรมจะ Converge ถา ei < ei-1
Errors • ในการนาคอมพวเตอรมาชวยแกปญหา
ทางดานคณตศาสตรนน จะตองเขาใจกอนวาลกษณะการทางานของคอมพวเตอร หรอการคานวณจะขนอยกบวธการทเราเขยนโปรแกรมและ Algorithm ทใช ในการคานวณโดยใชเครองคานวณใดใดจะม Error เกดขนเสมอ ซงจะแบงไดเปนสองประเภทคอ Round- Off Error และ Truncation Error
Errors • Round- Off Error เกดจากการเกบ
ตวเลขใน Memory ของคอมพวเตอรน �นจะใชขนาดของ Memory ทจากด และข �นอย กบวาเราใหตวแปรชนดอะไร เชน Integer, Long Integer, Float, หรอ Double
Errors • Truncation Error เกดจาก Algorithm
หรอวธท เราใหคอมพวเตอรคานวณ น �น เปนแคการประมาณ จากสมการทางคณ ตศาสตรท เราตองการหาคาโดยใช คอมพวเตอรเขาช วย และเราไมไดแก สมการทางคณ ตศาสตรจรงๆ – การคานวณ จะตองมการบวกกนของ
Infinite Terms จงจะไดคาท ถ กตอง ดงน �นคอมพวเตอรจะคานวณ คาประมาณโดยตดเทอมทายๆท�ง
Precision, Accuracy, Significant Digit
• ดงนนเราจาเปนทจะตองรวา Error เรามมากแคไหน และพอจะยอมรบไดหรอไม นนขนอยกบการนาการคานวณไปใชงาน ปกตตวเลขทจะไปใชงานทางวศวกรรมศาสตรจะกาหนดดวยคา Significant Digit และจะยอมใหความไมแนนอนของตกเลขตกอยทหลกทายเทานน ดงนนคาของ Significant Digit จะบงบอกถงจานวนหลกของตวเลขทจะนาไปใชไดอยางมนใจ
• คาวา Accuracy หมายถงตวเลขทเราไดนนมคาใกลเคยงกบคาในความเปนจรงเทาไร แตคาวา Precision หมายถงตวเลขทไดมานนเกาะกลมกนมากแคไหน ดงนนคาของ Precision จะเปนตวกาหนดจานวนของ Significant Digit ทจะตองใช และคาของ Error จะเปนตวกาหนดความ Accuracy ของตวเลข
การยงเปา
Precision = ตา Accuracy = ตา
Precision = ตา Accuracy = สง
Precision = สง Accuracy = ตา
Precision = สง Accuracy = สง
Accuracy วดจากคา Error ทเกดระหวางคาเฉลยของ Data กบคาจรง Precision วดจากคา Variance หรอ SD ของกลมของ Data
Significant Digits (Figures) • คอตวเลขทมความหมายในการกาหนดคา
Precision – เปนเลขทกตวยกเวนเลขศนยนาหนา และศนยท
ตอทาย • จานวนศนยตอทายทเปนสวนของ s.f. บางทเรากาหนด
โดยใชเครองหมาย ‘bar’ • Examples
– 11.152 = 5 s.f. – 0.00879 = 3 s.f. – 000125600. = 6 s.f. – 0123400000 = 7 s.f.
• เลขทางขวามอของ s.f. จะไมนามาใส ใหทาการ Round-Off เชน – 456.389864 (6 s.f.) = 456.390 – 1287563.94 (3 s.f.) = 1290000
Error
• คอคาทแตกตางจากคาทแทจรง • เปนตวกาหนดคา Accuracy • แบงเปน
– Absolute Error, Et – Relative Error (%จากคาจรง), et
• นอกจากนยงม – Estimated Error, ea
• คาประมาณของ Error (Relative Error)
Absolute Error
Relative Error
Error Estimation • Convergence • Divergence
Mean Square Error(MSE) • ในการเปรยบเทยบความแตกตางระหวางคาท
แทจรงกบคาทประมาณได สาหรบกลมของตวอยาง เรามกจะใชคาเฉลยของ Error ในรปของคาเฉลยของกาลงสองของ Error ในแตละค เรยก Mean Square Error – ถาให Y เปนคาทแทจรง และ เปนคาทประมาณได
สาหรบคของตวอยาง N ตวอยาง
– คา RMSE หรอคา Root Mean Square Error คอคา Square Root ของคา MSE
Y
∑−
=
−=1
0
2)ˆ(1 N
iii YY
NMSE
∑−
=
−=1
0
2)ˆ(1 N
iii YY
NRMSE
Functions • เปนความสมพนธแสดง Set ของ Input
และ Set ของ Output – Function สามารถรบคา Input ไดหลาย Set
(หลายตวแปร) – การ Mapping เปนทางเดยว
• ถา Map กลบจะเปน อก Function ทเปน Inverse Function กบตวเดม
• Inverse อาจจะไมมสาหรบบาง Function
f(.)
x y
y=f(x)
x=f-1(y) อาจจะไมม
Polynomial Functions • เปนรปแบบของ Function ทงายทสด • Polynomial function Degree ‘n’ จะอยใน
รปของ(One Argument)
∑∑==
−−
+==
+++++==n
i
ii
n
i
ii
nn
nn
xaaxa
axaxaxaxaxfy
10
0
012
21
1)(
Polynomial of degree 2: f(x) = x2 - x - 2 = (x+1)(x-2)
Polynomial of degree 3: f(x) = x3/4 + 3x2/4 - 3x/2 - 2 = 1/4 (x+4)(x+1)(x-2)
Polynomial of degree 4: f(x) = 1/14 (x+4)(x+1)(x-1)(x-3) + 0.5
Polynomial of degree 5: f(x) = 1/20 (x+4)(x+2)(x+1)(x-1)(x-3) + 2
Polynomial of degree 6: f(x) = 1/30 (x+3.5)(x+2)(x+1)(x-1)(x-3)(x-4) + 2
Polynomial of degree 7: f(x) = (x-3)(x-2)(x-1)(x)(x+1)(x+2)(x+3)
เอามาจาก http://en.wikipedia.org/wiki/Polynomial
Transcendental Function
• เปน Function ทไมใช Algebraic – Algebraic function คอ Function ทสามารถ
นยามไดจาก Root ของสมการ Polynomial • Transcendental Function ไมสามารถ
เขยนในรป Solution ของ Polynomial – Exponential Function – Logarithm – Trigonometric Functions
• ดวยเหตผลน การหาคาของ Function อาจจะตองใชวธการประมาณคาแทน
Transcendental Examples
xxfcxxf
xxf
xxfccxf
xxf
c
x
x
x
sin)(1,0,log)(
)(
)(
1,0,)(
)(
6
5
1
4
3
2
1
=≠=
=
=
≠=
= π
การประมาณคาของ Function • ในสวนนเราจะ Review เรอง
– Infinite Series – Power Series – Taylor Series – Maclaurin Series – การประมาณคาโดยใช Series
Series และ Infinite Series • Series คอผลรวมของเทอมใน Sequence ใน
กรณของ Infinite Series นน เทอมทจะตองนามาบวกกนจะไมมทส นสด
• ถาใหแตละเทอมใน Sequence เปน an ดงนน Series คอ a1+a2+a3+… เชน
• NOTE: บางครง Index ของ Summation อาจจะเรมท 0 กได
+++===
+++==
∑
∑∞
=
∞
=
81
41
21
21;
21
1
3211
nnnn
nn
Sa
aaaaS
Convergence of Series • Series จะ Converge กตอเมอผลรวมของ
Term สามารถหาคาได – กลาวคอผลรวมของเทอมจนถง N ม Limit
• ถา Series ไม Converge มนจะ Diverge
∑∑=
∞→∞→
∞
=
===N
nnNNNn
n aSaS00
limlim
Power Series • Power Series เปน Infinite Series ใน
รปของ (c = Constant, an = coefficient, x เปน Variable ทอยรอบๆ c)
– Power Series ทสาคญคอ Taylor Series • ในกรณท c=0 เราได
– เชนในกรณของ Maclaurin Series
∑∞
=
−=0
)()(n
nn cxaxf
++++==∑∞
=
33
2210
0)( xaxaxaaxaxf
n
nn
Taylor Series/Maclaurin Series • ให f(x) เปน Function ทสามารถหา
Derivative ไดอยางไมจากดในชวงใกลเคยงกบ a ดงนน Taylor Series ของ f(x) คอ Power Series ในรปของ
• ในกรณท a = 0 บางครงเราเรยก Maclaurin Series
∑∞
=
−=+−+−+−+0
)(3
)3(2 )(
!)()(
!3)()(
!2)('')(
!1)(')(
n
nn
axn
afaxafaxafaxafaf
∑∞
=
=++++=0
)(3
)3(2
!)0(
!3)0(
!2)0(''
!1)0(')0()(
n
nn
xn
fxfxfxffxf
Example 1 • จาก
∑∞
=
=+++++==
=⇒===
+++++=
+−+−+−+−+=
0
432
)()(
4)4(
3)3(
2
4)4(
3)3(
2
!...
!4!3!21)( have We
1)0()(,...,)(',)( Suppose
..!4
)0(!3
)0(!2
)0('')0(')0()(
...)(!4
)()(!3
)()(!2
)(''))((')()(
k
kx
nxnxx
kxxxxxexf
fexfexfexf
xfxfxfxffxf
axafaxafaxafaxafafxf
389056099.7 :Value Actual
3873.740320
2565040128
72064
12032
2416
68
2421:ionApproximatOrder Eighth
356.772064
12032
2416
68
2421:ionApproximatOrder Sixth
72416
68
2421:ionApproximatOrder Forth
52421 :ionApproximatOrder Second
)2( find Example
2
2
2
2
2
2
=
=++++++++≈
=++++++≈
=++++≈
=++≈
=
e
e
e
e
e
ef
นกศกษาหาคาประมาณของ e-2 ถง 4 Significant Digit ไดหรอไม
Example 2:
∑∞
=
+
+−
=+−+++−++==
=−=−===
+++++=
0
12753
)4(
4)4(
3)3(
2
)!12()1(...
!70
!50
!300sin)( have We
),...sin(,cos)(''',sin)('',cos)(',sin)( Suppose
..!4
)0(!3
)0(!2
)0('')(')0()(
n
nn
xn
xxxxxxf
xfxxfxxfxxfxxf
xfxfxfxaffxf
479425538.050sin :Value Actual
479425533.0!75.0
!55.0
!35.05.050sin:ionApproximatOrder Eighth
47942708.0!55.0
!35.05.050sin:ionApproximatOrder Sixth
47916667.0!35.05.050sin:ionApproximatOrder Forth
5.050sin :ionApproximatOrder Secondradian5.0sin)5.0( find Example
753
53
3
=
=−+−≈
=+−≈
=−≈
≈=
.
.
.
.
.f
นกศกษาหาคาประมาณของ cos 0.5 ถง 8 Significant Digit ไดหรอไม
Taylor (Maclaurin) Series
∑
∑
∑
∑
∑
∑
∑
∑
∑
∞
=
∞
=
+
∞
=
∞
=
=
+
∞
=
∞
=
+
∞
=
∞
=
∀−+−=−
=
∀−+−=+
−=
<+−+−+=−−
=+
<=−
≠=−
−
<=−
≤<−−=+
<≤−−=−
∀+++++==
0
422
0
5312
4128
531612
81
021
2
02
0
10
1
1
1
432
0
;!4!2
1)!2()1(cos
;!5!3)!12(
)1(sin
1;1)4()!)(21(
)!2()1(1
1;)1(
1;1
1
1;1
1
11;)1()1log(
11;)1log(
;!4!3!2
1!
n
nn
n
nn
n
nn
n
n
m
n
nm
n
n
n
nn
n
nn
nx
xxxxn
x
xxxxxn
x
xxxxxxnnn
nx
xnxx
x
xxx
x
xxx
xnxx
xnxx
xxxxxnxe
MATLAB Program • จะสาธตการเขยน Function โดยใช MATLAB
– เขยน Function ทรบคา Vector x และ Vector y return z1 และ z2; x=-3:.1:3; y = -3:.1:3
– คานวณคา z1 = 2sin(x^2+y^2)/(x^2+y^2) – คานวณคา z2 = xsiny-ycosx – mesh (x,y,z1), (x,y,z2), (x,z1), (y,z2) และ (z1,z2) – Surf function, shading modes – Contour plot
• ศกษาวธการเขยน Function ใน MATLAB โดยดจาก Tutorial 4-5
END OF WEEK 10 • Download HW 7 • Next Week Chapter 9: Zeros of Functions
MATLAB Program • function [z1,z2]=test(x,y)
• % function [z1,z2]=test(x,y) • % Test matlab program calculate z1=f(x,y)=sin(x^2+y^2)/(x^2+y^2) • % and z2=f(x,y)=xsiny-ysinx
• n=length(x); • m=length(y); • z1=zeros(n,m); • z2=zeros(n,m); • for i = 1:n • for j= 1:m • t=x(i)^2+y(j)^2; • if (t ~= 0) • z1(i,j)=sin(t)/t; • else • z1(i,j)=1; • end • z2(i,j)=x(i)*sin(y(j))-y(j)*cos(x(i)); • end • end
MATLAB Program • function view2(x,y,z,az) • mesh(x,y,z); • view(0,az); • for i = 0:10:360 • view(i,az); • pause(0.05); • end
MATLAB Program • function view3(az)
• view(0,az); • for i = 0:10:360 • view(i,az); • pause(0.05); • end