My Python is not working like I need it to

  • Context: Python 
  • Thread starter Thread starter jrgoodin
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion revolves around a Python code snippet intended to simulate the motion of a weight attached to a spring using the Visual Python library. Participants are exploring issues related to the movement of the weight and the implementation of physics principles, particularly Hooke's Law.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant shares a code snippet but notes that the weight does not move as expected.
  • Another participant suggests that the code lacks instructions for updating the weight's position, proposing an addition to the loop to move the weight at a constant velocity.
  • The second participant emphasizes the need to calculate the new position and velocity of the weight in each iteration to reflect the effects of the spring force.
  • A third participant expresses gratitude for the assistance received.

Areas of Agreement / Disagreement

Participants seem to agree on the need for modifications to the code to achieve the desired motion, but there is no consensus on the specific implementation details or the final approach to solving the problem.

Contextual Notes

The discussion does not resolve the specific implementation of the spring force effect on the weight's movement, leaving open questions about the correct expressions to use in the simulation.

jrgoodin
Messages
2
Reaction score
0
This is what I have, and for some reason it won't move...I trust all you smart people could point me in the right direction...thanks

from visual import *
from visual.graph import *


relaxedlength = vector(.60,0,0) # length of spring when it isn't stretched or compressed
spring = helix(pos=(-.75,0,0),axis=relaxedlength, radius=.1,coils=8,thickness=.01,color=color.green)
spring.constant = 25 # k

weight = box(pos=(0,0,0),size=(.3,.3,.3),color=color.yellow)
weight.mass = 10 # kg
weight.velocity = vector(.30,0,0)
weight.acceleration = vector(.01,0,0)
weight.force = vector(0.3,0,0)

frictionlessSurface = box(size=(2,.02,.5),pos=(0,-.16,0))
wall = box(size=(.04,.5,.3),pos=(-.77,.1,0),color=color.red)
spring.displacement = weight.pos # the weight starts at (0,0,0) and is attached to spring
spring.axis = relaxedlength + spring.displacement


dt = .01 # seconds
t = 0 # total time
while 1:
rate(100)
t += dt

# Calculate the spring force using Hooke's Law
spring.force = -spring.constant * spring.displacement

# The spring force acts on the weight
weight.force = spring.force

# calculate the new spring displacement
spring.displacement = weight.pos

# update the length of the spring
spring.axis = relaxedlength + spring.displacement
 
Technology news on Phys.org
Hi jrgoodin,

jrgoodin said:
This is what I have, and for some reason it won't move

Your code is not yet telling the weight to move. For example, to get the weight to move at constant velocity, you might add a line to the end like this:

Code:
from visual import *
from visual.graph import *


relaxedlength = vector(.60,0,0) # length of spring when it isn't stretched or compressed
spring = helix(pos=(-.75,0,0),axis=relaxedlength, radius=.1,coils=8,thickness=.01,color=color.green)
spring.constant = 25 # k

weight = box(pos=(0,0,0),size=(.3,.3,.3),color=color.yellow)
weight.mass = 10 # kg
weight.velocity = vector(.30,0,0)
weight.acceleration = vector(.01,0,0)
weight.force = vector(0.3,0,0)

frictionlessSurface = box(size=(2,.02,.5),pos=(0,-.16,0))
wall = box(size=(.04,.5,.3),pos=(-.77,.1,0),color=color.red)
spring.displacement = weight.pos # the weight starts at (0,0,0) and is attached to spring
spring.axis = relaxedlength + spring.displacement


dt = .01    # seconds
t = 0 # total time
while 1:
    rate(100)
    t += dt
    
    # Calculate the spring force using Hooke's Law
    spring.force = -spring.constant * spring.displacement

    # The spring force acts on the weight
    weight.force = spring.force
    
    # calculate the new spring displacement
    spring.displacement = weight.pos

    # update the length of the spring
    spring.axis = relaxedlength + spring.displacement

    weight.pos = weight.pos + (dt,0,0)

Now of course this is not what you want for your problem; you'll have to replace the (dt,0,0) with an expression for the effect of the spring. (Also, you'll have to use the position of the weight to update the spring displacement for the next iteration of the loop, etc.)

But the point is for each iteration of the loop you have to calculate the new position (and velocity) of the weight; those give the new value of the force for the next pass of the loop, etc. Does this help?
 
yes, very much so...thank you
-J
 
Sure, glad to help!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K