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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

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

Hashes for numerical_methods-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ac8f3818e72aa84cff98e516e9898b599ae71694ff5fd4043f8feed0db9beebc
MD5 4254f7d8d9113f5eb97549274e59f0ce
BLAKE2b-256 cfe8df3406812e2940b29fae3bbd123c08830623f05491cf5aec671ca2e9a79b

See more details on using hashes here.

File details

Details for the file numerical_methods-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for numerical_methods-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 167129d1fcb3403a2684f9ef5bca819bdea04f5e826488734e02a46fb451120f
MD5 ebfbd45082f30dbf246bf50ab5b82594
BLAKE2b-256 94858a6026389e4ee00ac34a66d8b155ba58738e004e6bbe3340c6a92b78f50f

See more details on using hashes here.

Supported by

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