First off my apologies if this is a noob issue but I am completely new to coding.
I am following a guide on building a harmonic oscilation simulator in Vpython but every time I try to run the program I get this error.
File "C:/Users/Nathan/Desktop/vspring", line 14 F_spring=-k*(block.pos -L) TypeError: bad operand type for unary -: 'tuple'
Can someone please look at the code and tell me where I have gone wrong. I have literally followed the guide to the letter as I am just copying line by line what it tells me to put. But it has no trouble shooting. It just states "Your program is now ready to run"
from visual import *
from visual.graph import *
L=vector(1,0,0)
s=vector(1.,0,0)
block=sphere(radius=0.25, color=color.cyan, pos=L+s)
spring=cylinder(pos=(0,0,0), axis=L+s, radius=.1)
scene.autoscale=0
posgraph=gcurve(color=color.green)
k=100,; g=9.8; mu_s=.5; mu_k=.5; m=1.; dt=.0001; d=0.; t=0.
F_mu_s=mu_s*m*g
F_spring=-k*(block.pos-L)
almost_zero_p=mag(F_spring)*dt
block.p=vector(0,0,0)
while not(mag(block.p)<almost_zero_p and F_mu_s>=mag(F_spring)):
if not(mag(block.p)<almost_zero_p):
F_mu_k=m*g*mu_k*norm(block.p)
else:
F_ms_k=vector(0,0,0)
F_spring=-k*(block.pos-L)
Fnet=F_spring+F_mu_k
block.p=block.p+Fnet*dt
block.pos=block.pos+block.p/m*dt
spring.axis=block.pos-spring.pos
d=d+mag(block.p/m)*dt
posgraph.plot(pos=(t,block.pos.x-L.x))
t=t+dt
print "Total distance traveled is", d
When you do at line 12 :
k=100,
You actually create a tuple (100,).
If you wanted to create a float, do :
k=100.
Indeed when you were trying to do -k
, the unary operator -
doesn't work because of k
type (tuple). I think this is just a syntax mistake.