python二元多次函数拟合_Python实现的拟合二元一次函数功能示例【基于scipy模块】...
本文實(shí)例講述了Python實(shí)現(xiàn)的擬合二元一次函數(shù)功能。分享給大家供大家參考,具體如下:
背景:
使用scipy擬合一元二次函數(shù)。
參考:
HYRY Studio-《用Python做科學(xué)計(jì)算》
代碼:
# -*- coding:utf-8 -*-
#! python3
import numpy as np
from scipy.optimize import leastsq
import pylab as pl
def func(x,p):
"""
數(shù)組擬合函數(shù)
"""
A,k,theta = p
return A*(x-k)**2+theta
def residuals(p,y,x):
"""
殘差
"""
return y-func(x,p)
x = np.linspace(0,2,100)
A,k,theta = 10.,1,2. #真實(shí)數(shù)據(jù)參數(shù)
y0 = func(x,[A,k,theta]) #真實(shí)數(shù)據(jù)
y1 = y0 + 2 * np.random.randn(len(x)) #加入噪聲序列
p0 = [7.,0.2,1.]
plsq = leastsq(residuals,p0,args = (y1,x))
print("真實(shí)參數(shù):",[A,k,theta])
print("擬合參數(shù):",plsq[0]) #試驗(yàn)數(shù)據(jù)擬合后的參數(shù)
pl.plot(x,y0,label = "real")
pl.plot(x,y1,label = "real+noise")
pl.plot(x,func(x,plsq[0]),label = "fitting")
pl.legend()
pl.show()
結(jié)果:
(貌似這里的求解方法用了智能算法,因?yàn)槊看蔚慕Y(jié)果都有細(xì)小差異。具體資料沒見到,以后有精力再找)
真實(shí)參數(shù): [10.0, 1, 2.0]
擬合參數(shù): [ 10.83391995?? 0.98950039?? 1.63356065]
PS:這里再為大家推薦兩款相似的在線工具供大家參考:
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
總結(jié)
以上是生活随笔為你收集整理的python二元多次函数拟合_Python实现的拟合二元一次函数功能示例【基于scipy模块】...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 耳软骨鼻修复还要重新取肋骨吗
- 下一篇: python中main的作用_浅析pyt