Python Help in logic for amplitude vs frequency

AI Thread Summary
The discussion revolves around solving a physics problem related to a damped driven harmonic oscillator using Python. The user has implemented a numerical solution with a Runge-Kutta method to simulate the motion of a spring-mass system. The key challenge is making the angular frequency (omega) a variable in order to plot amplitude versus omega. The user notes that the problem is somewhat disconnected from the chapter's focus on integrating differential equations, suggesting that understanding the expressions for the damped driven harmonic oscillator is crucial for addressing the task. The code provided initializes parameters for the simulation, defines the external force, and sets up the differential equations governing the system's motion. The user seeks guidance on how to modify the code to achieve the desired variable frequency plotting.
ChrisVer
Science Advisor
Messages
3,372
Reaction score
465
I am trying to solve the problem 4.8 found http://phys.csuchico.edu/ayars/312/Handouts/comp-phys-python.pdf at pdf's page 115-6 [book's page 107-8].

My code so far is:
Python:
from mytools import rungekuta4
from pylab import *

#set globals
G=9.81
MASS=1.0
K=42.0
MU=0.15
A=1.
OMEGA=0.01

#set diff solver variables
N=1000
tau=5.
dt= tau/float(N-1)
t=linspace(0,tau,N)

def Fexternal(A,w,t):
    return A*cos(w*t)

def springmass(xv_vector,time):
    # x' =v
    # v' = -k/m x +/- mu g + F[t]
    diff0= xv_vector[1]
    if diff0>0:
        diff1= -(K/MASS)*xv_vector[0] - MU*G +  F(A,OMEGA,time) / MASS
    else:
        diff1= -(K/MASS)*xv_vector[0] + MU*G + F(A,OMEGA,time) / MASS

    return array([diff0,diff1])

xo=1.0
vo=0.0

y=zeros([N,2])
y[0,0]=xo
y[0,1]=vo

for i in range(N-1):
     y[i+1]= rungekuta4(y[i],t[i],dt,springmass)

plot(t,y[:,0],'r-')
show()

My problem is that I cannot see how I can make the w a variable... So that I could plot the amplitude vs the omega...
 
Last edited by a moderator:
Technology news on Phys.org
Yes, it's a bit weird they ask this in a chapter where most of the material is about integrating differential equations. I think here you really need the expressions for the damped driven harmonic oscillator
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top