Scroll to Top

Virtual Math Learning Center

Virtual Math Learning Center Texas A&M University Virtual Math Learning Center

Calculating Work in Python: Hooke’s Law

Author: David Manuel

In this video, we use Python to calculate the amount of work done in moving a spring using Hooke's Law. First, we solve for the spring constant k using the given information. Once we know k, we can setup the integral representing the work done and evaluate it.

Transcript 23

Problem: It takes 10 Joules of work to move a spring from a natural length of 10cm to a length of 15cm. How much work would it take to move the spring from a length of 15cm to a length of 20cm?

Python Code: 
10 Joules of work to move a spring from a natural length of 10cm to a length of 15cm

How much work would it take to move the spring from a length of 15cm to a length of 20cm?

from sympy import *

x,k=symbols('x k')
F=k*x
Work1=integrate(F,(x,0,0.05))
ksol=solve(Work1-10,k)[0]
print(ksol)
Fnew=F.subs(k,ksol)
print(Fnew)
Work=integrate(Fnew,(x,.05,.10))
print('The work required is',Work,'J')

Open Python Notebook File

See more videos from this section