If you attach torsion spring to inverted pendulum, is this the equation of motion? The spring is attached on one end to a pivot point and also at the pivot point the spring is attached. I used L/2 for the location of the center of mass.
I*theta"+mg(L/2)sin theta-theta*k=0