Numerical Analysis

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

LABORATORY 4

NUMERICAL INTEGRATION

Objectives

1.To solve for the area under the curve using the Trapezoidal Rule and the Simpson’s Rule.

2.To implement the Trapezoidal Rule and the Simpson’s Rule using Python programming.

ALGORITHM

#TRAPEZOIDAL RULE

1. Input the function to integrate, lower limit, upper limit, number of strips, and exact value.
2. Define a function using eval ().
3. Calculate the height of each trapezoid as the difference between the upper and lower limits
divided by the number of strips.
4. Initialize the sum variable and x variable to 0.
5. Calculate the values using the trapezoidal formula.
6. Calculate the trapezoidal area using the formula height/2 times the sum.
7. Round the trapezoidal area to three decimal places.
8. Calculate the relative error by using the formula of relative error formula.
9. Output the numerical integral, exact integral, and relative error to the console.

#SIMPSONS RULE

1. Input the function to integrate, lower limit, upper limit, number of strips, and exact value.
2. Define a function using eval ().
3. Calculate the height of each parabola as the difference between the upper and lower limits
divided by the number of strips.
4. Initialize the sum variable and x variable to 0.
5. Calculate the values using the Simpson’s formula.
6. Calculate the Simpson’s area using the formula height/3 times the sum.
7. Round the trapezoidal area to three decimal places.
8. Calculate the relative error by using the formula of relative error formula.
9. Output the numerical integral, exact integral, and relative error.
FLOWCHART

TRAPEZOIDAL METHOD SIMPSON’S RULE


LAB WORK

1. Write the Python code which solves for the numerical integral and relative error of the given
function below using the two methods with 10strips.

𝟏𝟎
∫ 𝟔𝒙𝟑 − 𝟕𝒙𝟐 + 𝟖𝒙 − 𝟏𝟎 𝒅𝒙
−𝟏

TRAPEZOIDAL RULE:

# Trapezoidal Rule

import math

function = input("Enter Function: ")

def f(x):
return eval(function)

lower_limit = float(input("Enter Lower Limit: "))


upper_limit = float(input("Enter Upper Limit: "))
strips = float(input("Enter number of strips: "))
Exact_value = float(input("Enter exact value: "))

height = (upper_limit - lower_limit) / strips


x = 0
sum = 0

while strips >= x:


x1 = lower_limit + (height * x)
if x == 0 or x == strips:
sum += f(x1)
else:
sum += 2 * f(x1)
x += 1

trapezoid = (height / 2) * sum


trapezoid_round = round(trapezoid, 3)

relative_error = round(abs((Exact_value - trapezoid_round) / Exact_value *


100), 3)

print(f'Numerical Integral (n = {strips}) is {trapezoid_round}.')


print(f'Exact Integral is {Exact_value}.')
print(f'Relative Error is {relative_error}%')
TRAPEZOIDAL RULE (OUTPUT):

SIMPSONS RULE:

# Simpson Rule

import math

function = input("Enter Function: ")

def f(x):
return eval(function)

lower_limit = float(input("Enter Lower Limit: "))


Upper_limit = float(input("Enter Upper Limit: "))
Strips = float(input("Enter number of strips: "))
Exact_value = float(input("Enter exact value: "))

Height = (Upper_limit - lower_limit) / Strips


x = 0
sum = 0

while x <= Strips:


x1 = lower_limit + (Height * x)
if x == 0 or x == Strips:
sum += f(x1)
elif x % 2 == 0:
sum += 2 * f(x1)
else:
sum += 4 * f(x1)
x += 1

Simpsons = (Height / 3) * sum


Simpsons_round = round(Simpsons, 3)

relative_error = round(abs((Exact_value - Simpsons) / Exact_value * 100), 3)

print(f'Numerical Integral (n = {Strips}) is {Simpsons_round}')


print(f'Exact Integral is {Exact_value}')
print(f'Relative Error is {relative_error}%')

SIMPSONS RULE (OUTPUT):

2. Write the Python code which solves for the numerical integral and relative error of the given
function below using the two methods with 16 strips.

−𝟔
∫ 𝟐𝟏𝒙𝟐 𝒔𝒊𝒏(𝟕𝒙𝟑 − 𝟖)𝒅𝒙
−𝟏𝟓

TRAPEZOIDAL RULE:

# Trapezoidal Rule

import math

function = input("Enter Function: ")

def f(x):
return eval(function)

lower_limit = float(input("Enter Lower Limit: "))


upper_limit = float(input("Enter Upper Limit: "))
strips = float(input("Enter number of strips: "))
Exact_value = float(input("Enter exact value: "))

height = (upper_limit - lower_limit) / strips


x = 0
sum = 0

while strips >= x:


x1 = lower_limit + (height * x)
if x == 0 or x == strips:
sum += f(x1)
else:
sum += 2 * f(x1)
x += 1

trapezoid = (height / 2) * sum


trapezoid_round = round(trapezoid, 3)

relative_error = round(abs((Exact_value - trapezoid_round) / Exact_value *


100), 3)

print(f'Numerical Integral (n = {strips}) is {trapezoid_round}.')


print(f'Exact Integral is {Exact_value}.')
print(f'Relative Error is {relative_error}%')

TRAPEZOIDAL RULE (OUTPUT):

SIMPSONS RULE:

# Simpson Rule

import math

function = input("Enter Function: ")

def f(x):
return eval(function)

lower_limit = float(input("Enter Lower Limit: "))


Upper_limit = float(input("Enter Upper Limit: "))
Strips = float(input("Enter number of strips: "))
Exact_value = float(input("Enter exact value: "))

Height = (Upper_limit - lower_limit) / Strips


x = 0
sum = 0

while x <= Strips:


x1 = lower_limit + (Height * x)
if x == 0 or x == Strips:
sum += f(x1)
elif x % 2 == 0:
sum += 2 * f(x1)
else:
sum += 4 * f(x1)
x += 1

Simpsons = (Height / 3) * sum


Simpsons_round = round(Simpsons, 3)

relative_error = round(abs((Exact_value - Simpsons) / Exact_value * 100), 3)

print(f'Numerical Integral (n = {Strips}) is {Simpsons_round}')


print(f'Exact Integral is {Exact_value}')
print(f'Relative Error is {relative_error}%')

SIMPSONS RULE (OUTPUT):

Discussion and Analysis

1. Given the function, fill out the necessary information for the table below.
11
2𝑥 3 + 3𝑥
∫ 𝑑𝑥
𝑥 4 + 3𝑥 2
1

Trapezoidal Rule Simpson’s Rule


No. of Strips
Numerical Integral Relative Error Numerical Integral Relative Error
4 4.453 8.214% 4.212 2.348%
6 4.282 4.058% 4.154 0.959%
8 4.214 2.406% 4.135 0.48%
10 4.181 1.604% 4.126 0.266%
12 4.162 1.142% 4.122 0.158%
14 4.15 0.851% 4.119 0.099%
16 4.142 0.656% 4.118 0.064%
18 4.136 0.51% 4.117 0.043%
20 4.132 0.413% 4.116 0.03%

2. Based on the result of the table above, how will you compare the Trapezoidal Rule and the
Simpson’s Rule? What are the similarities and differences of the two methods?
- The similarities about these two methods are that it approximates an integral by using linear
function that solves the error which tells what is more accurate. The difference about the result
shows in the table is that even though they are the same approaching 0 depends on how many
strips you are using. We can see in the table that the error from Simpson’s Rule is much lower
than the trapezoidal rule and much near to approach 0 from 4-20 No. of strips.

You might also like