Home > database >  Problem for solving a second order differential equation with ODEINT with a driven force
Problem for solving a second order differential equation with ODEINT with a driven force

Time:01-20

I need your help because I want to code the movement of a tower for a sinusoidal excitation. The problem is that when I plot the result, there is like a sinusoidal noise which looks abnormal and I don't know where does it come from... I was indeed expecting a more smooth curve as it is normally the case for a driven damped harmonic oscillator. Below is the equation of the movement:

ddx1   (f1/m1)*dx1   (k1/m1)*x1 = omega^2*Em*sin(omega*t)

with the initial conditions: x0 = 0 m and v0=dx0=0 m/s

here is my code:

from math import *
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

#params
m1=264000000. # kg
f1 = 5000000. # kg/s
k1=225000000. # N/m
#initial displacement of the tower:
x0 = 0. # m
dx0 = 0. # m/s
N=1000000
duration=200
time = np.linspace(0, duration, N)

# Creating the excitation
#sinusoidal excitation
def entry(Em,f,t):
    omega = 2*np.pi*f
    return -omega**2*Em*np.sin(omega*t)

# Equation: ddx1   (f1/m1)*dx1   (k1/m1)*x1 = omega^2*Em*sin(omega*t)
# Solving
def dX(X,t):
    #X = [x1, dx1]
    A=np.array([[   0  ,    1  ],
                [-k1/m1, -f1/m1]])
    B=np.array([0, -entry(1,50,t)])
    dX=np.dot(A,X) B
    return dX

result = odeint(dX,[x0,dx0],time)
plt.plot(time, result[:, 0])
plt.show()

And here are some pictures: a first picture and here when I zoom-in

Could you please tell me what is wrong with my code?

Thank you by advance for your help!

[EDIT] I had tried the code for smaller frequencies and it was more what I expected. What I hadn't thought of is as pointed out by JustLearning, that the difference between the natural frequency and the driving frequency is very important and therefore it is in fact normal to have these micro oscillations. Concerning the value of the parameters, they are indeed very important because they are those of the Taipei tower. But as there is each time a ratio of all these quantities, I think (but I could be wrong) that python does not bother doing the calculations. I am really new to this so thank you for answering so quickly and helping me.

CodePudding user response:

Assessing what's wrong with your ODE purely based on your plots is probably not wise. In order to check whether your code makes sense when run, you should probably go for convergence tests: pick a known analytic solution and check that the L2 norm of |numerical - analytic| decreases as expected as you make the timestep smaller.

That said, by only looking at the oscillation on top of the oscillation from your plots, what you see is nothing more than a superposition between the not-forced damped oscillation, and the forcing term. The reason why this superposed frequency is so microscopic is because it is roughly FIFTY! times larger than the natural damped frequency of the oscillator. If you change 50 in entry by something smaller, say 1, you will find that both the natural damped oscillation and the forcing will superpose with roughly similar frequency. Try 0.1 for an even more comparable superposition of all oscillations in play.

By the way, given your very crazy large parameters, you truly may want to do a convergence test and, if not successful, try some ODE solvers that can handle stiff ODEs -- something that the odeint default solver can' manage most of the time!

  • Related