Ejemplo n.º 1
0
PI = 2. * np.arcsin(1)
N = 1000
a = 0
b = 2. * PI
h = (b - a) / (N - 1)
x = np.zeros(N)
y1 = np.zeros(N)
y2 = np.zeros(N)
y3 = np.zeros(N)
for i in range(N):
  x[i] = a + h * i
  y1[i] = np.sin(x[i])
  y2[i] = np.sin(2. * x[i])
  y3[i] = np.sin(8. * x[i])

print(intr.trap_d(x,intr.multiply(y1,y1)))
print(intr.trap_d(x,intr.multiply(y1,y2)))
print(intr.trap_d(x,intr.multiply(y2,y3)))
print(intr.trap_d(x,intr.multiply(y3,y3)))

#We find that when m=n the scalar product is pi and when m=/n the scalar product
#is 0. This is very similar to the scalar product of the legendre functions.

def powr(x,n):
  m = 1
  if n == 0:
    return 1
  else:
    for l in range(n):
      m = m * x
    return m
Ejemplo n.º 2
0
PI = 2.*np.arcsin(1)
a=0
b= 2*PI
N= 10
h = (a+b) / (N-1)
x = np.zeros(N)
y1 = np.zeros(N)
y2 = np.zeros(N)
y8 = np.zeros(N)
for i in range(N):
  x[i] = a + h*i
  y1[i] = np.sin(x[i])
  y2[i] = np.sin(2*x[i])
  y8[i] = np.sin(8*x[i]) 

print(intr.trap_d(x,intr.multiply(y1,y1)))
print(intr.trap_d(x,intr.multiply(y1,y2)))
print(intr.trap_d(x,intr.multiply(y2,y8)))
print(intr.trap_d(x,intr.multiply(y8,y8)))

#2nd problem
a2= 0
b2 = 1
N = [10, 100, 1000] 
N2 = len(N)
for i in range(N2):
  x = np.zeros(N[i])
  y = np.zeros(N[i])
  print("for N = " + str(N[i])) 
  print(intr.trap(intr.lin,a2,b2,N[i]))
  print(intr.trap(intr.quad,a2,b2,N[i]))
Ejemplo n.º 3
0
# I wrote a function that calculates the power of x using inputs x and n, for the power. The code goes through for all the x^n terms and then for the exp(x) term.  I needed two slightly different gauss_quad functions to deal with the extra input for my power function.
N = [10, 100, 1000]
n = [1, 2, 6]
powers = [1, 2, 11, 12]
a = 0
b = 1
for k in range(len(powers)):
    for j in range(len(N)):
        m = (b - a) / (N[j] - 1)
        x = np.zeros(N[j])
        y = np.zeros(N[j])
        for i in range(N[j]):
            x[i] = a + m * i
            y[i] = intr.power(x[i], powers[k])
        print(N[j], powers[k])
        print(intr.trap_d(x, y))
    for h in range(len(n)):
        [x2, w] = intr.gauss_leg(a, b, n[h])
        print(N[j], powers[k], n[h])
        print(intr.gauss_quad(intr.power, x2, w, powers[k]))

# exp calculation
for j in range(len(N)):
    m = (b - a) / (N[j] - 1)
    x = np.zeros(N[j])
    y = np.zeros(N[j])
    for i in range(N[j]):
        x[i] = a + m * i
        y[i] = np.exp(x[i])
    print(intr.trap_d(x, y))
for h in range(len(n)):