多重多项式拟合曲面图

2024-04-26 00:39:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我的要求可能不可能,但我希望你们能帮我。你知道吗

所以我有两个二维数组,f1(x1)=y1,和f2(x2)=y2。我想做一个这些比率的曲面图,所以z维是(f2(x2)/f1(x1))。不幸的是,无论我从哪个方向解决问题,我都会遇到困难。你知道吗

我的主要问题是每个数组的范围不同,x1从300到50000,x2从3001200。现在我很高兴假设f2(x2)=f2(1200)代表所有x2>;1200。但是这个界限意味着我不可能用任何实际的方法来拟合一个多项式这个数据(我的第一组数据是用一个5阶多项式很好地复制的,而我的第二组数据是用一阶多项式最好地拟合的)。有没有另一种方法可以将函数拟合到(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.]

Tags: 数据方法fromanisnp数组array
1条回答
网友
1楼 · 发布于 2024-04-26 00:39:15

所以我设法解决了我的问题。如前所述,我对数据进行了一些预处理,设置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()

相关问题 更多 >