rectangles.py

Created by christian-mercat

Created on March 11, 2024

305 Bytes

Implémentation de la méthode des rectangles pour intégrer une fonction réelle. Elle prend comme argument une fonction (par défaut le demi-cercle f=lambda x: sqrt(1-x**2) ), une borne inférieure, une borne supérieure, un nombre de rectangles et un coefficient gd=0 pour gauche, gd=1 pour droite, gd=0.5 pour un rectangle centré.


from math import *

def rectangles(f=lambda x: sqrt(1-x**2),a=0,b=1,n=100,gd=0):
  s=0
  d=(b-a)/n
  for k in range(n):
    s += f(a+(k+gd)*d)
  return s*d

def trapezes(f=lambda x: sqrt(1-x**2),a=0,b=1,n=100):
  return rectangles(f,a,b,n,0)+(f(b)-f(a))/(2*n)

def f(x):
  return x**2

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.