Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numerical_methods-0.1.0.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

numerical_methods-0.1.0-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page