1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 filename = 'Unradiated_CG_Steel.txt'
5 f = np.genfromtxt(filename, delimiter=',')
6 X = f[:,0]
7 Y = f[:,1]
8
9 plt.xlabel('Engineering Strain (%)', fontsize=24)
10 plt.ylabel('Engineering Stress (Mpa)', fontsize=24)
11 l = plt.plot(X,Y,'ro')
12 plt.savefig('Stress_strain_sample.png',fortmat='png',\
13 bbox_inches='tight', dpi=72)
1 # The rectangles method
2 def rectangle(X,Y,n):
3 d = X[1]-X[0]
4 val = 0
5 for i in range(n-1):
6 val += Y[i]
7 val *= d
8 return val
1 # The trapezoid method
2 def trapz(X,Y,n):
3 d = X[1]-X[0]
4 val_a = 0
5 for i in range(n):
6 if i == 0 or i == n-1:
7 pass
8 else:
9 val_a += 2*Y[i]
10 return (d*0.5)*(Y[0]+Y[n-1]+val_a)
1 # Simpson’s method
2 def simps(X,Y,n):
3 d = X[1]-X[0]
4 val_a = 0
5 val_b = 0
6 for i in range(n):
7 if i ==0 or i == n-1:
8 pass
9 else:
10 if i % 2 == 1:
11 val_a += 4*Y[i]
12 else:
13 val_b += 2*Y[i]
14 return (d/3)*(Y[0]+Y[n-1]+val_a+val_b)
1 R = rectangle(X,Y,7)
2 print('Rectangle: ', R)
3
4 T = trapz(X,Y,7)
5 print('Trapezoid: ', T)
6
7 S = simps(X,Y,7)
8 print('Simpson: ', S)