Python scientific computational _scipy_ constants and optimization

Source: Internet
Author: User
Tags mathematical functions

Based on NumPy, SCIPY provides many modules commonly used in mathematics, science and engineering calculation, and is a powerful numerical computing library.

1. Constants and Special functions

SciPy's Constants module contains a number of physical constants:

Import scipy.constants as C
C.C? #真空中的光速
C.h? #普朗克常数
C.pi #圆周率?

In the C.physical_constants dictionary, the physical constant is accessed by the name of the physical constant, such as:

c.physical_constants[' speed of light in vacuum ']

(299792458.0, ' m s^-1 ', 0.0)

c.physical_constants[' Planck constant ']

(6.62606957e-34, ' J s ', 2.9e-41)

c.physical_constants[' electron mass ']

(9.10938291e-31, ' kg ', 4e-38)

Returns a tuple of three values, representing constant values, units, and errors, respectively;

The special module of SCIPY contains basic mathematical functions, special mathematical functions, and all functions in numpy;

Due to the precision limit of floating-point numbers, some functions can not accurately represent the results, such as log (1+1e-20), 1+1e-20 is very close to 1, the result will be 0, not the exact value; log1p (1e-20) gives the exact value:

Import Math

Import scipy.special as S

Math.log (1+1e-20,10)

0.0

S.LOG1P (1e-20)

9.9999999999999995e-21

Also, look at the document to see: log1p is a ufunc;

2. Optimization: Optimize

SCIPY's optimize module provides many numerical optimization algorithms.

1. Least Squares fitting

OPTIMIZE.LEASTSQ () calculates the least squares fitting of the data. When LEASTSQ () is used, the function of calculating error and the initial value of the parameter to be determined can be passed.

Line fitting:

def residuals (p):
? ? K, B = P
? ? Return Y-(k*x-b)

X, Y is a two one-dimensional array representing the x-axis and y-axis position of the point;

R = leastsq (residuals, [1,0])

The LEASTSQ () function passes in the error calculation function and the initial value [1,0], which is passed in as the first parameter of the error calculation function;

The result of the calculation R is a tuple containing two elements, the first element is an array that represents the fitted parameter k, B, and the second element if it is equal to one of the integers in 1, 2, 3, 4, then the fitting succeeds, otherwise the MESG will be returned; more return values are shown in the document;

Other function fitting (in the case of sine wave fitting):

def func (x,p):
? ? A,k,theta = P
? ? Return A*np.sin (2*np.pi*k*x+theta)

def residuals (p,y,x):
? ? Return Y-func (X,P)

x = Np.linspace (0,2*np.pi,100)
A, k, theta = ten, 0.34, NP.PI/6? #真实参数
Y0 = func (X,[a,k,theta])? #真实值
Y1 = y0 + 2 * NP.RANDOM.RANDN (len (x))? # Add Noise
P0 = [Ten, 0.3, 0.5]? #猜测拟合参数, that is, the initial value

PLSQ = LEASTSQ (residuals, P0, args= (y1,x)) # In addition to the initial value, the args parameter is called to specify additional parameters used in residuals (the linear fit uses the global variable of x, y directly) and also returns a tuple , the first element is a fitted parameter array;

Import? pylab as Pl
Pl.plot (x,y0,label=u ' real data ')
Pl.plot (x,y1,label=u ' noise data ')
Pl.plot (X,func (x,plsq[0]), label=u ' fitting results ')
Pl.legend ()
Pl.show ()

In the process of fitting, the initial value of guessing is very important, the noise data need to be drawn first, then the approximate phase and frequency are estimated, the initial value of the estimate is introduced, and then the fitting is done;

2. Minimum value of the function

The Optimize module also provides many algorithms for finding function minimums: Fmin, Fmin_powell, FMIN_CG, FMIN_BFGS, etc., which fit the minimum value by passing in the target function and the initial value, fmin* () This type of function also provides a fprime parameter that computes the function of the target function on the partial derivative of each argument.

3. Solving the nonlinear Equation group

The Fsolve () function in the Optimize module solves a nonlinear equation group, which passes in a function that calculates the error of the equations, and the initial values of the parameters such as:

def func (x):
? ? U1,U2,U3 = X
? ? return [F1 (U1,U2,U3), F2 (U1,U2,U3), F3 (U1,U2,U3)]

result = Fsolve (func,[1,1,1])

The above process can be asked to:

F1 (U1,U2,U3) = 0
F2 (U1,U2,U3) = 0
F3 (U1,U2,U3) = 0

The solution of the nonlinear equation Group;

Python scientific computational _scipy_ constants and optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.