无法用 3D 或 2d 轮廓中的边界值范围拟合曲线

问题描述 投票:0回答:0

我正在尝试拟合我的函数,但我无法用边界值拟合它,而且我无法更改这些边界值。有什么办法可以解决这种问题??? 有没有更好的方法来做这种拟合?

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# x_y data values
xmin, xmax, nx = -9289.34, 9668.51, 51
ymin, ymax, ny = -9289.34, 9668.51, 51
x, y = np.linspace(xmin, xmax, nx), np.linspace(ymin, ymax, ny)
# f(x)_f(y) data values
z1min, z1max, nz1 = 4e-15,5.76e-11, 51
z2min, z2max, nz2 = 1.29e-14, 5.68e-11, 51
fx, fy = np.linspace(z1min, z1max, nz1), np.linspace(z2min, z2max, nz2)
X, Y = np.meshgrid(x, y)
Z = np.outer(fx, fy)
# Fitting Function/AW
def bi_max(x,y, A, vpet, vpat, v):
    return ((A/np.pi**3/2)*(1/vpat)*(1/vpet**2))*np.exp(-(((x-v)/vpat)**2) - ((y/vpet)**2))
def _bi_max(M, *args):
    x, y = M
    arr = np.zeros(x.shape)
    for i in range(len(args)//5):
       arr += bi_max(x, y, *args[i*5:i*5+4])
    return arr
# Initial guesses to the fit parameters.
guess_prms = [(8e6, 90, 70, 90),
              (4e5, 350, 250, -350),
              (1e5, 750, 550, -750),
              (1e4, 1500, 1300, -1500),
              (1e4, 3500, 2700, -3500)
# Flatten the initial guess parameter list.
p0 = [p for prms in guess_prms for p in prms]
print(p0)
bounds = [[+1e5,+1,+1,-1e7,
           +1e4,+1,+1,-1e7,
           +1e4,+1,+1,-1e7,
           +1e3,+1,+1,-1e7,
           +1e3,+1,+1,-1e7],
           [+1e7,+100,+100,+1e7,
           +1e6,+400,+400,+1e7,
           +1e6,+800,+800,+1e7,
           +1e5,+1600,+1600,+1e7,
           +1e5,+4000,+4000,+1e7]]
xdata = np.vstack((X.ravel(), Y.ravel()))
popt, pcov = curve_fit(_bi_max, xdata, Z.ravel(), p0, method='trf', maxfev=100000, bounds=bounds)
fit = np.zeros(Z.shape)
for i in range(len(popt)//5):
    fit += bi_max(X, Y, *popt[i*5:i*5+4])
print('     N,            Vpet,                Vpat,             Vd')
print(popt)    
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot_surface(X, Y, fit, color='white',linewidth=1, antialiased=True)
cset = ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_zlim(np.min(Z),np.max(Z))
plt.show()
norm=None#LogNorm(vmin=1e-16, vmax=1e-10)
fig = plt.figure()
ax = fig.add_subplot(111)
cc = ax.contour(X,Y,fit, colors='black')
cset = ax.contour(X, Y, Z, colors='red')
plt.xlim([-7500, 7500])
plt.ylim([-7500, 7500])
plt.show()

2D contour Fit 3D surface Fit

optimization curve-fitting data-fitting model-fitting
© www.soinside.com 2019 - 2024. All rights reserved.