07 Power Series
07 Power Series
Deret Pangkat
N
dn f (x) (x − x0 )n
X
f (x) = n
. (61.1)
n=0
dx
x=x0 n!
1 cos x0 (x − x0 ) 1
2 − sin x0 (x − x0 )2 2!
3 − cos x0 (x − x0 )3 3!
4 sin x0 (x − x0 )4 4!
5 cos x0 (x − x0 )5 5!
6 − sin x0 (x − x0 )6 6!
7 − cos x0 (x − x0 )7 7!
125
126 7. DERET PANGKAT
(x − x0 )2 (x − x0 )3
sin x = sin x0 + (x − x0 ) cos x0 − sin x0 − cos x0
2! 3!
(x − x0 )4 (x − x0 )5 (x − x0 )6 (61.2)
+ sin x0 + cos x0 − sin x0
4! 5! 6!
(x − x0 )7
− cos x0 + ...
7!
1 − sin x0 (x − x0 ) 1
2 − cos x0 (x − x0 )2 2!
3 sin x0 (x − x0 )3 3!
4 cos x0 (x − x0 )4 4!
5 − sin x0 (x − x0 )5 5!
6 − cos x0 (x − x0 )6 6!
7 sin x0 (x − x0 )7 7!
(x − x0 )2 (x − x0 )3
cos x = cos x0 − (x − x0 ) sin x0 − cos x0 + sin x0
2! 3!
(x − x0 )4 (x − x0 )5 (x − x0 )6 (61.3)
+ cos x0 − sin x0 − cos x0
4! 5! 6!
(x − x0 )7
+ sin x0 + ...
7!
1 exp x0 (x − x0 ) 1
2 exp x0 (x − x0 )2 2!
3 exp x0 (x − x0 )3 3!
4 exp x0 (x − x0 )4 4!
5 exp x0 (x − x0 )5 5!
6 exp x0 (x − x0 )6 6!
7 exp x0 (x − x0 )7 7!
(x − x0 )2 (x − x0 )3
exp x = exp x0 + (x − x0 ) exp x0 + exp x0 + exp x0
2! 3!
(x − x0 )4 (x − x0 )5 (x − x0 )6 (61.4)
+ exp x0 + exp x0 + exp x0
4! 5! 6!
(x − x0 )7
+ exp x0 + ...
7!
Problem 62. Apa nama ekspansi deret bila dipilih x0 = 0? Tuliskan ekspansi
deret tersebut untuk ketiga fungsi sebelumnya.
Ekspansi deret di sekitar x = x0 dengan memilih x0 = 0 dikenal sebagai deret
Maclaurin yang untuk fungsi-fungsi sin x, cos x, dan exp x akan menjadi
1 3 1 1
sin x = x − x + x5 − x7 + ..
3! 5! 7!
N
(62.1)
X (−1)n 2n+1
= x ,
n=0
(2n + 1)!
128 7. DERET PANGKAT
1 2 1 1
cos x = 1 − x + x4 − x6 + ..
2! 4! 6!
N
(62.2)
X (−1)n 2n
= x ,
n=0
(2n)!
dan
1 2 1 1 1 1 1
exp x = 1 + x + x + x3 + x4 + x5 + x6 + x7 + ..
2! 3! 4! 5! 6! 7!
N
(62.3)
X 1 n
= x .
n=0
n!
Problem 63. Buatlah suatu fungsi sederhana dalam C++ untuk menghitung
n! dengan x ∈ Z.
Fungsi untuk menghitung n! adalah sebagai berikut
dengan batasnya adalah n = 20. Lebih dari nilai tersebut jenis variabel unsigned
long long yang digunakan sudah tidak dapat mengakomodasinya, dengan hasil-
nya
$ ./factorial_0
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
129
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
Kode 64.1: Fungsi untuk menghasilkan koefisien expansi Maclaurin bagi f (x) =
sin x.
1 // Generate Maclaurin series coefficient for sin x
2 void sin ( double c []) {
3 double nom = 1 ;
4 for ( int n = 0 ; n <= N_MACLAURIN ; n ++) {
5 double cc ;
6 if ((0 . 5 * n ) = = ( n / 2)) {
7 cc = 0 ;
8 } else {
9 cc = nom / fact ( n ) ;
10 nom * = -1 ;
11 }
12 c [ n ] = cc ;
13 }
14 }
# f(x) = sin x
# N = 20
# c = [0, 1, 0, -0.166667, 0, 0.00833333, 0, -0.000198413, 0,
2.75573e-06, 0, -2.50521e-08, 0, 1.6059e-10, 0, -7.64716e-13,
131
0, 2.81146e-15, 0, -8.22064e-18]
# x sin x
0 0
0.0628319 0.0627905
0.125664 0.125333
0.188496 0.187381
0.251327 0.24869
0.314159 0.309017
..
6.15752 -0.126021
6.22035 -0.0636405
6.28319 -0.00104818
1 sin x
0.5
0
y
−0.5
−1
0 1 1 3 π 5 3 7 2π
4π 2π 4π 4π 2π 4π
x
Kode 65.1: Fungsi untuk menghasilkan koefisien expansi Maclaurin bagi f (x) =
cos x.
1 // Generate Maclaurin series coefficient for cos x
2 void cos ( double c []) {
3 double nom = 1 ;
132 7. DERET PANGKAT
35
36 // Display data for plotting
37 int M = 100 ;
38 double xmin = 0 ;
39 double xmax = 2 * M_PI ;
40 double dx = ( xmax - xmin ) / M ;
41 cout < < " # x \ tcos x " < < endl ;
42 for ( int i = 0 ; i <= M ; i ++) {
43 double x = i * dx ;
44 cout < < x < < " \ t " ;
45 cout < < polynom (N , c , x ) < < endl ;
46 }
47
48 // Terminate proram
49 return 0 ;
50 }
# f(x) = cos x
# N = 20
# c = [1, 0, -0.5, 0, 0.0416667, 0, -0.00138889, 0, 2.48016e-05, 0, -2.75573e-07, 0, 2.08768e-09, 0, -1.1470
# x cos x
0 1
0.0628319 0.998027
0.125664 0.992115
0.188496 0.982287
0.251327 0.968583
0.314159 0.951057
..
6.09469 0.980387
6.15752 0.989785
6.22035 0.995177
6.28319 0.996521
1 cos x
0.5
0
y
−0.5
−1
0 1 1 3 π 5 3 7 2π
4π 2π 4π 4π 2π 4π
x
# f(x) = exp x
# N = 20
# c = [1, 1, 0.5, 0.166667, 0.0416667, 0.00833333, 0.00138889, 0.000198413, 2.48016e-05, 2.75573e-06, 2.7557
# x exp x
0 1
0.0628319 1.06485
0.125664 1.1339
0.188496 1.20743
0.251327 1.28573
0.314159 1.36911
..
6.03186 416.486
6.09469 443.494
6.15752 472.253
136 7. DERET PANGKAT
6.22035 502.877
6.28319 535.486
600 exp x
400
y
200
0
0 1 1 3 π 5 3 7 2π
4π 2π 4π 4π 2π 4π
x
Problem 67. Sebuah fungsi f (x) berbentuk polinomial seperti dalam Per-
samaan (55.1)
N
X
f (x) = c n xn
n=0
g(x) = f (ax),
dengan a suatu konstanta real tidak nol. Fungsi g(x) akan memiliki koefisien
ekspansi deret pangkat c′ dengan isinya [c0’ c1’ c2’ .. c20’]. Tuliskanlah
hubungan antara c′i dengan ci dan buatlah suatu program sederhanan untuk
menghitung hal ini dalam bentuk void mul(double[], double). Gambarkan
pula hasilnya. Gunakan fungsi sin 2x sebagai contoh, yang dalam hal ini a = 2.
Hubungan antara c dan c′ adalah
c′n = cn k n (67.1)
Kode 67.2: Kode untuk menghitung c′ dan c antara f (x) dan g(x) = f (ax).
1 /*
2 series_sin_1 . cpp
3 Calculate coefficent of power series for sin ax
4
5 Sparisoma Viridi | dudung@gmail . com
6
7 Compile : g ++ series_sin_1 . cpp -o series_sin_1
8 Execute : . / series_sin_1 > data_sin_ax . txt
9
10 20171001
11 Create this program
12 */
13
14 # include < iostream >
15 # include < cmath >
16
17 using namespace std ;
18
19 # include " simplestats . h "
20 # include " series . h "
21
22 int main ( int argc , char * argv []) {
23 // Define array to store power series coefficents
24 int N = 20 ;
25 double c1 [ N ] ;
26 double c2 [ N ] ;
27
28 // Generate coefficient of Maclaurin series for sin x
138 7. DERET PANGKAT
29 sin ( c1 ) ;
30
31 // Generate coefficient of Maclaurin series for sin ax
32 cpy ( c1 , c2 ) ;
33 double a = 2 ;
34 mul ( c2 , a ) ;
35
36 // View coefficient
37 cout < < " # N = " < < N < < endl ;
38 cout < < " # a = " < < a < < endl ;
39 cout < < endl ;
40 cout < < " # f ( x ) = sin x " < < endl ;
41 cout < < " # c = [ " < < strval (N , c1 ) < < " ] " < < endl ;
42 cout < < endl ;
43 cout < < " # g ( x ) = sin ax " < < endl ;
44 cout < < " # c = [ " < < strval (N , c2 ) < < " ] " < < endl ;
45 cout < < endl ;
46
47 // Display data for plotting
48 int M = 100 ;
49 double xmin = 0 ;
50 double xmax = M_PI ;
51 double dx = ( xmax - xmin ) / M ;
52 cout < < " # x \ tsin ( x )\ tsin ( ax ) " < < endl ;
53 for ( int i = 0 ; i <= M ; i ++) {
54 double x = i * dx ;
55 cout < < x < < " \ t " ;
56 cout < < polynom (N , c1 , x ) < < " \ t " ;
57 cout < < polynom (N , c2 , x ) < < endl ;
58 }
59
60 // Terminate proram
61 return 0 ;
62 }
sehingga menghasilkan
# N = 20
# a = 2
# f(x) = sin x
# c = [0, 1, 0, -0.166667, 0, 0.00833333, 0, -0.000198413, 0, 2.75573e-06, 0, -2.50521e-08, 0, 1
# g(x) = sin ax
# c = [0, 2, 0, -1.33333, 0, 0.266667, 0, -0.0253968, 0, 0.00141093, 0, -5.13067e-05, 0, 1.31556
# x sin(x) sin(ax)
0 0 0
0.0314159 0.0314108 0.0627905
0.0628319 0.0627905 0.125333
139
0.5
0
y
Gambar 67.1: Hasil ekspansi Maclaurin dengan N = 10 untuk f (x) = sin x dan
g(x) = sin ax, dengan a = 2.
Perhatikan bahwa digambarkan x ∈ [0, 2π], yang lebih sedikit dari rentang se-
belumnya. Hal ini perlu dilakukan karena ekspansi sampai N = 20 tidak cukup
untuk x > π.
Problem 68. Terdapat suatu fungsi polinomial f (x) = x − 2x2 yang diwakili
oleh [0 1 -2] dan g(x) = 1 + 3x3 yang diwakili oleh [1 0 0 3]. Rumuskan
bagaimana memperoleh h(x) = f (x)g(x) dan koefisiennya dalam bentuk [c0 c1
c2 c3 c4 c5]. Buatlah program sederhana dalam C++ untuk memeriksa hasil
perumusan tersebut.
Bila koefisien fungsi f (x) diwakili oleh fi , g(x) oleh gi dan h(x) oleh hi maka
dapat ditunjukkan bahwa
i
X
hi = fj gi−j . (68.1)
j=0
$ ./mul_polynom_0
a = [0, 1, -2]
b = [1, 0, 0, 3]
c = [0, 1, -2, 0, 3, -6]
Problem 69. Buatlah suatu program sederhana agar tampilan fungsi poli-
nomial menjadi lebih ramah pengguna, lebih mudah untuk dilihat ketimbang
hanya [c0 c1 c2 .. cN]. Gunakan untuk mengalikan (1 + x) dengan (1 + 4x)
dan tampilkan hasilnya.
Fungsi untuk menampilkan bentuk fungsi polinomial
2 disp_polynom_0 . cpp
3 Dispay polynomial in string format
4
5 Sparisoma Viridi | dudung@gmail . com
6
7 Compile : g ++ disp_polynom_0 . cpp -o disp_polynom_0
8 Execute : . / disp_polynom_0
9
10 20171001
11 Create this program
12 */
13
14 # include < iostream >
15 # include < cmath >
16
17 using namespace std ;
18
19 # include " simplestats . h "
20 # include " series . h "
21
22 int main ( int argc , char * argv []) {
23 // Define two polynomial functions
24 double a [] = {1 , 1} ;
25 int Na = sizeof ( a ) / sizeof ( double ) ;
26 double b [] = {1 , 4} ;
27 int Nb = sizeof ( b ) / sizeof ( double ) ;
28
29 // Calcuate size of output , zero it , and calculate result
30 int Nc = 1 + ( Na - 1) + ( Nb - 1) ;
31 double c [ Nc ] ;
32 zero ( Nc , c ) ;
33 mul ( Na , a , Nb , b , Nc , c ) ;
34
35 // Show result
36 cout < < " f ( x ) = " < < strpoly ( Na , a ) < < endl ;
37 cout < < " g ( x ) = " < < strpoly ( Nb , b ) < < endl ;
38 cout < < " h ( x ) = f ( x ) g ( x ) = " < < strpoly ( Nc , c ) < < endl ;
39
40 // Terminate proram
41 return 0 ;
42 }
dengan hasilnya
$ ./disp_polynom_0
f(x) = 1x^0 + 1x^1
g(x) = 1x^0 + 4x^1
h(x) = f(x) g(x) = 1x^0 + 5x^1 + 4x^2
Tampilan ini belumlah terlalu ramah pengguna. Agar lebih baik, seharusnya
143
dan
string strLaTeXHTMLMathJax(string),
79 s = "";
80 } else {
81 s = " -" ;
82 }
83 }
84
85 ss < < s + cc + x ;
86 }
87 string s = ss . str () ;
88 stringstream ss2 ;
89 ss2 < < " $ " < < s < < " $ " ;
90 s = ss2 . str () ;
91 return s ;
92 }
93 string strLaTeXHTMLMathJax ( string polynom ) {
94 stringstream ss ;
95 ss < < " <! DOCTYPE html >" < < endl ;
96 ss < < " < html >" < < endl ;
97 ss < < " < head >" < < endl ;
98 ss < < " < meta charset = ’ ISO - 8859 -1 ’ >" < < endl ;
99 ss < < " < title > MathJax </ title >" < < endl ;
100 ss < < " < script type = ’ text / javascript ’ " ;
101 ss < < " async src = ’ https :// cdn . mathjax . org / mathjax / " ;
102 ss < < " latest / MathJax . js ? config = TeX - MML - AM_CHTML ’ >" ;
103 ss < < " </ script >" < < endl ;
104 ss < < " < script src = ’ script . js ’ > </ script >" < < endl ;
105 ss < < " </ head >" < < endl ;
106 ss < < " < body >" < < endl ;
107 ss < < " $ " < < polynom < < " $ " < < endl ;
108 ss < < " </ body >" < < endl ;
109 ss < < " </ html >" < < endl ;
110
111 string s = ss . str () ;
112 return s ;
113 }
$ ./disp_polynom_1
$ f(x) = -1 - 3x^2 + 4x^3 + 6x^5 + 10x^8 $
$ ./disp_polynom_1
<!DOCTYPE html>
<html>
<head>
<meta charset=’ISO-8859-1’>
<title>MathJax</title>
146 7. DERET PANGKAT
Gambar 70.1: Hasil integrasi HTML dan MathJax dalam menampilkan fungsi
polinomial (ditampilkan menggunakan Google Chrome).
<script type=’text/javascript’
async src=’https://cdn.mathjax.org/mathjax/latest/
MathJax.js?config=TeX-MML-AM_CHTML’></script>
</head>
<body>
$$ f(x) = -1 - 3x^2 + 4x^3 + 6x^5 + 10x^8 $$
</body>
</html>
lalu hasilnya dibuka dengan penjelajah web akan diperoleh Gambar 70.1.