You are on page 1of 8

Report: Assignment 1

Yosua Heru Irawan


2019 - 10 - 09

Compute definite integral using c++code


1. Problem
Known function as follow:

f (x) = x3 + 2x2 .......................(1)

Use the rectangular rule and trapezoidal rule to estimate integral of the
function above from a = 0 to b = 2, after that, compare the results with
analytical solution.

2. Analytical solution
2
x4 2 3 2 24 2
Z
x3 + 2x2 dx = [ + x ]0 = [ + 23 ] − [0] = 9.33
0 4 3 4 3
The analytical solution for this problem is 9.33.

3. Rectangular rule
Midpoint rectangular rule:
n
X
I = ∆x[ f (a + ∆x(i − 0.5))]..................(2)
i=1

∆x = b−a
n
n = number of rectangle element
To solve the problem using c++ code:

1
1. Define the function f(x)

2. Define boundary of integral

3. Define number of rectangle elements

4. Compute ∆x

5. Compute xi

6. Compute function f(xi)

7. Compute single rectangle area

8. Compute total integral

Figure 1: Result of rectangular rule of integral

2
Table 1: Results of this code for 10 rectangle elements
Elements Results Errors (o/o)
1 6 35.71
2 8.5 8.92
3 8.96 3.96
4 9.12 2.23
5 9.2 1.42
6 9.24 0.99
7 9.26 0.72
8 9.28 0.55
9 9.29 0.44
10 9.3 0.35

4. Trapezoidal rule
The multiple-application trapezoidal rule:
Pn−1
f (x0 ) + 2 i=1 f (xi ) + f (xn )
I = (b − a)
2n
To solve this problem using c+ + code:

1. define function f(x)

2. define interval of integral

3. define number of trapezoidal element

4. compute dx

5. compute xi

6. compute f(xi)

7. compute xi for i = 1 until i = n - 1

8. compute integral

3
Figure 2: Result of trapezoidal rule of integral

Table 2: Result of this code for 10 trapezoidal elements


Element Results Errors (o/o)
1 16 71.42
2 11 17.85
3 10.07 7.93
4 9.75 4.46
5 9.6 2.85
6 9.51 1.98
7 9.46 1.45
8 9.43 1.11
9 9.41 0.88
10 9.4 0.71

4
5. Conclusion

Figure 3: Comparassion of rectangular and trapezoidal rule

Integral calculations using c ++ code have been completed. each method


uses 10 elements for the calculation process. after that, the results of the
numerical calculation are compared with the exact solution. in this case, the
rectangular rule gives better results than the trapezoidal rule. this can be
seen from the smaller error percentage of rectangular rule calculation results.

6. Plan to Study
learn and practice c ++ code programming to solve numerical integrals simp-
son 1/3 rule method using c ++ code.

5
1. Problem
Known function as follow:

f (x) = 0.2 + 25x − 200x2 + 675x3 − 900x4 + 400x5

Use the Simpson 1/3 rule to compute integral of the function above from a =
0 to b = 0.8. The exact value of the integral can be determined analytically
to be 1.640533.

2. Simpson 1/3 rule


n−1
P n−2
P
f (x0 ) + 4 f (xi ) + 2 +f (xn )
i=1,3,5 j=2,4,6
I = (b − a)
3n
n = number of elements (this method can be employed only if the number
of elements is even)
a = lower limit
b = upper limit
Step by step to solve this problem using c++ code:

1. Define function

2. Define limit of integral

3. Define number of elements

4. Compute h

5. Define array to save xi

6. Define array to save f(xi)

7. Compute xi

8. Compute f(xi)

9. Compute sum of odd elements

10. Compute sum of even elements

11. Compute total integral

6
Figure 4: Results of this case for 20 elements

Table 3: Results of this case for 20 elements


Elements Results Errors (o/o)
2 1.36747 16.64
4 1.62347 1.040
6 1.63716 0.205
8 1.63947 0.064
10 1.6401 0.026
12 1.64032 0.012
14 1.64042 0.006
16 1.64047 0.003
18 1.64049 0.002
20 1.64051 0.001

7
3. Conclussion
to solve a case using c++ code, we must first know step by step or the process
that must be taken to solve the case. after that, then we try to write the
step by step into c++ code.

4. Plan to study
Learn to compute numerical differentiation using c++ code.

You might also like