我希望使用Python绘制Alpine1函数。函数如下所示:
Alp(x_1,…,x_n)=∑_(i=1)^n【| x|i*sin(x|i)+1.01x|i】
所以函数求和(from i=1 to n
)的倒数xi*sin(xi) +1.01xi
。如果没有求和,Python中的函数如下所示:
def f(x):
return (abs(x * np.sin(x) + 1.01 * x))
<席>这里,我用X代替XI,求和被省略。如何正确实现求和from i=1 to n
?简单地使用sum()只给出一个值,而我需要一个数组我想我需要朝着一种for i in range(1,...)
类型的东西的方向看,但我还没有弄明白
提前感谢能帮助我的人
Python和其他语言中的求和通常使用
for
循环实现。 在上面的公式中,您正在循环所有元素x1,x2,x3。。。xn现在,为了存储这些值,我们使用一个数组,或者在python中使用一个列表,它基本上是一个数据集合(顺序很重要)
这将是函数
f(x)
的输入。为了循环x中的所有值,我们使用语法这将打印所有值,即x_1、x_2、x_3。。。x_n
现在,我们将声明一个变量
summation = 0
,并存储所需的计算值,正如您在其中的问题中所提到的,并在循环遍历所有x值后返回求和值可以使用
np.sum
获取数组的和。Numpy的broadcasting和向量化注意将所有元素分别计算,然后求和以下是一个例子:
正如您的帖子提到的绘图,下面是Alpine1函数的热图如何应用于两个值:
这是3D曲面(
z = Alpine1(x1, x2)
)的外观:谢谢,我想我已经明白多了
实际上我在想另一件事。Alpine1是一个n维函数,因此假设我想采用函数直接产生的“2D版本”:
现在我该如何绘制它?仍然需要两个变量(实际上是数组)(x1和x2),但我认为
np.apply_along_axis
只适用于1d。我该怎么处理也许用x表示x1,用y表示x2更容易理解
相关问题 更多 >
编程相关推荐