Professional Documents
Culture Documents
Report: Assignment 1: Compute Definite Integral Using C Code 1. Problem
Report: Assignment 1: Compute Definite Integral Using C Code 1. Problem
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)
4. Compute ∆x
5. Compute xi
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:
4. compute dx
5. compute xi
6. compute f(xi)
8. compute integral
3
Figure 2: Result of trapezoidal rule of integral
4
5. Conclusion
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:
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.
1. Define function
4. Compute h
7. Compute xi
8. Compute f(xi)
6
Figure 4: Results of this case for 20 elements
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.