我一直收到一个错误,指出无法添加标量和矢量数量。 [英] I keep getting an error that says cannot add scalar and vector quantities.
问题描述
scene.width = 600#设置我们的观察门户的大小
scene.height = 600
#创建情节
PosGraph =图形(宽度= 600,高度= 250,标题='水上漂浮的罐:实验与模拟',
ytitle =' 位置(m)',xtitle ='时间(秒)')
#expt = series( graph = PosGraph,color = color.cyan)
sim = series(graph = PosGraph,color = color.red)
#Load数据。它期望数据在行中,每组数据以逗号分隔。
#时间应该在第一列,而位置在第二列。
#它忽略任何其他专栏,虽然你可以读你的速度或其他。
#
#第一行必须有数据。删除所有标题或列名。
#f = read_local_file()
#创建一个空列表或数组来存储数据
t_data = []
x_data = []
#现在去行按行遍历文件,并将数据从第一列(数字0)添加到时间数组
#和第二列(数字1)到位置数组
#对于f.text.splitlines()中的行:
#cols = row.split(,)
#t_data.append(float(cols [0]) )
#x_data.append(float(cols [1]))
#也将它放在图表上。 X =时间,Y =位置
#expt.plot(float(cols [0]),float(cols [1]))
####
##现在模拟这些结果。以下是您需要为实验更改的内容
####
#设置一些常数
g = -9.8
deltat = .05#每个循环的时间间隔(以秒为单位);选择任何小值
质量= 0.022#这是测量的
Pi = 3.14
#我的振荡器是水平弹簧上的质量。你的振荡器可能取决于不同的因素
D = 1000#这是测量的。您也可以将它作为一个适合的参数来改变(kg)
p = D #Rho
f_friction = 1.1#这可能是很难衡量,所以我会用它作为一个适合的参数来改变
#从0开始是一个很好的方法来看你的模拟中的振荡工作
y_init = -0.028#查看你的第一个数据点
y_equil = -0.017#你振荡的点。一个不错的选择来自数据
r = 0.015
#SET INTITIAL VALUES OF THE
t = 0 #initialize time。选择实验数据的第一个时间点,使图表匹配
v_i = vec(0,1,0)#initialize speed。看看你的第一个位置数据点估计初始速度
#DEFINE一些物体
#First,我的块
container = cylinder(pos = vec(0,y_init,0),size = vector(.03,.049,.03),v = v_i)
#I可以添加弹簧,但它只会用于表演
#THE计算 - 这个循环是所有魔法发生的地方
而t< 6.8:#Tll它运行循环的时间,以秒为单位(你的实验数据需要多长时间?)
rate(40)#tell它运行循环的速度有多快(数字更快更快)
H = container.pos
V = Pi * r ^ 2 * H
v = v_i
#计算振荡器上的力。我的是弹簧和摩擦力。你的可能会有所不同
#Fsp = -spring_k *(block.pos - vector(x_equil,0,0))# - k * delata_x这是块位减去平衡位置
Ffric = -0.5 * v.norm
Fg = g *质量#与速度方向相反
Fb = p * V * g
#获取净力矢量
Fnet = Fb + Fg + Ffric
#找到飞行器的加速度。牛顿第二定律!
a = Fnet /质量
#允许加速度以短时间间隔改变飞行器的速度'deltat '
#vf = vi + a * t
v = v + a * deltat
#allow the速度改变工艺的位置一小段时间'deltat'
#xf = xi + v * t
container.pos = container.pos + container。 v * deltat
#plot匹配的实验数据
sim.plot(t,container .pos)
#interval interval by interval deltat
t = t + deltat
#go回到循环的顶部并重新计算新位置的所有内容;重复直到'while'声明得到满足
我尝试过:
我试图给每个变量一个数字
v_i = vec( 0 , 1 , 0 )#initialize speed。查看您的第一个位置数据点来估算初始速度
...
v = v_i#所以v被定义为向量
。 ..
v = v + a * deltat#你不能将单个值添加到向量
scene.width = 600 #Set the size of our viewing portal
scene.height = 600
# Create the plot
PosGraph = graph(width=600, height=250, title='Canister Floating on Water: Experiment and Simulation',
ytitle='position (m)', xtitle='time (sec)')
#expt = series(graph = PosGraph, color=color.cyan)
sim = series(graph = PosGraph, color=color.red)
# Load the data. It expects data in rows, where each set of data is comma separated.
# The time should be in the first column, and position in the second.
# It ignores any other columns, although you could have yours read in velocity or whatever.
#
#It must have data in the first row. Remove any headers or column names.
#f = read_local_file()
# Create an empty list or array to store the data
t_data = []
x_data = []
# Now go row by row through the file, and add the data from the first column (number 0) to time array
# and the second column (number 1) to position array
#for row in f.text.splitlines():
#cols = row.split(",")
# t_data.append(float(cols[0]))
# x_data.append(float(cols[1]))
# put it on the graph, too. X = time, Y = position
# expt.plot(float(cols[0]), float(cols[1]))
####
## Now simulate those results. Below here is what you'll need to change for your experiment
####
#Set some CONSTANTS
g = -9.8
deltat = .05 #the time interval for each loop in seconds; choose any small value
mass = 0.022 # This was measured
Pi=3.14
#My oscillator was a mass on a horizontal spring. Your oscillator may depend on different factors
D = 1000 # This was measured. You could also use it as a fitting parameter that you vary (kg)
p = D #Rho
f_friction = 1.1 # This is probably hard to measure, so I'll use it as a fitting parameter to vary
# starting with 0 is a good way to see that the oscillation in your simulaiton works
y_init = -0.028 # look at your first data point
y_equil = -0.017 # the point around which you oscillate. A good choice to start with comes from the data
r=0.015
#SET INITIAL VALUES OF THINGS
t = 0 #initialize time. Choose the first time point of your experimental data so the graphs can match
v_i = vec(0,1,0) #initialize speed. Look at your first position data points to estimate the initial speed
#DEFINE SOME OBJECTS
#First, my block
container = cylinder(pos=vec(0,y_init,0), size = vector(.03, .049, .03), v = v_i)
#I could add the spring, but it would only be for show
#THE CALCULATION - THIS LOOP IS WHERE ALL THE MAGIC HAPPENS
while t < 6.8: #tell it how long to run the loop, in seconds (how long does your experimental data go?)
rate(40) #tell it how quickly to run the loop (bigger numbers are faster)
H= container.pos
V= Pi*r^2*H
v=v_i
#Calculate the forces on your oscillator. Mine are spring and friction. Yours may be different
# Fsp = -spring_k * (block.pos - vector(x_equil, 0,0)) # - k *delata_x which is block position minus equilibrium position
Ffric=-0.5*v.norm
Fg = g*mass # opposite direction to velocity
Fb = p*V*g
#Get the net force vector
Fnet = Fb + Fg + Ffric
#Find the acceleration of the craft. Newton's Second Law!
a = Fnet/mass
#allow the acceleration to change the velocity of the craft for a short time interval 'deltat'
# vf = vi + a*t
v = v + a*deltat
#allow the velocity to change the position of the craft for a short time interval 'deltat'
# xf = xi + v*t
container.pos = container.pos + container.v*deltat
#plot the matching data to your experiment
sim.plot(t,container.pos)
#advance time by the interval deltat
t = t + deltat
#go back to the top of the loop and re-calculate everything at the new position; repeat until the 'while' statement is satisfied
What I have tried:
I have tried to give each variable a number
v_i = vec(0,1,0) #initialize speed. Look at your first position data points to estimate the initial speed ... v=v_i # so v is defined as a vector ... v = v + a*deltat # you cannot add a single value to a vector
这篇关于我一直收到一个错误,指出无法添加标量和矢量数量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!