A preliminary study of scientific calculation using Python

Source: Internet
Author: User

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-*-
2ImportNumPy as NP
3 fromScipy.optimizeImportLeastsq
4ImportPylab as Pl
5
6defFunc (x, p):
7# """
8#functions used for data fitting: A*sin (2*pi*k*x + theta)
9# """
TenA, k, theta = P
OnereturnA*np.sin (2*np.pi*k*x+theta)
A
-defResiduals (p, y, x):
-# """
the#the difference between the experimental data x, Y, and the fitting function, p is the coefficient to be found for fitting
-# """
-returnY-func (x, p)
-
+x = Np.linspace (0, -2*np.pi, 100)
-A, k, theta = ten, 0.34, NP.PI/6#function parameters for real data
+Y0 = func (x, [A, K, Theta])#Real Data
AY1 = y0 + 2 * NP.RANDOM.RANDN (len (x))#experimental data after adding noise
at
-P0 = [7, 0.2, 0]#function fitting parameters for the first guess
-
-#call LEASTSQ for data fitting
-#residuals as a function of calculating errors
-#P0 is the initial value of the fitting parameter
in#args is the experimental data that needs to be fitted
-PLSQ = LEASTSQ (residuals, P0, args= (y1, x))
to
+#Print (U "true parameter:")
-Print([A, K, Theta])
the#Print (U "fit parameter")
*Print(Plsq[0])#parameters after fitting the experimental data
$
Panax NotoginsengPl.plot (x, y0, Label=u"Real Data")
-Pl.plot (x, y1, Label=u"data with noisy")
thePl.plot (x, func (x, plsq[0]), Label=u"Nihe Data")
+Pl.legend ()
APl.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;

A preliminary study of scientific calculation using Python

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.