Anda di halaman 1dari 22

7

Deret Pangkat

Problem 61. Tuliskan ekspansi Deret Taylor di sekitar x = x0 untuk sem-


barang fungsi f (x) dan implementasikan untuk fungsi-fungsi sin x, cos x, dan
exp x.
Untuk sembarang fungsi f (x) ekspansi Deret Taylor di sekitar x = x0 adalah

N
dn f (x) (x − x0 )n
X
f (x) = n
. (61.1)
n=0
dx
x=x0 n!

Tabel 61.1: Komponen ekspansi Deret Taylor fungsi f (x) = sin x.


dn f (x)

n (x − x0 )n n!
dxn x=x0
0 sin x0 1 1

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

Untuk f (x) = sin x

(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!

yang suku-sukunya diperoleh dari Tabel 61.1.

Tabel 61.2: Komponen ekspansi Deret Taylor fungsi f (x) = cos x.


dn f (x)

n (x − x0 )n n!
dxn x=x0
0 cos x0 1 1

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!

Untuk f (x) = cos x

(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!

yang suku-sukunya diperoleh dari Tabel 61.2.


127

Tabel 61.3: Komponen ekspansi Deret Taylor fungsi f (x) = exp x.


dn f (x)

n (x − x0 )n n!
dxn x=x0
0 exp x0 1 1

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!

Untuk f (x) = exp x

(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!

yang suku-sukunya diperoleh dari Tabel 61.3.

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

Kode 63.1: Fungsi menghitung n! untuk 0 ≤ n ≤ 20.


1 unsigned long long fact ( int N ) {
2 unsigned long long F = 1 ;
3 for ( int i = 1 ; i <= N ; i ++) {
4 F *= i;
5 }
6 return F ;
7 }

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

sampai batas tersebut.

Problem 64. Perhatikan bahwa dari Persamaan (62.2) – (62.3) ketiganya


tak lain merupakan deret pangkat dengan koefisiennya ditentukan dari jenis
fungsinya. Dengan memanfaatkan double polynom(int, double[], double) dalam
Kode 54.1, buatlah fungsi untuk menghasilkan koefisien yang mewakili fungsi
sin x dan program untuk memanfaatkannya, serta gambarkan hasilnya.
Fungsi untuk menghasilkan koefisien expansi Maclaurin bagi fungsi sin x adalah

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 }

yang disimpan dalam pustaka series.h dan dipanggil melalui program

Kode 64.2: Program untuk untuk menghasilkan koefisien expansi Maclaurin


bagi f (x) = sin x.
1 /*
2 series_sin_0 . cpp
3 Calculate coefficent of power series for sin x
4
5 Sparisoma Viridi | dudung@gmail . com
6
7 Compile : g ++ series_sin_0 . cpp -o series_sin_0
130 7. DERET PANGKAT

8 Execute : . / series_sin_0 > data_sin . 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 c [ N ] ;
26
27 // Generate coefficient of Maclaurin series for sin x
28 sin ( c ) ;
29
30 // View coefficient
31 cout < < " # f ( x ) = sin x " < < endl ;
32 cout < < " # N = " < < N < < endl ;
33 cout < < " # c = [ " < < strval (N , c ) < < " ] " < < endl ;
34 cout < < endl ;
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 \ tsin 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 }

yang berkas keluarannya adalah

# 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

dengan hasil plotnya dalam Gambar 64.1.

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

Gambar 64.1: Hasil ekspansi Maclaurin dengan N = 10 untuk f (x) = sin x.

Problem 65. Perhatikan bahwa dari Persamaan (62.2) – (62.3) ketiganya


tak lain merupakan deret pangkat dengan koefisiennya ditentukan dari jenis
fungsinya. Dengan memanfaatkan double polynom(int, double[], double) dalam
Kode 54.1, buatlah fungsi untuk menghasilkan koefisien yang mewakili fungsi
cos x dan program untuk memanfaatkannya, serta gambarkan hasilnya.
Fungsi untuk menghasilkan koefisien expansi Maclaurin bagi fungsi cos x adalah

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

4 for ( int n = 0 ; n <= N_MACLAURIN ; n ++) {


5 double cc ;
6 if ((0 . 5 * n ) = = ( n / 2)) {
7 cc = nom / fact ( n ) ;
8 nom * = -1 ;
9 } else {
10 cc = 0 ;
11 }
12 c [ n ] = cc ;
13 }
14 }

yang disimpan dalam pustaka series.h dan dipanggil melalui program


Kode 65.2: Program untuk untuk menghasilkan koefisien expansi Maclaurin
bagi f (x) = cos x.
1 /*
2 series_cos_0 . cpp
3 Calculate coefficent of power series for cos x
4
5 Sparisoma Viridi | dudung@gmail . com
6
7 Compile : g ++ series_cos_0 . cpp -o series_cos_0
8 Execute : . / series_cos_0 > data_cos . 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 c [ N ] ;
26
27 // Generate coefficient of Maclaurin series for cos x
28 cos ( c ) ;
29
30 // View coefficient
31 cout < < " # f ( x ) = cos x " < < endl ;
32 cout < < " # N = " < < N < < endl ;
33 cout < < " # c = [ " < < strval (N , c ) < < " ] " < < endl ;
34 cout < < endl ;
133

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 }

yang berkas keluarannya adalah

# 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

dengan hasil plotnya dalam Gambar 65.1.

Problem 66. Perhatikan bahwa dari Persamaan (62.2) – (62.3) ketiganya


tak lain merupakan deret pangkat dengan koefisiennya ditentukan dari jenis
fungsinya. Dengan memanfaatkan double polynom(int, double[], double) dalam
Kode 54.1, buatlah fungsi untuk menghasilkan koefisien yang mewakili fungsi
exp x dan program untuk memanfaatkannya, serta gambarkan hasilnya.
Fungsi untuk menghasilkan koefisien expansi Maclaurin bagi fungsi exp x adalah
Kode 66.1: Fungsi untuk menghasilkan koefisien expansi Maclaurin bagi f (x) =
exp x.
1 // Generate Maclaurin series coefficient for exp x
134 7. DERET PANGKAT

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

Gambar 65.1: Hasil ekspansi Maclaurin dengan N = 10 untuk f (x) = cos x.

2 void exp ( double c []) {


3 double nom = 1 ;
4 for ( int n = 0 ; n <= N_MACLAURIN ; n ++) {
5 double cc = nom / fact ( n ) ;
6 c [ n ] = cc ;
7 }
8 }

yang disimpan dalam pustaka series.h dan dipanggil melalui program

Kode 66.2: Program untuk untuk menghasilkan koefisien expansi Maclaurin


bagi f (x) = exp x.
1 /*
2 series_exp_0 . cpp
3 Calculate coefficent of power series for exp x
4
5 Sparisoma Viridi | dudung@gmail . com
6
7 Compile : g ++ series_exp_0 . cpp -o series_exp_0
8 Execute : . / series_exp_0 > data_exp . txt
9
10 20171001
11 Create this program
12 */
13
14 # include < iostream >
15 # include < cmath >
16
17 using namespace std ;
18
135

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 c [ N ] ;
26
27 // Generate coefficient of Maclaurin series for exp x
28 exp ( c ) ;
29
30 // View coefficient
31 cout < < " # f ( x ) = exp x " < < endl ;
32 cout < < " # N = " < < N < < endl ;
33 cout < < " # c = [ " < < strval (N , c ) < < " ] " < < endl ;
34 cout < < endl ;
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 \ texp 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 }

yang berkas keluarannya adalah

# 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

dengan hasil plotnya dalam Gambar 66.1.

600 exp x

400
y

200

0
0 1 1 3 π 5 3 7 2π
4π 2π 4π 4π 2π 4π
x

Gambar 66.1: Hasil ekspansi Maclaurin dengan N = 10 untuk f (x) = exp x.

Problem 67. Sebuah fungsi f (x) berbentuk polinomial seperti dalam Per-
samaan (55.1)

N
X
f (x) = c n xn
n=0

memiliki koefisien ekspansi deret pangkat c dengan isinya [c0 c1 c2 .. c20].


Sebuah fungsi baru dapat didefinisikan, misalnya

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)

dengan fungsi yang dapat menerapkannya adalah


137

Kode 67.1: Dua buah fungsi untuk menghitung c′ dan c.


1 // Calculate c ’ from g ( x ) = f ( ax )
2 void mul ( double c [] , double a ) {
3 double k = 1 ;
4 for ( int n = 0 ; n <= N_MACLAURIN ; n ++) {
5 double cc = c [ n ] * k ;
6 c [ n ] = cc ;
7 k *= a;
8 }
9 }
10
11 // Copy from c to d
12 void cpy ( double c [] , double d []) {
13 for ( int n = 0 ; n <= N_MACLAURIN ; n ++) {
14 d[n] = c[n];
15 }
16 }

yang dipanggil melalui

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.0942478 0.0941083 0.187381


0.125664 0.125333 0.24869
..
3.04734 0.0941083 -0.187937
3.07876 0.0627905 -0.126021
3.11018 0.0314108 -0.0636405
3.14159 -5.28918e-10 -0.00104818

dengan grafiknya diberikan oleh Gambar 67.1.

0.5

0
y

−0.5 f (x) = sin x


g(x) = sin ax
h(x) = sin x/a
−1
0 1 1 3 π
4π 2π 4π
x

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

Penerapannya dapat dilakukan dengan fungsi yang termasuk dalam pustaka


series.h
140 7. DERET PANGKAT

Kode 68.1: Fungsi-fungsi untuk mengalikan dua polinomial.


1 // Make zero all value
2 void zero ( int N , double c []) {
3 for ( int i = 0 ; i < N ; i ++) {
4 c[i] = 0;
5 }
6 }
7
8 // Calculate a with b and store result in c
9 void mul ( int Na , double a [] , int Nb , double b [] ,
10 int Nc , double c []) {
11 for ( int ia = 0 ; ia < Na ; ia ++) {
12 for ( int ib = 0 ; ib < Nb ; ib ++) {
13 c [ ia + ib ] + = ( a [ ia ] * b [ ib ]) ;
14 }
15 }
16 }

yang dipanggil melalui

Kode 68.2: Mengalikan dua polinomial.


1 /*
2 mul_polynom_0 . cpp
3 Multiply two polynomial
4
5 Sparisoma Viridi | dudung@gmail . com
6
7 Compile : g ++ mul_polynom_0 . cpp -o mul_polynom_0
8 Execute : . / mul_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 [] = {0 , 1 , - 2} ;
25 int Na = sizeof ( a ) / sizeof ( double ) ;
26 double b [] = {1 , 0 , 0 , 3} ;
27 int Nb = sizeof ( b ) / sizeof ( double ) ;
28
141

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 < < " a = [ " < < strval ( Na , a ) < < " ] " < < endl ;
37 cout < < " b = [ " < < strval ( Nb , b ) < < " ] " < < endl ;
38 cout < < " c = [ " < < strval ( Nc , c ) < < " ] " < < endl ;
39
40 // Terminate proram
41 return 0 ;
42 }

dan memberikan hasil

$ ./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

Kode 69.1: Konversi koefisien ke string.


1 // Creat string representation of polynomial
2 string strpoly ( int N , double c []) {
3 stringstream ss ;
4 for ( int i = 0 ; i < N ; i ++) {
5 ss < < c [ i ] < < " x ^ " < < i ;
6 if ( i < N - 1) {
7 ss < < " + " ;
8 }
9 }
10 string s = ss . str () ;
11 return s ;
12 }

yang dipanggil melalui

Kode 69.2: Program untuk menampikan polinomial.


1 /*
142 7. DERET PANGKAT

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

x0 tidak perlu ditampilkan. Fleksibilitas dapat ditingkatkan dengan menuliskan


keluaran dalam bentuk HTML atau LATEX.

Problem 70. Dengan menggunakan LATEXdan MathJax [8] buatlah suatu


fungsi dengan nama
string strpolyLaTeX(int, double[])

dan
string strLaTeXHTMLMathJax(string),

di mana yang pertama akan menghasilkan representasi fungsi polinomial dalam


LATEXdan yang kedua akan membungkusnya dalam format HTML yang dilengkapi
dengan MathJax. Tampilkan hasilnya.
Program untuk menghasilkan representasi fungsi polinomial dalam LATEXdan ke-
mudian membungkusnya dalam format HTML yang dilengkapi dengan MathJax
adalah sebagai berikut

Kode 70.1: Program untuk merepresentasikan polinomial dalam LATEXdan


HTML-MathJax.
1 /*
2 disp_polynom_1 . cpp
3 Dispay polynomial in string format LaTeX dan HTML - MathJax
4
5 Sparisoma Viridi | dudung@gmail . com
6
7 Compile : g ++ disp_polynom_1 . cpp -o disp_polynom_1
8 Execute : . / disp_polynom_1
9
10 20171001
11 Create this program
12 */
13
14 # include < iostream >
15 # include < cmath >
16 # include < cstring >
17 # include < sstream >
18
19 using namespace std ;
20
21 # include " simplestats . h "
22 # include " series . h "
23
24 // List the functions
25 string strpolyLaTeX ( const string , int , double []) ;
26 string strLaTeXHTMLMathJax ( string ) ;
27
28 int main ( int argc , char * argv []) {
144 7. DERET PANGKAT

29 // Define two polynomial functions


30 double c [] = { -1 , 0 , -3 , 4 , 0 , 6 , 0 , 0 , 10} ;
31 int N = sizeof ( c ) / sizeof ( double ) ;
32
33 // Show polynom
34 string LaTeXpoly = strpolyLaTeX ( " f ( x ) " , N , c ) ;
35 string HTMLpoly = strLaTeXHTMLMathJax ( LaTeXpoly ) ;
36 // cout < < LaTeXpoly < < endl ;
37 cout < < HTMLpoly < < endl ;
38
39
40 // Terminate proram
41 return 0 ;
42 }
43
44 // Present polynomial in string using LaTeX
45 string strpolyLaTeX ( const string fx , int N , double c []) {
46 stringstream ss ;
47 ss < < fx < < " = " ;
48 for ( int i = 0 ; i < N ; i ++) {
49 string x ;
50 if ( i = = 0) {
51 x = "";
52 } else if ( i = = 1) {
53 x = "x";
54 } else {
55 stringstream sx ;
56 sx < < " x ^ " < < i ;
57 x = sx . str () ;
58 }
59
60 string s ;
61 if ( c [ i ] > 0) {
62 s = " + ";
63 } else {
64 s = " - ";
65 }
66
67 stringstream cs ;
68 cs < < fabs ( c [ i ]) ;
69 string cc = cs . str () ;
70
71 if ( c [ i ] = = 0) {
72 x = "";
73 s = "";
74 cc = " " ;
75 }
76
77 if ( i = = 0) {
78 if ( c [ i ] >= 0) {
145

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 }

yang akan memberikan hasil

$ ./disp_polynom_1
$ f(x) = -1 - 3x^2 + 4x^3 + 6x^5 + 10x^8 $

atau f (x) = −1 − 3x2 + 4x3 + 6x5 + 10x8 . Dan

$ ./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>

yang apabila disimpan dalam berkas bernama HTMLpoly.html dengan cara

$ ./disp_polynom_1 > HTMLpoly.html

lalu hasilnya dibuka dengan penjelajah web akan diperoleh Gambar 70.1.

Anda mungkin juga menyukai