我正在尝试使用 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()