Library for solving mathematical problems in numerical form
Project description
Numerical methods
Implementation of methods for solving mathematical problems in numerical form
Integrals
Let's try calculate integral $\int_{-1}^{4} ! (2x^2-3) , \mathrm{d}x = \frac {85} 3$
The whole code avialable in notebook in this repo
At first, we will find analytical (exact) solution:
$ pip install sympy
from sympy import symbols, integrate
x = symbols('x')
f = (2*x**2 - 3)
display(integrate(f, (x, -1, 4)))
$\frac{85}{3}$
exact_solution = 85/3
print(f"exact solution = {exact_solution}")
>>> exact solution = 28.333333333333332
Now we will calculate it numerically by rectangle method with 10 rectangles:
$I = \int_{a}^b f(x) \mathrm dx \approx \sum_{i=0}^{n-1}f(x_i)(x_{i+1}-x_i)$
from integrate import rectangle_method
def f(x):
return 2*x**2 - 3
integral = rectangle_method(-1, 4, 10)
print("Approximate integral:", integral[0])
print('Difference between exact and approximate solutions equals', abs(exact_solution - integral[0]))
>>> Approximate integral: 28.125 >>> Difference between exact and approximate solutions equals 0.20833333333333215
After increasing number of rectangles (from 10 to 100) difference between exact and approximate solutions is less significant:
integral = rectangle_method(-1, 4, 100)
print('Exact solution = ', exact_solution)
print("Approximate integral:", integral[0])
print(f'Difference between exact and approximate solutions equals {abs(exact_solution - integral[0]):.15f}')
>>> Exact solution = 28.333333333333332 >>> Approximate integral: 28.331249999999958 >>> Difference between exact and approximate solutions equals 0.002083333333374
to be continued in close times
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for numerical_methods-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 167129d1fcb3403a2684f9ef5bca819bdea04f5e826488734e02a46fb451120f |
|
MD5 | ebfbd45082f30dbf246bf50ab5b82594 |
|
BLAKE2b-256 | 94858a6026389e4ee00ac34a66d8b155ba58738e004e6bbe3340c6a92b78f50f |