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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file numerical_methods-0.1.0.tar.gz.
File metadata
- Download URL: numerical_methods-0.1.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac8f3818e72aa84cff98e516e9898b599ae71694ff5fd4043f8feed0db9beebc
|
|
| MD5 |
4254f7d8d9113f5eb97549274e59f0ce
|
|
| BLAKE2b-256 |
cfe8df3406812e2940b29fae3bbd123c08830623f05491cf5aec671ca2e9a79b
|
File details
Details for the file numerical_methods-0.1.0-py3-none-any.whl.
File metadata
- Download URL: numerical_methods-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
167129d1fcb3403a2684f9ef5bca819bdea04f5e826488734e02a46fb451120f
|
|
| MD5 |
ebfbd45082f30dbf246bf50ab5b82594
|
|
| BLAKE2b-256 |
94858a6026389e4ee00ac34a66d8b155ba58738e004e6bbe3340c6a92b78f50f
|