A preliminary study of scientific calculation using Python

Source: Internet
Author: User
Tags sin

Take a look at Python's scientific computing power today after setting up the Django Framework blog.

The scientific calculation of Python has three musketeers: Numpy,scipy,matplotlib.

NumPy is responsible for numerical calculation, matrix operation and so on;

SCIPY is responsible for common mathematical algorithms, interpolation, fitting and so on;

Matplotlib is responsible for drawing.

First of all, Baidu top three, followed by installation.

May consider using pyhton34/script/easy-install tools; easy-insatll-m matplotlib;

Try the code to fit the example;

1 #-*-Coding:utf-8-*-
2 Import NumPy as NP
3 from scipy.optimize import leastsq
4 Import Pylab as pl
5
6 def func (x, p):
7 # "" "
8 # Functions used for data fitting: A*sin (2*pi*k*x + theta)
9 # "" "
Ten A, k, theta = P
Return A*np.sin (2*np.pi*k*x+theta)
12
def residuals (p, y, x):
14 # "" "
15 # The difference between the experimental data x, Y, and the Fit function, p is the coefficient to be found for fit
16 # "" "
Y-func return (x, p)
18
x = Np.linspace (0, -2*np.pi, 100)
A, k, theta = ten, 0.34, NP.PI/6 # function parameters of real data
Y0 = func (x, [A, K, Theta]) # Real data
Y1 = y0 + 2 * NP.RANDOM.RANDN (len (x)) # Experiment data after adding noise
23
P0 = [7, 0.2, 0] # First guessing function fitting parameters
25
26 # Call LEASTSQ for data fitting
# residuals as a function of calculating errors
# P0 is the initial value of the fitting parameter
# args is the experimental data that needs to be fitted
PLSQ = LEASTSQ (residuals, P0, args= (y1, x))
31
# Print (U "true parameter:")
Print ([A, K, Theta])
# Print (U "Fit Parameters")
Print (plsq[0]) # parameters after fitting the experimental data
36
PNS Pl.plot (x, y0, label=u "real data")
Pl.plot (x, y1, label=u "data with noisy")
Pl.plot (x, func (x, plsq[0]), label=u "Nihe data")
Pl.legend ()
Pl.show ()

Run prompt error, missing third-party packages, such as six,dateutil,pyparsing, and so on, what is missing, third-party packages are mostly directly dragged into the D:\python34\lib directory can be, very convenient.

All mounted upon, running successfully;

Http://www.cnblogs.com/javajava/p/4792791.html

Using Python to do scientific Computing (GO)

Related Article

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.