具有多个多项式拟合的曲面图

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

我所问的可能是不可能的,但我希望你们能帮助我。

所以我有两个二维数组,f1(x1) = y1 和 f2(x2) = y2。我想绘制它们的比率的曲面图,因此 z 维度为 (f2(x2)/f1(x1))。不幸的是,无论从哪个方向解决问题,我都会遇到困难。

我的主要问题是每个数组的范围不同,x1 从 300 到 50,000,x2 从 300, 1200。现在我很高兴假设 f2(x2) = f2(1200) 对于所有 x2 > 1200。但是这个界限意味着我不可能以任何现实的方式拟合该数据的多项式(我的第一组数据可以通过五阶多项式很好地再现,而我的第二组数据最适合一阶多项式) 。是否有另一种方法可以将函数拟合到 (x2,y2),以便它采用边界之外的所有点的边界值?

我极其错误的尝试看起来像这样,

# x1 is an array from 300 to 50,000 in steps of 50
# x2 is an array from 300 to 1,150 in steps of 50

f1_fit = np.poly1d(np.polyfit(x1, y1, 5))
f2_fit = np.poly1d(np.polyfit(x2, y2, 1))

X, Y = np.meshgrid(x1, x2)
Z = (f2_fit(x2) / f1_fit(x1))

有趣的是,看似无害的问题却可能成为令人头疼的问题。 :D

编辑:这是玩具数据量,

x1 = x2 = [  300.   350.   400.   449.   499.   548.   598.   648.   698.   748.
             798.   848.   897.   947.   997.  1047.  1097.  1147.  1196.  1246.
             1296.  1346.  1396.  1446.  1496.  1546.  1595.  1645.  1695.  1745.]

y1 = [  351.   413.   476.   561.   620.   678.   734.   789.   841.   891.
        938.   982.  1023.  1062.  1099.  1133.  1165.  1195.  1223.  1250.
        1274.  1298.  1320.  1340.  1360.  1378.  1395.  1411.  1426.  1441.]

y2 = [ 80.  75.  70.  65.  62.  58.  58.  52.  48.  46.  44.  41.  38.  35.  32.
       32.  29.  30.  30.  30.  30.  30.  30.  30.  30.  30.  30.  30.  30.  30.]
python matplotlib curve-fitting surface
1个回答
0
投票

所以我设法解决了我的问题。如上所述,我通过设置 x1 = x2 对数据进行了一些预处理,从而推断出 f(x2) 的边缘值。

import numpy as np
import scipy.interpolate as interp
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

data1 = np.loadtxt('data1.dat')
data2 = np.loadtxt('data2.dat')

X = []
Y = []
Z = []

for i in data1:
    for j in data2:
        X.append(i[0])
        Y.append(j[0])
        Z.append(i[1]/j[1])

x_mesh,y_mesh, = np.meshgrid(np.linspace(300,50000,200), np.linspace(300,50000,200))

z_mesh = interp.griddata((X,Y),Z,(x_mesh,y_mesh),method='linear')

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(x_mesh,y_mesh,z_mesh,cstride=10,rstride=10,cmap='OrRd_r')
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.