This projetcos consists of a Python library including some of the most common numerical methods, based on contents of the course "Numerical Methods and Applications (MAT3121)" from Escola Politécnica da Universidade de São Paulo. This is a work in progress and other functions are yet to be implemented.
This group of functions in the library implements methods for finding the zero of a function
- Bissection Method -
zeros.bissection
: Takes a functionf
, the limits of the intervala
andb
, and optionaly the number of iterationsN
or maximum errore
. Returns the zero of the function inside the interval, which may be not unique. - Fixed Point Method -
zeros.fixedPoint
: Takes a functionf
, an initial estimativep0
and optionaly the number of iterationsN
. Returns the fixed point of the function. - Newton's Method -
zeros.Newton
: Takes a functionf
, an initial estimativep0
and optionaly the number of iterationsN
. Returns the zero of the function to which Newton's method converge.
This group of functions is used to calculate the derivative of a function
-
Central -
derivative.central
: Takes a functionf
, a pointp
and optionaly the step sizeh
. Returns the derivative at$f(p)$ , using the central method with steph
. -
Forward -
derivative.forward
: Takes a functionf
, a pointp
and optionaly the step sizeh
. Returns the derivative at$f(p)$ , using the forward method with steph
. -
Backward -
derivative.backward
: Takes a functionf
, a pointp
and optionaly the step sizeh
. Returns the derivative at$f(p)$ , using the backward method with steph
.
This group of functions is used to calculate the integral of a function
- Trapezoidal rule -
integration.trapezoidal
: Takes a functionf
, two pointsa
andb
, and a number of divisionsN
in the interval. Returns the numerical value of the integral. - Simpson's rule -
integration.Simpson
: Takes a functionf
, two pointsa
andb
, and a number of divisionsN
in the interval. Returns the numerical value of the integral.
This group of functions is used to solve 1st order ODE or systems of 1st order ODEs, in which the equations $y'n(t) = f(t, y_1(t), y_2(t), ... )$ are passed in the form of a vector, along with their initial values $y_n(t_0)=y{n,0}$. The solution is calculated for discrete time steps of size
- Euler's method -
ode.Euler
: takes the list or array which represents the system of ODEs, the list or array of their initial values, initial timet0
, end timetf
and optionally the step sizeh
. Returns a tuple containing: 1) Discrete time vector for which solutions are calculated; 2) An array in which each row is the solution for an ODE of the system. - Runge-Kutta of 2nd order -
ode.RK2
: takes the list or array which represents the system of ODEs, the list or array of their initial values, initial timet0
, end timetf
and optionally the step sizeh
. Returns a tuple containing: 1) Discrete time vector for which solutions are calculated; 2) An array in which each row is the solution for an ODE of the system. - Runge-Kutta of 4th order -
ode.RK4
: takes the list or array which represents the system of ODEs, the list or array of their initial values, initial timet0
, end timetf
and optionally the step sizeh
. Returns a tuple containing: 1) Discrete time vector for which solutions are calculated; 2) An array in which each row is the solution for an ODE of the system.
Example of use:
#Define the ODEs as functions of t and of
dy1 = lambda t, y: y[1] #y0'(t) = y1(t)
dy2 = lambda t, y: -y[0] - 0.5*y[1] #y1'(t) = -y0(t) - 0.5y1(t)
#Pass the functions as a list to method
time, solutions = ode.RK4([dy1, dy2],[1,0],0, 50, 0.01)
#Use the vectors returned to plot the behavior
plt.plot(tempo, solutions[0], linewidth=2)
plt.plot(tempo, solutions[1], linewidth=1)
plt.xlabel("Time (s)")
plt.ylabel("y(t)")
plt.show()
This library is not inteded to be published at PyPI. If you want to use or try it on your machine:
- Install Git on your machine, via https://git-scm.com/downloads.
- Clone this repository to your machine. (more on how to do this here).
- Locate the file
NumMethPy-0.1.0-py3-none-any.whl
insiderdist
folder. - Run
pip install your-path-to/NumMethPy-0.1.0-py3-none-any.whl
to install library. - Use it on your programs via
import NumMethPy
.