继续接收太多用于插值的数组索引

2024-10-02 12:30:31 发布

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

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

我无法按照下面的编码顺序完成for循环。我没有看到任何错误,因为我选择NP.WORD(NPUTE)来创建席的一维数组,并且由于某些原因,循环不会填充这些值。p>

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

Tags: ofthetofordatanprangepoints
2条回答

np.array的文档中:

Parameters:

object: _array_like_

An array, any object exposing the array interface, an object whose array method returns an array, or any (nested) sequence.

这意味着数组应该接收类似列表的内容,以便进行转换,而输入返回字符串。python在一天结束时试图做的是

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

虽然做这样的事情很有诱惑力

np.array(eval(input()))

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

np.array(list(map(int, input('Enter data point: ')
                       .replace('[','')
                       .replace(']','')
                       .split(','))))

数据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.WORD(NPLE)来创建席的一维数组,并且由于某些原因,循环不会填充这些值。”p>

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

如果我将输入行更改为:

   ...: 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])

相关问题 更多 >

    热门问题