Skip to content

Commit

Permalink
Ecriture des methodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Déhaies Nicolas committed Apr 15, 2015
1 parent 808a4db commit 98ed66c
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as mp
import numpy.random as npr

#----------------------------------#
# INTEGRAL METHODS #
#----------------------------------#

def rect_meth(x, y, n, f):
a = (y-x)/n
s = 0.0
t = np.arange(0, n)
for i in t:
s += f(x + i*a)
return a*s

def simpson_meth(x, y, n, f):
a = (y-x)/n
s1 = 0.0
s2 = 0.0
t = np.arange(1, n/2)
u = np.arange(1, (n/2)+1)
for i in t:
s1 += f(x + 2*i*a)
s1 = 2*s1
for i in u:
s2 += f(x + (2*i-1)*a)
s2 = 4*s2
return (a/3)*(f(x) + s1 + s2 + f(y))

def monte_carlo_meth(x, y, n, f):
max = 0.0
s = 0.0
for i in range(n+1):
a = f(x + y*float(i+1)/n)
if (max < a):
max = a
alpha = (b-a)*max
for i in range(n):
x_random = (b-a)*npr.rand() + x
y_random = max*npr.rand()
if (y_random <= max):
s += (alpha/n) * f(x_random)
return s


#--------------------#
# LENGTH #
#--------------------#

def length(I, n, df, x, y):
length = lambda x: np.sqrt(1 + (df(x)*df(x)))
return I(x, y, n, length)

0 comments on commit 98ed66c

Please sign in to comment.