Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

My Python is not working like I need it to

  1. Oct 9, 2008 #1
    This is what I have, and for some reason it wont 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
     
  2. jcsd
  3. Oct 9, 2008 #2

    alphysicist

    User Avatar
    Homework Helper

    Hi jrgoodin,

    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 (Text):


    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?
     
  4. Oct 9, 2008 #3
    yes, very much so......thank you
    -J
     
  5. Oct 10, 2008 #4

    alphysicist

    User Avatar
    Homework Helper

    Sure, glad to help!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: My Python is not working like I need it to
  1. I need help with Excel (Replies: 2)

Loading...