I'm trying to find the trapezoidal integration of a bunch of data but it keeps saying
File "D:/uni work/Sci programming/aps3.py", line 25, in trapz
x2 = x[i+1]
IndexError: index 1 is out of bounds for axis 0 with size 1'
I changed range(n) to range(n-1) but I still get the same error.
import numpy as np
solarfile = open('solar.dat','r',)
x1=[]#wavelength
y1=[]#irradiance
for theline in solarfile:
num = theline.split()
x1.append(float(num[0]))
y1.append(float(num[1]))
x=np.array([x1])
y=np.array([y1])
a = int(input("Please Enter the lower bound: "))
b = int(input("Please enter the upper bound: "))
if (a<np.argmin(x)):
a =np.argmin(x)
if (b>np.argmax(x)):
b =np.argmax(x)
n = b - a
def trapz(x,y,a,b):
traparray=[]
for i in range(n-1):
x1 = x[i]
x2 = x[i+1]
dx = x2-x1
y1 = y[i]
y2 = y[i+1]
dy = y2-y1
trap = ((y1*dx)+(0.5*(dy*dx)))
d = traparray.append(trap)
return (s)
print (trapz(x,y,a,b))
solarfile.close()
I keep trying things but they don't work.