0% found this document useful (0 votes)
26 views9 pages

Lab 2 NA

Uploaded by

alijamshed271
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Lab 2 NA

Uploaded by

alijamshed271
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

INDEX

SNO DATE LAB LAB OBJECTIVE SIGN


NO
INTRODUCTION TO PYTHON
1 28/9/23 1

2 5/9/23 2 MEASURING ERRORS, TYPES OF ERRORS, PROPAGATION OF


ERRORS, ROUND OFF AND TRUNCATION ERRORS
SNO DATE LAB LAB OBJECTIVE SIGN
NO
Bahria University,
Karachi Campus

LAB EXPERIMENT NO.


02

LIST OF TASKS
TASK NO OBJECTIVE
1 Write a Python program that calculates the absolute as well as relative error present in the
following measurements; Actual values: [11.0098, 167.902, 56.0567, 67.9860] Measured values:
[12.0001, 166.802, 55.0001, 69.0000]
2 Write a Python program the find the propagated error in the area and perimeter found for the
following measurements: - SQUARE (length : 45.09 cm ; uncertainty : 0.01 cm) - CIRCLE (radius :
34.90 cm ; uncertainty : 0.05 cm) - TRIANGLE (side1 : 70.9 m ; uncertainty : 0.23 m, side2 :
89.07cm ; uncertainty : 0.07 m, base : 76.07cm ; uncertainty : 0.04 m, height : 100.07cm ;
uncertainty : 0.05 m) - TRAPEZIUM(side1 : 670.9 m ; uncertainty : 0.53 m (parallel one), side2 :
849.07cm ; uncertainty : 0.27 m (parallel one) side3 : 376.07cm ; uncertainty : 0.74 m, side4 :
716.07cm ; uncertainty : 0.14 m, height : 231.07cm ; uncertainty : 0.25 m)

3 . Write a Python program that calculates the square root of following numbers using both the
math.sqrt function (which uses floating-point arithmetic) and a custom square root function
that uses integer arithmetic. Then, find and compare the results to observe the rounding error.
(56.90, 100.45, 67.90, 25.67, 56.67)
4
Write a python program which find the value of PI (π) using Taylor series, and then
find the truncating error occurred due to the use of finite number of terms. HINT:
arctan (1) = π/4, and formula for finding Tylor series for arctan is: 𝐚𝐫𝐜𝐭𝐚𝐧(𝐱) = ∑ (−1)
𝑛𝑧 2𝑛+1 2𝑛+1 ; ∞ 𝑛=0 |z| ≤ 1, z ≠ i, i

Submitted On:
Date: 16/10/23
Task No. 01:
Write a Python program that calculates the absolute as well as relative error present in the following measurements;
Actual values: [11.0098, 167.902, 56.0567, 67.9860] Measured values: [12.0001, 166.802, 55.0001, 69.0000]

Solution:

Task No. 02:


Write a Python program the find the propagated error in the area and perimeter found for the following measurements:
- SQUARE (length : 45.09 cm ; uncertainty : 0.01 cm) - CIRCLE (radius : 34.90 cm ; uncertainty : 0.05 cm) - TRIANGLE
(side1 : 70.9 m ; uncertainty : 0.23 m, side2 : 89.07cm ; uncertainty : 0.07 m, base : 76.07cm ; uncertainty : 0.04 m,
height : 100.07cm ; uncertainty : 0.05 m) - TRAPEZIUM(side1 : 670.9 m ; uncertainty : 0.53 m (parallel one), side2 :
849.07cm ; uncertainty : 0.27 m (parallel one) side3 : 376.07cm ; uncertainty : 0.74 m, side4 : 716.07cm ; uncertainty :
0.14 m, height : 231.07cm ; uncertainty : 0.25 m)

Solution:
import math

# Function to calculate the area and perimeter of a square


def square_area_perimeter(length, length_uncertainty):
area = length**2
perimeter = 4 * length
area_error = 2 * length * length_uncertainty
perimeter_error = 4 * length_uncertainty
return area, perimeter, area_error, perimeter_error

# Function to calculate the area and perimeter of a circle


def circle_area_perimeter(radius, radius_uncertainty):
area = math.pi * radius**2
perimeter = 2 * math.pi * radius
area_error = 2 * math.pi * radius * radius_uncertainty
perimeter_error = 2 * math.pi * radius_uncertainty
return area, perimeter, area_error, perimeter_error

# Function to calculate the area and perimeter of a triangle


def triangle_area_perimeter(side1, side2, base, height, side1_uncertainty,
side2_uncertainty, base_uncertainty, height_uncertainty):
area = 0.5 * base * height
perimeter = side1 + side2 + base
area_error = 0.5 * height * (base_uncertainty + height_uncertainty) + 0.5 * base
* height_uncertainty
perimeter_error = side1_uncertainty + side2_uncertainty + base_uncertainty
return area, perimeter, area_error, perimeter_error

# Function to calculate the area and perimeter of a trapezium


def trapezium_area_perimeter(side1, side2, side3, side4, height, side1_uncertainty,
side2_uncertainty, side3_uncertainty, side4_uncertainty, height_uncertainty):
area = 0.5 * (side1 + side3) * height
perimeter = side1 + side2 + side3 + side4
area_error = 0.5 * height * (side1_uncertainty + side3_uncertainty) + 0.5 *
(side1 + side3) * height_uncertainty
perimeter_error = side1_uncertainty + side2_uncertainty + side3_uncertainty +
side4_uncertainty
return area, perimeter, area_error, perimeter_error

# Square measurements and uncertainties


length_square = 45.09
length_uncertainty_square = 0.01

# Circle measurements and uncertainties


radius_circle = 34.90
radius_uncertainty_circle = 0.05

# Triangle measurements and uncertainties


side1_triangle = 70.9
side2_triangle = 89.07
base_triangle = 76.07
height_triangle = 100.07
side1_uncertainty_triangle = 0.23
side2_uncertainty_triangle = 0.07
base_uncertainty_triangle = 0.04
height_uncertainty_triangle = 0.05

# Trapezium measurements and uncertainties


side1_trapezium = 670.9
side2_trapezium = 849.07
side3_trapezium = 376.07
side4_trapezium = 716.07
height_trapezium = 231.07
side1_uncertainty_trapezium = 0.53
side2_uncertainty_trapezium = 0.27
side3_uncertainty_trapezium = 0.74
side4_uncertainty_trapezium = 0.14
height_uncertainty_trapezium = 0.25

# Calculate area and perimeter for each shape


area_square, perimeter_square, area_error_square, perimeter_error_square =
square_area_perimeter(length_square, length_uncertainty_square)
area_circle, perimeter_circle, area_error_circle, perimeter_error_circle =
circle_area_perimeter(radius_circle, radius_uncertainty_circle)
area_triangle, perimeter_triangle, area_error_triangle, perimeter_error_triangle =
triangle_area_perimeter(side1_triangle, side2_triangle, base_triangle,
height_triangle, side1_uncertainty_triangle, side2_uncertainty_triangle,
base_uncertainty_triangle, height_uncertainty_triangle)
area_trapezium, perimeter_trapezium, area_error_trapezium, perimeter_error_trapezium
= trapezium_area_perimeter(side1_trapezium, side2_trapezium, side3_trapezium,
side4_trapezium, height_trapezium, side1_uncertainty_trapezium,
side2_uncertainty_trapezium, side3_uncertainty_trapezium,
side4_uncertainty_trapezium, height_uncertainty_trapezium)

# Print results
print("Square:")
print(f"Area: {area_square} cm^2 (±{area_error_square} cm^2)")
print(f"Perimeter: {perimeter_square} cm (±{perimeter_error_square} cm)")
print()
print("Circle:")
print(f"Area: {area_circle} cm^2 (±{area_error_circle} cm^2)")
print(f"Perimeter: {perimeter_circle} cm (±{perimeter_error_circle} cm)")
print()
print("Triangle:")
print(f"Area: {area_triangle} cm^2 (±{area_error_triangle} cm^2)")
print(f"Perimeter: {perimeter_triangle} cm (±{perimeter_error_triangle} cm)")
print()
print("Trapezium:")
print(f"Area: {area_trapezium} cm^2 (±{area_error_trapezium} cm^2)")
print(f"Perimeter: {perimeter_trapezium} cm (±{perimeter_error_trapezium} cm)")
Output:

Task No. 03:


. Write a Python program that calculates the square root of following numbers using both the math.sqrt function (which
uses floating-point arithmetic) and a custom square root function that uses integer arithmetic. Then, find and compare
the results to observe the rounding error. (56.90, 100.45, 67.90, 25.67, 56.67)

Solution:
Output:

Task No. 04:


Write a python program which find the value of PI (π) using Taylor series, and then find the truncating error occurred
due to the use of finite number of terms. HINT: arctan (1) = π/4, and formula for finding Tylor series for arctan is:
𝐚𝐫𝐜𝐭𝐚𝐧(𝐱) = ∑ (−1) 𝑛𝑧 2𝑛+1 2𝑛+1 ; ∞ 𝑛=0 |z| ≤ 1, z ≠ i, i

Solution:

You might also like