Scipy-数值计算库

SciPy包含众多物理常数:

from scipy import constants as C
C.c  #光速
C.h #普朗克常数

SciPy的special模块是一个非常完整的函数库, 例如伽马函数:

\[ \Gamma= \int_0^\inf t^{z-1}e^{-t} \rm d \it t \]

import scipy.special as S
S.gamma(4)

最小二乘拟合

View Code
import numpy as np
import pylab as pl
from scipy.optimize import leastsq

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=10, 0.34, np.pi/6
y0=func(x,[A,k,theta])
y1=y0 + 2*np.random.randn(len(x))
p0=[7, 0.3, 0]

plsq=leastsq(residuals,p0,args=(y1,x))

print plsq

y_sq=func(x,plsq[0])
pl.plot(x,y1)
pl.plot(x,y_sq)
pl.show()

 

     

 

posted @ 2012-12-30 14:27  zjuhjm  阅读(235)  评论(0编辑  收藏  举报