Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views13 pages

Assignment 8 Report

The document outlines a Python program that calculates the integral of sin(x) from -2π to 2π using both Python lists and NumPy, comparing the time taken and maximum error for various mesh sizes. It also includes a section for converting a system of linear equations into an upper diagonal matrix and finding the solution. The results indicate that NumPy is faster and more accurate than using lists for large mesh sizes.

Uploaded by

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

Assignment 8 Report

The document outlines a Python program that calculates the integral of sin(x) from -2π to 2π using both Python lists and NumPy, comparing the time taken and maximum error for various mesh sizes. It also includes a section for converting a system of linear equations into an upper diagonal matrix and finding the solution. The results indicate that NumPy is faster and more accurate than using lists for large mesh sizes.

Uploaded by

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

Q1.

(a) Please plot sin(x) and area under the curve for sin(x) from -2π to 2π on
the same figure using python lists. Try this for a “mesh” of 100, 1000, 10000,
100000, 1000000 and 10000000 points. For a 100-point mesh, the distance
between 2 values on the x-axis would be 4π/100. Compare the results to the
closed form expression of the integral and find the print the maximum error.
Print the time required to find calculate the area for each mesh size. Please do
not include the time required for plotting or doing other calculations.
import math
import matplotlib.pyplot as plt
import time

inp=[2*(math.pi)/100*i for i in range(-100,101)]


output=[]
for i in inp:
output.append(math.sin(i))

mesh=0
a=time.time()
for i in range(0,200):
mesh+=(output[i] *2*math.pi/100)

b=time.time()
print(b-a)

print("integral :",mesh)
print("Maximum Error is " , mesh)
plt.figure(1)
plt.plot(inp,output)
plt.savefig("assign8fig_1")

import math
import matplotlib.pyplot as plt
import time

inp=[2*(math.pi)/1000*i for i in range(-1000,1001)]

output=[]
for i in inp:
output.append(math.sin(i))

mesh=0
a=time.time()
for i in range(0,2000):
mesh+=(output[i] *2*math.pi/1000)

b=time.time()
print(b-a)
print("integral :",mesh)
print("Maximum Error is " , mesh)
plt.figure(2)
plt.plot(inp,output)
plt.savefig("assign8fig_2")

import math
import matplotlib.pyplot as plt
import time

inp=[2*(math.pi)/10000*i for i in range(-10000,10001)]

output=[]
for i in inp:
output.append(math.sin(i))

mesh=0
a=time.time()
for i in range(0,20000):
mesh+=(output[i] *2*math.pi/10000)

b=time.time()
print(b-a)
print("integral :",mesh)
print("Maximum Error is " , mesh)
plt.figure(3)
plt.plot(inp,output)
plt.savefig("assign8fig_3")

import math
import matplotlib.pyplot as plt
import time

inp=[((2*(math.pi))/100000)*i for i in range(-100000,100001)]

output=[]
for i in inp:
output.append(math.sin(i))

mesh=0
a=time.time()
for i in range(0,200000):
mesh+=(output[i] *2*math.pi/100000)

b=time.time()
print(b-a)
print("integral :",mesh)
print("Maximum Error is " , mesh)
plt.figure(4)
plt.plot(inp,output)
plt.savefig("assign8fig_4")

import math
import matplotlib.pyplot as plt
import time
inp=[(2*(math.pi)/1000000)*i for i in range(-1000000,1000001)]

output=[]
for i in inp:
output.append(math.sin(i))

mesh=0
a=time.time()
for i in range(0,2000000):
mesh+=(output[i] *2*math.pi/1000000)

b=time.time()
print(b-a)
print("integral :",mesh)
print("Maximum Error is " , mesh)
plt.figure(5)
plt.plot(inp,output)
plt.savefig("assign8fig_5")

import math
import matplotlib.pyplot as plt
import time
inp=[((2*(math.pi))/10000000)*i for i in range(-10000000,10000001)]

output=[]
for i in inp:
output.append(math.sin(i))

mesh=0
a=time.time()
for i in range(0,20000000):
mesh+=(output[i] *2*math.pi/10000000)

b=time.time()
print(b-a)
print("integral :",mesh)
print("Maximum Error is " , mesh)
plt.figure(6)
plt.plot(inp,output)
plt.savefig("assign8fig_6")
1st output:

2nd output:

3rd output:

4th output:
5th output:

6th output:

(b) Now, repeat part (a) using NumPy. Compare the


time required to do the same
calculations and the error for each mesh size.

.. import math
import matplotlib.pyplot as plt
import numpy as np
import time

inp=np.array([((2*(math.pi))/100)*i for i in range(-100,101)])

output=np.array([])
for i in inp:
output = np.append(output, math.sin(i))

mesh=0
a=time.time()
for i in range(0,200):
mesh+=(output[i] *2*math.pi/100)

b=time.time()
print(b-a)

print(mesh)
print("Maximum Error is " , mesh)

plt.figure(1)
plt.plot(inp,output)
plt.show()

import math
import matplotlib.pyplot as plt
import numpy as np
import time

inp=np.array([((2*(math.pi))/1000)*i for i in range(-1000,1001)])

output=np.array([])
for i in inp:
output = np.append(output, math.sin(i))

mesh=0
a=time.time()
for i in range(0,2000):
mesh+=(output[i] *2*math.pi/1000)

b=time.time()
print(b-a)

print(mesh)
print("Maximum Error is " , mesh)

plt.figure(2)
plt.plot(inp,output)
plt.show()

import math
import matplotlib.pyplot as plt
import numpy as np
import time

inp=np.array([((2*(math.pi))/10000)*i for i in range(-10000,10001)])

output=np.array([])
for i in inp:
output = np.append(output, math.sin(i))
mesh=0
a=time.time()
for i in range(0,20000):
mesh+=(output[i] *2*math.pi/10000)

b=time.time()
print(b-a)

print(mesh)
print("Maximum Error is " , mesh)

plt.figure(3)
plt.plot(inp,output)
plt.show()

import math
import matplotlib.pyplot as plt
import numpy as np
import time

inp=np.array([((2*(math.pi))/100000)*i for i in range(-100000,100001)])

output=np.array([])
for i in inp:
output = np.append(output, math.sin(i))

mesh=0
a=time.time()
for i in range(0,200000):
mesh+=(output[i] *2*math.pi/100000)

b=time.time()
print(b-a)

print(mesh)
print("Maximum Error is " , mesh)

plt.figure(4)
plt.plot(inp,output)
plt.show()
import math
import matplotlib.pyplot as plt

import numpy as np
import time

inp=np.array([((2*(math.pi))/1000000)*i for i in range(-1000000,1000001)])

output=np.array([])
for i in inp:
output = np.append(output, math.sin(i))

mesh=0
a=time.time()
for i in range(0,2000000):
mesh+=(output[i] *2*math.pi/1000000)

b=time.time()
print(b-a)

print(mesh)
print("Maximum Error is " , mesh)

plt.figure(5)
plt.plot(inp,output)
plt.show()

import math
import matplotlib.pyplot as plt
import numpy as np
import time

inp=np.array([((2*(math.pi))/100000000)*i for i in range(-10000000,10000001)])

output=np.array([])
for i in inp:
output = np.append(output, math.sin(i))

mesh=0
a=time.time()
for i in range(0,200000000):
mesh+=(output[i] *2*math.pi/100000000)

b=time.time()
print(b-a)

print(mesh)
print("Maximum Error is " , mesh)

plt.figure(1)
plt.plot(inp,output)
plt.show()

(c) Which approach is faster? Why?


Solution:
NumPy is generally faster than using lists with the math
library for this task. NumPy is highly optimized and can
efficiently handle large arrays. It takes less time to
calculate it.
(d) Which approach is more accurate? Why
Solution:
Both approaches are accurate for practical purposes,
but NumPy is more accurate for very large mesh sizes
due to its numerical stability and built-in optimizations.
It has integral value approaches to zero.
Q2. Please write a python program to convert
matrix A from a system of linear equations
(Ax=b) into an upper diagonal matrix. Your code
should work for a square matrix A of
any size. An example of a system of linear
equations is given below. Print the upper
diagonal matrix corresponding to this system and
the solution to this system.
1a + 1b + 1c + 1d + 1e = 4
1a + 2b + 1c + 1d + 1e = 5
1a + 1b + 2c + 1d + 1e = 6
1a + 1b + 1c + 2d + 1e = 7
1a + 1b + 1c + 1d + 2e = 8
Please submit your python files along with a pdf
report containing the figures
import numpy as np
x=int(input("Enter the no. of equations: "))
sol=True
a=np.array([[0.0]*x]*x)
b=np.array([[0.0]]*x)
c=np.array([[0.0]]*x)
for i in range(0,x):
for j in range(0,x):
t="Enter the value of a "+str(i+1)+","+str(j+1)+": "
val=float(input(t))
a[i,j]=val
for i in range(0,x):
t="Enter the value of b "+str(i+1)+": "
val=float(input(t))
b[i,0]=val

for i in range (0,x):


if a[i].any()==0:
sol=False
else:
for k in range (i+1,x):
if a[i,i]!=0:
q=a[k,i]/a[i,i]
a[k]=a[k]-(q*a[i])
b[k]=b[k]-(q*b[i])
else:
a[k],a[i]=a[i],a[k]
b[k],b[i]=b[i],b[k]
if tag==True:
for i in range(x-1,-1,-1):
if a[i,i]==0:
sol=False
break
else:
val=0
for k in range(x-1,i,-1):
val+=a[i,k]*c[k,0]
c[i,0]=(b[i,0]-val)/a[i,i]
print(a)
print(b)
if sol==False:
print("Unique solution not possible")
else:
print("Unique solution matrix:")
print(c)

You might also like