Python scipy solve equation

scipy.integrate.odeint#

scipy.integrate. odeint ( func , y0 , t , args = () , Dfun = None , col_deriv = 0 , full_output = 0 , ml = None , mu = None , rtol = None , atol = None , tcrit = None , h0 = 0.0 , hmax = 0.0 , hmin = 0.0 , ixpr = 0 , mxstep = 0 , mxhnil = 0 , mxordn = 12 , mxords = 5 , printmessg = 0 , tfirst = False ) [source] #

Integrate a system of ordinary differential equations.

For new code, use scipy.integrate.solve_ivp to solve a differential equation.

Solve a system of ordinary differential equations using lsoda from the FORTRAN library odepack.

Solves the initial value problem for stiff or non-stiff systems of first order ode-s:

dy/dt = func(y, t, . ) [or func(t, y, . )] 

By default, the required order of the first two arguments of func are in the opposite order of the arguments in the system definition function used by the scipy.integrate.ode class and the function scipy.integrate.solve_ivp . To use a function with the signature func(t, y, . ) , the argument tfirst must be set to True .

Computes the derivative of y at t. If the signature is callable(t, y, . ) , then the argument tfirst must be set True .

Initial condition on y (can be a vector).

A sequence of time points for which to solve for y. The initial value point should be the first element of this sequence. This sequence must be monotonically increasing or monotonically decreasing; repeated values are allowed.

args tuple, optional

Extra arguments to pass to function.

Dfun callable(y, t, …) or callable(t, y, …)

Gradient (Jacobian) of func. If the signature is callable(t, y, . ) , then the argument tfirst must be set True .

col_deriv bool, optional

True if Dfun defines derivatives down columns (faster), otherwise Dfun should define derivatives across rows.

full_output bool, optional

True if to return a dictionary of optional outputs as the second output

printmessg bool, optional

Whether to print the convergence message

tfirst bool, optional

If True, the first two arguments of func (and Dfun, if given) must t, y instead of the default y, t .

Array containing the value of y for each desired time in t, with the initial value y0 in the first row.

infodict dict, only returned if full_output == True

Dictionary containing additional output information

vector of step sizes successfully used for each time step

vector with the value of t reached for each time step (will always be at least as large as the input times)

vector of tolerance scale factors, greater than 1.0, computed when a request for too much accuracy was detected

value of t at the time of the last method switch (given for each time step)

cumulative number of time steps

cumulative number of function evaluations for each time step

cumulative number of jacobian evaluations for each time step

a vector of method orders for each successful step

index of the component of largest magnitude in the weighted local error vector (e / ewt) on an error return, -1 otherwise

the length of the double work array required

the length of integer work array required

a vector of method indicators for each successful time step: 1: adams (nonstiff), 2: bdf (stiff)

If either of these are not None or non-negative, then the Jacobian is assumed to be banded. These give the number of lower and upper non-zero diagonals in this banded matrix. For the banded case, Dfun should return a matrix whose rows contain the non-zero bands (starting with the lowest diagonal). Thus, the return matrix jac from Dfun should have shape (ml + mu + 1, len(y0)) when ml >=0 or mu >=0 . The data in jac must be stored such that jac[i — j + mu, j] holds the derivative of the i`th equation with respect to the `j`th state variable. If `col_deriv is True, the transpose of this jac must be returned.

rtol, atol float, optional

The input parameters rtol and atol determine the error control performed by the solver. The solver will control the vector, e, of estimated local errors in y, according to an inequality of the form max-norm of (e / ewt)

tcrit ndarray, optional

Vector of critical points (e.g., singularities) where integration care should be taken.

h0 float, (0: solver-determined), optional

The step size to be attempted on the first step.

hmax float, (0: solver-determined), optional

The maximum absolute step size allowed.

hmin float, (0: solver-determined), optional

The minimum absolute step size allowed.

ixpr bool, optional

Whether to generate extra printing at method switches.

mxstep int, (0: solver-determined), optional

Maximum number of (internally defined) steps allowed for each integration point in t.

mxhnil int, (0: solver-determined), optional

Maximum number of messages printed.

mxordn int, (0: solver-determined), optional

Maximum order to be allowed for the non-stiff (Adams) method.

mxords int, (0: solver-determined), optional

Maximum order to be allowed for the stiff (BDF) method.

solve an initial value problem for a system of ODEs

a more object-oriented integrator based on VODE

for finding the area under a curve

The second order differential equation for the angle theta of a pendulum acted on by gravity with friction can be written:

theta''(t) + b*theta'(t) + c*sin(theta(t)) = 0 

where b and c are positive constants, and a prime (’) denotes a derivative. To solve this equation with odeint , we must first convert it to a system of first order equations. By defining the angular velocity omega(t) = theta'(t) , we obtain the system:

theta'(t) = omega(t) omega'(t) = -b*omega(t) - c*sin(theta(t)) 

Let y be the vector [theta, omega]. We implement this system in Python as:

>>> import numpy as np >>> def pend(y, t, b, c): . theta, omega = y . dydt = [omega, -b*omega - c*np.sin(theta)] . return dydt . 

We assume the constants are b = 0.25 and c = 5.0:

For initial conditions, we assume the pendulum is nearly vertical with theta(0) = pi — 0.1, and is initially at rest, so omega(0) = 0. Then the vector of initial conditions is

We will generate a solution at 101 evenly spaced samples in the interval 0 t

Call odeint to generate the solution. To pass the parameters b and c to pend, we give them to odeint using the args argument.

>>> from scipy.integrate import odeint >>> sol = odeint(pend, y0, t, args=(b, c)) 

The solution is an array with shape (101, 2). The first column is theta(t), and the second is omega(t). The following code plots both components.

>>> import matplotlib.pyplot as plt >>> plt.plot(t, sol[:, 0], 'b', label='theta(t)') >>> plt.plot(t, sol[:, 1], 'g', label='omega(t)') >>> plt.legend(loc='best') >>> plt.xlabel('t') >>> plt.grid() >>> plt.show() 

Источник

scipy.linalg.solve#

Solves the linear equation set a @ x == b for the unknown x for square a matrix.

If the data matrix is known to be a particular type then supplying the corresponding string to assume_a key chooses the dedicated solver. The available options are

If omitted, ‘gen’ is the default structure.

The datatype of the arrays define which solver is called regardless of the values. In other words, even when the complex array entries have precisely zero imaginary parts, the complex solver will be called based on the data type of the array.

Parameters : a (N, N) array_like

b (N, NRHS) array_like

Input data for the right hand side.

lower bool, default: False

Ignored if assume_a == ‘gen’ (the default). If True, the calculation uses only the data in the lower triangle of a; entries above the diagonal are ignored. If False (default), the calculation uses only the data in the upper triangle of a; entries below the diagonal are ignored.

overwrite_a bool, default: False

Allow overwriting data in a (may enhance performance).

overwrite_b bool, default: False

Allow overwriting data in b (may enhance performance).

check_finite bool, default: True

Whether to check that the input matrices contain only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs.

assume_a str,

Valid entries are explained above.

transposed bool, default: False

If True, solve a.T @ x == b . Raises NotImplementedError for complex a.

Returns : x (N, NRHS) ndarray

If size mismatches detected or input a is not square.

If the matrix is singular.

If an ill-conditioned input a is detected.

If transposed is True and input a is a complex matrix.

If the input b matrix is a 1-D array with N elements, when supplied together with an NxN input a, it is assumed as a valid column vector despite the apparent size mismatch. This is compatible with the numpy.dot() behavior and the returned result is still 1-D array.

The generic, symmetric, Hermitian and positive definite solutions are obtained via calling ?GESV, ?SYSV, ?HESV, and ?POSV routines of LAPACK respectively.

>>> import numpy as np >>> a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]]) >>> b = np.array([2, 4, -1]) >>> from scipy import linalg >>> x = linalg.solve(a, b) >>> x array([ 2., -2., 9.]) >>> np.dot(a, x) == b array([ True, True, True], dtype=bool) 

Источник

Читайте также:  Css блоки для контента
Оцените статью