{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "fcdc8220-ff32-44a6-a1aa-008c9d2ab047", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 3, "id": "f6807c1d-c008-4661-a888-5244338ff765", "metadata": { "tags": [] }, "outputs": [], "source": [ "def f(x):\n", " return x**-2" ] }, { "cell_type": "code", "execution_count": 4, "id": "b1095809-d35b-475a-887e-728695022470", "metadata": { "tags": [] }, "outputs": [], "source": [ "def integrNC(fct, a, b, N, m, n, r, coeff, tol=1e10):\n", " h = (b-a)/m/N\n", " I = 0\n", " for k in range(N):\n", " # Ajout d'un shéma élementaire\n", " Ie = 0\n", " \n", " Xk = a + h*k*m\n", " for i in range(n+1):\n", " Ie += coeff[i] * fct(Xk + (i+r)*h)\n", " \n", " I += Ie * h\n", " \n", " return I" ] }, { "cell_type": "code", "execution_count": 7, "id": "36508cd9-143a-4174-bc98-6c01634f1fcd", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.998686802598624\n" ] } ], "source": [ "I = 0\n", "a, b = 1, 5\n", "N=100\n", "m=1\n", "n=1\n", "r=0\n", "coeff = [.5, .5]\n", " \n", "while True:\n", " Ie = integrNC(f, a, b, N, m, n, r, coeff)\n", " \n", " \n", " d= b-a\n", " b += d\n", " a += d\n", " # print(I, abs(Ie), abs(Ie)<1e-10)\n", " if abs(I+Ie) - abs(I) < 1e-5:\n", " break\n", " I += Ie\n", " \n", "print(I)" ] }, { "cell_type": "code", "execution_count": null, "id": "d52567ea-6629-4433-9df9-e3f93de1c36c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }