前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python做曲线拟合和曲面拟合

Python做曲线拟合和曲面拟合

作者头像
用户6021899
发布2024-04-11 15:35:08
5870
发布2024-04-11 15:35:08
举报
文章被收录于专栏:Python编程 pyqt matplotlib

scipy.optimize 模块的 curve_fit 函数可以用于曲线/曲面拟合。

  • 曲线拟合示例:
代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit


def func(x,a,b,c):
    return a*np.exp(-b*x)+c


x = np.linspace(0,3,100)
y = func(x,2.5,1.3,0.5)
yn = y+0.1*np.random.normal(size=len(x))




# 曲线拟合
popt, pcov = curve_fit(func, x, yn)
a,b,c = popt
print(f"a= {a}, b= {b}, c= {c}")
print(pcov)


plt.scatter(x,yn, color='b',label='raw data')
plt.plot(x, func(x,*popt), "r-", label='fit')
plt.legend()
plt.title("曲线拟合")
plt.show()
  • 曲面拟合示例:
代码语言:javascript
复制
import numpy as np
from scipy.optimize import curve_fit
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm


n_x = 11
n_y = 5
X0 = np.array([[198.15]*n_y, [223.15]*n_y,[248.15]*n_y,[273.15]*n_y,[298.15]*n_y,[323.15]*n_y,[348.15]*n_y,[373.15]*n_y,
              [398.15]*n_y,[423.15]*n_y,[448.15]*n_y,])
X = X0.flatten()


Y0 =np.array([16.0,17.0,18.0,19.0,20.0]*n_x).reshape((n_x,n_y))
Y= Y0.flatten()


Z0= np.array([[1.5,1.5040 ,1.4385 ,1.1440 ,1.005696],
            [1.3305,1.3374 ,1.2974 ,1.0936 ,1.004434],
            [1.1879,1.2089 ,1.1731 ,1.0625 ,1.003258],
            [1.0773,1.0906 ,1.0808 ,1.0297 ,1.002171],
            [1.000,1.0000 ,1.0000 ,1.0000 ,1.0000],
            [0.9073,0.9223 ,0.9285 ,0.9528 ,0.9999],
            [0.8422,0.8575 ,0.8548 ,0.9174 ,0.99943],
            [0.7922,0.7997 ,0.8133 ,0.8842 ,0.99869],
            [0.7438,0.7491 ,0.7630 ,0.8470 ,0.99804],
            [0.6918,0.7053 ,0.7212 ,0.8193 ,0.99748],
            [0.6536,0.6646 ,0.6818 ,0.7768 ,0.99700]])
Z= Z0.flatten()


#定义二元函数
#def func(xy, a,b,c,d,e,f):
def func(xy, a, c, d, e, f):
    x,y = xy
    # return a*x**2 + b*y**2 + c*x*y + d*x + e*y + f
    return a * x ** 2 + c * x * y + d * x + e * y + f


#准备数据
# X= np.linspace(0,10,10)
# Y= np.linspace(-10,5,10)
# Z = 5*X*X + 5*Y*Y + 5*X*Y + 5*X + 5*Y + 5 + 20*np.random.normal(size=(len(X)))


# 进行曲面拟合
#p0 = [5.e-06,3.77e-03,8.20e-04,-2.07e-02,-3.99e-01,8.2e+00] # 拟合参数的初始值
# params, pcov = curve_fit(func,(X,Y),Z, p0)
params, pcov = curve_fit(func,(X,Y),Z)
# a,b,c,d,e,f = params
a,c,d,e,f = params
print(f"拟合参数={params}")
print(pcov)


print(f"np.abs(pcov).max()= {np.abs(pcov).max()}")


def fit_surface(x,y):
    # return a*x**2 + b*y**2 + c*x*y + d*x + e*y + f
    return a * x ** 2  + c * x * y + d * x + e * y + f


print(f"fit_surface(298.15,20)= {fit_surface(298.15,20)}")
print(f"fit_surface(448.15,20)= {fit_surface(448.15,20)}")
np_fit_surface = np.frompyfunc(fit_surface,2,1) # nin, 输入参数的数量,nout 返回值的数量
z_fit = np_fit_surface(X,Y)
error = z_fit-Z
print(f"np.abs(error).sum()/len(error)= {np.abs(error).sum()/len(error)}")


z_fit = z_fit.reshape((n_x,n_y))


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X,Y,Z,c="red") # 原始数据
# ax.plot_surface(X0, Y0, z_fit, rcount=100,ccount=100,cmap='jet', alpha=0.99)
ax.plot_surface(X0, Y0, z_fit,cmap='jet', alpha=0.99)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title("曲面拟合")
plt.show()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-04-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档