Advertisement
Guest User

Numerical Analysis

a guest
Dec 8th, 2010
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def midpoint(f, x0, xn, n):
  4.     X=[0.0]*(4*n)
  5.     Y=[0.0]*(4*n)
  6.    
  7.     dx = float(xn-x0) / float(n)
  8.     VLines = [x0+i*dx for i in xrange(n)]
  9.     for i in xrange(n):
  10.         X[4*i] = X[4*i+1] = float(x0+i*dx)
  11.         X[4*i+2] = X[4*i+3] = x0+(i+1)*dx
  12.  
  13.         Y[4*i] = Y[4*i+3] = 0.0
  14.         Y[4*i+1] = Y[4*i+2] = f( (X[4*i]+X[4*i+3]) / 2.0)
  15.     return X,Y,VLines
  16.  
  17. def trapezoid(f, x0, xn, n):
  18.     X=[0.0]*(4*n)
  19.     Y=[0.0]*(4*n)
  20.    
  21.     dx = float(xn-x0) / float(n)
  22.     VLines = [x0+i*dx for i in xrange(n)]
  23.     for i in xrange(n):
  24.         X[4*i] = X[4*i+1] = float(x0+i*dx)
  25.         X[4*i+2] = X[4*i+3] = x0+(i+1)*dx
  26.  
  27.         Y[4*i] = Y[4*i+3] = 0.0
  28.         Y[4*i+1] = f(X[4*i])
  29.         Y[4*i+2] = f(X[4*i+3])
  30.     return X,Y,VLines
  31.  
  32. def simpson(f, x0, xn, n):
  33.     X,Ym,V = midpoint(f,x0,xn,n)
  34.     Xt,Yt,Vt = trapezoid(f,x0,xn,n)
  35.     Y = [(2.0*M+T)/3.0 for M,T in zip(Ym,Yt)]
  36.     return X,Y,V
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement