My Python is not working like I need it to

  • Thread starter jrgoodin
  • Start date
  • Tags
    Python
In summary: Keep in mind that you will need to update the position and velocity of the weight in each iteration of the loop, based on the force acting on it from the spring. This will allow the weight to move and be affected by the spring's force.
  • #1
jrgoodin
2
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
  • #2
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?
 
  • #3
yes, very much so...thank you
-J
 
  • #4
Sure, glad to help!
 

1. Why is my code not running at all?

There could be multiple reasons for this issue. Firstly, make sure that you have installed Python correctly on your computer. Also, check for any syntax errors or missing modules in your code. If everything seems correct, try restarting your computer and running the code again.

2. How do I fix error messages in my Python code?

Error messages are helpful in identifying the issue in your code. Make sure to carefully read the error message and check the line number mentioned. It could be a simple syntax error or a missing module. Use online resources or consult with an experienced programmer to help you debug the error.

3. My code is taking too long to execute. How can I improve its performance?

There are a few ways to improve the performance of your code. You can optimize your algorithms, use built-in functions or libraries, and reduce the number of unnecessary operations. Another way is to use a more powerful computer or to parallelize your code if possible.

4. I am getting incorrect outputs from my code. How can I troubleshoot this?

Firstly, check your code for any logical errors. Make sure that your variables and functions are named correctly and the logic is sound. Additionally, try using print statements to track the values of your variables and identify where the issue may be occurring. You can also try using a debugger tool to step through your code and identify the problem.

5. How can I make my Python code more readable and maintainable?

One way to improve the readability and maintainability of your code is by following PEP 8 guidelines. This includes using consistent and meaningful variable names, proper indentation, and clear commenting. You can also break down your code into smaller functions or modules to make it more manageable and reusable.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
Back
Top