1

我正在尝试使用 Python 来解决一些简单的静态问题并生成结果图。出于某种原因,NumPy 在尝试计算我的计算结果的标准偏差时不接受我的数据(但在原始数据列表中成功)。我需要将yerr=[std(f10)...第 61 行更改为yerr=[std(solf10).... 但是,每次我尝试时,python 环境都会引发以下错误:

Traceback (most recent call last):
  File "C:\Users\evanlane\Dropbox\School\f13\homework\statics\lab1\data.py", line 70, in <module>
    ax.errorbar(x, [solf10avg,solf12avg,solf15avg], yerr=[std(solf10),std(f12),std(f15)], lw=1.5)
  File "C:\Program Files\Python33\lib\site-packages\numpy\core\fromnumeric.py", line 2590, in std
    keepdims=keepdims)
  File "C:\Program Files\Python33\lib\site-packages\numpy\core\_methods.py", line 107, in _std
    ret = um.sqrt(ret)
AttributeError: 'Float' object has no attribute 'sqrt'   

我试图找出数据的结构是否不同,print(type(f10), type(solf10))但这表明它们都是<class 'list'>类型。我应该如何按摩数据以更好地适应?我是 python 新手,所以如果您有任何其他样式更正,也请告诉我。

完整代码:

# Imports
from sympy import *
from numpy import *
import matplotlib.pyplot as plt

# Constants
g = 9.81

# Given data
l1, l2, l3 = 0.023, 0.07492, 0.0325
mw = 0.220
w = g*mw

# Collected data
m10 = [1540,1500,1400,1400,1670]
m10kg = [x/1000 for x in m10]

m12 = [1220, 1300, 1200, 1050, 900]
m12kg = [x/1000 for x in m12]

m15 = [770, 790, 740, 760, 750]
m15kg = [x/1000 for x in m15]

# Conversion from mass to force in Newtons due to gravity
f10, f12, f15 = [x*g for x in m10kg], [y*g for y in m12kg], [z*g for z in m15kg]

# Averages of the data
f10avg, f12avg, f15avg = mean(f10), mean(f12), mean(f15)

# Instantiate symbolic variables
fr, my = symbols('fr, my')

# Equation of moment about the origin
sumMoments = Eq(fr, (w*l2+my*(l1+l2))/(l1+l2+l3))

# Newtons acting axially on the straw, solved from equation
solf10 = [solve(sumMoments.subs(my,x)) for x in f10]
solf12 = [solve(sumMoments.subs(my,x)) for x in f12]
solf15 = [solve(sumMoments.subs(my,x)) for x in f15]

solf10 = [x for sub1 in solf10 for x in sub1]
solf12 = [x for sub1 in solf12 for x in sub1]
solf15 = [x for sub1 in solf15 for x in sub1]

solf10avg, solf12avg, solf15avg = mean(solf10), mean(solf12), mean(solf15)

# Plotting section
# ------------------

# X positions
x = [10,12,15]

#Uncomment for hand-drawn style
#plt.xkcd()

fig = plt.figure()
ax = fig.add_subplot(111)

offset = .5

ax.errorbar(x, [solf10avg,solf12avg,solf15avg], yerr=[std(f10),std(f12),std(f15)], lw=1.5)
plt.text(x[0],solf10avg + offset, r'  $F_{10 cm}=\ %.3f \ N$' %(solf10avg), fontsize=18)
plt.text(x[2],solf15avg + offset, r'  $F_{15 cm}=\ %.3f \ N$' %(solf15avg), fontsize=18)
plt.text(x[1],solf12avg + offset, r'  $F_{12 cm}=\ %.3f \ N$' %(solf12avg), fontsize=18)

plt.xlim([9,20])
plt.ylim([0,20])

plt.title("Straw Yield Point Test", fontsize=24)
plt.xlabel("Length (cm)", fontsize=18)
plt.ylabel("Axial Force on Straw\n at Yield (N)", fontsize=18)

plt.minorticks_on()
plt.grid(which="both")

#plt.savefig('fig_1.pdf')

plt.show()
4

2 回答 2

3

您的一个 sympy 计算的输出是一个 sympyFloat对象,它不是 numpy 识别为应该强制转换为 C 的对象double。相反,它只是从中生成一个对象数组(即dtype=object)。numpy ufuncs 在对象数组上的工作方式是在对象上寻找同名的方法,所以numpy.sqrt(solf10)做相当于numpy.array([x.sqrt() for x in solf10]).

将列表中的值显式强制为 true float

solf10 = [float(x) for sub1 in solf10 for x in sub1]
于 2013-10-14T22:13:26.007 回答
1

您是否注意到您的以下代码:

# Collected data
m10 = [1540,1500,1400,1400,1670]
m10kg = [x/1000 for x in m10]
...

您将整数除以整数,因此会产生一个带有四舍五入数字的列表,例如:

m10kg = [1, 1, 1, 1, 1]

您可以通过将其除以 1000.0 轻松修复它,因此它将转换为浮点系列,就像这样:

# Collected data
m10 = [1540,1500,1400,1400,1670]
m10kg = [x/1000.0 for x in m10]
...

所以一般来说,在分裂的情况下:

float = float / float
int = int / int
float = int / float
float = float / int
于 2013-10-15T00:51:20.817 回答