我的任务是:
我们研究函数 f(x)=ln(x)。 我们想用x和f(x)的值分别填充两个数组x和y。 在间隔[1,10]中使用101个均匀间隔的x值。首先创建数组x 长度正确的y,包含所有零。然后计算并填写 x和y中的每个元素都有一个for循环
我试图用这种方式解决这个问题,但考虑到我的印刷品,它似乎不正确:
"Problem 6.1 - Import arrays; loop version"
import numpy as np
from numpy import log
x=np.linspace(1,10,101)
x_array=["0"]*101
y_array=["0"]*101
for x in range(len(x)):
f=log(x)
x_array.append(x)
y_array.append(f)
有人知道如何解决这个问题吗
您可以使用
range()
生成x_array
,然后使用math.log()
相应地填充y_array
:或者,简而言之,使用列表理解:
您实际上不需要填充数组^{} 为您生成一个均匀间隔的
numpy
数组。但是,根据您的要求,您可以这样做:解决方案1:循环方法
解决方案2:列表理解方法
输出:
编辑:
添加打印功能:
输出:
相关问题 更多 >
编程相关推荐