0

interp - 使用拉格朗日插值数据的程序

我无法在下面的编码序列中完成 for 循环。我看不出有什么问题,因为我选择 np.empty(nplot) 为 xi 创建一维数组,并且由于某种原因,循环不会填充这些值。

def intrpf(xi,x,y):
    """Function to interpolate between data points
       using Lagrange polynomial (quadratic)
       Inputs
        x    Vector of x coordinates of data points (3 values)
        y    Vector of y coordinates of data points (3 values)
        xi   The x value where interpolation is computed
      Output
        yi   The interpolation polynomial evaluated at xi
    """

#* Calculate yi = p(xi) using Lagrange polynomial
    yi = ( (xi-x[1])*(xi-x[2])/((x[0]-x[1])*(x[0]-x[2])) * y[0]
    + (xi-x[0])*(xi-x[2])/((x[1]-x[0])*(x[1]-x[2])) * y[1]
    + (xi-x[0])*(xi-x[1])/((x[2]-x[0])*(x[2]-x[1])) * y[2] )
    return yi

#* Initialize the data points to be fit by quadratic
x = np.empty(3)
y = np.empty(3)
print ('Enter data points as x,y pairs (e.g., [1, 2]')
for i in range(3):
    temp = np.array(input('Enter data point: '))
    x[i] = temp[0]
    y[i] = temp[1]

#* Establish the range of interpolation (from x_min to x_max)
xr = np.array(input('Enter range of x values as [x_min, x_max]: '))

我被困在这部分,它似乎设置正确,但是“数组索引太多”出现在 for 循环内的 xi[i] 上。

#* Find yi for the desired interpolation values xi using
#  the function intrpf
nplot = 100     # Number of points for interpolation curve
xi = np.empty(nplot)
yi = np.empty(nplot)
for i in range(nplot) :
    xi[i] = xr[0] + (xr[1]-xr[0])* i/float(nplot)
    yi[i] = intrpf(xi[i], x, y)    # Use intrpf function to interpolate
4

2 回答 2

0

input您的数据线发生错误:

Enter data points as x,y pairs (e.g., [1, 2]
Enter data point: [1,2]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-8d648ad8c9e4> in <module>
     22 for i in range(3):
     23     temp = np.array(input('Enter data point: '))
---> 24     x[i] = temp[0]
     25     y[i] = temp[1]
     26 

IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

代码甚至没有达到“我选择 np.empty(nplot) 为 xi 创建一维数组,并且由于某种原因循环不会填充这些值。” 部分。

寻求帮助时,请提供有关错误的完整准确信息。

如果我将输入行更改为:

   ...: x = np.empty(3) 
   ...: y = np.empty(3) 
   ...: print ('Enter data points as x,y pairs') 
   ...: for i in range(3): 
   ...:     temp = input('Enter data point: ').split() 
   ...:     x[i] = temp[0] 
   ...:     y[i] = temp[1] 
   ...:  
   ...: #* Establish the range of interpolation (from x_min to x_max) 
   ...: xr = np.array(input('Enter range of x values as x_min, x_max: ').split(),float)                         
Enter data points as x,y pairs
Enter data point: 1 2
Enter data point: 3 4
Enter data point: 5 6
Enter range of x values as x_min, x_max: 0 4
In [9]: x                                                                                                       
Out[9]: array([1., 3., 5.])
In [10]: y                                                                                                      
Out[10]: array([2., 4., 6.])
In [11]: xr                                                                                                     
Out[11]: array([0., 4.])

通过用户获取数组值input并不理想,但这至少有效。 input(在 Py3 中)不评估输入;它只是返回一个字符串。我拆分它(在默认空间上),然后将值分配给一个数组。 x被定义为浮点数组,因此x[i]=temp[0]负责将字符串转换为浮点数。同样,该xr行从字符串输入创建一个浮点数组。这种输入风格不是很健壮;我很容易用错误的输入引发错误。

===

其余代码使用此输入运行:

In [12]: nplot = 100     # Number of points for interpolation curve 
    ...: xi = np.empty(nplot) 
    ...: yi = np.empty(nplot) 
    ...: for i in range(nplot) : 
    ...:     xi[i] = xr[0] + (xr[1]-xr[0])* i/float(nplot) 
    ...:     yi[i] = intrpf(xi[i], x, y)    # Use intrpf function to interpolate 
    ...:                                                                                                        
In [13]: xi                                                                                                     
Out[13]: 
array([0.  , 0.04, 0.08, 0.12, 0.16, 0.2 , 0.24, 0.28, 0.32, 0.36, 0.4 ,
       0.44, 0.48, 0.52, 0.56, 0.6 , 0.64, 0.68, 0.72, 0.76, 0.8 , 0.84,
       ...
       3.52, 3.56, 3.6 , 3.64, 3.68, 3.72, 3.76, 3.8 , 3.84, 3.88, 3.92,
       3.96])
In [14]: yi                                                                                                     
Out[14]: 
array([1.  , 1.04, 1.08, 1.12, 1.16, 1.2 , 1.24, 1.28, 1.32, 1.36, 1.4 ,
       1.44, 1.48, 1.52, 1.56, 1.6 , 1.64, 1.68, 1.72, 1.76, 1.8 , 1.84,
       ....
       4.52, 4.56, 4.6 , 4.64, 4.68, 4.72, 4.76, 4.8 , 4.84, 4.88, 4.92,
       4.96])
于 2020-06-11T05:18:36.533 回答
0

来自以下文档np.array

参数:

对象:_array_like_

数组、任何公开数组接口的对象、其数组方法返回数组的对象或任何(嵌套)序列。

这意味着数组应该接收类似列表的东西,以便进行转换,而输入返回一个字符串。python 在一天结束时试图在这里做的事情就像

np.array('[1, 2]')

虽然做类似的事情可能很诱人

np.array(eval(input()))

你永远不应该这样做,因为它是不安全的,因为它允许用户在你的程序中执行任何类型的代码。如果你真的需要那种输入,我会建议像

np.array(list(map(int, input('Enter data point: ')
                       .replace('[','')
                       .replace(']','')
                       .split(','))))
于 2020-06-10T23:26:35.363 回答