Euler method for modeling simple harmonic oscillation

AI Thread Summary
The discussion focuses on using the Standard Euler method to model a simple harmonic oscillator, represented by two coupled first-order differential equations derived from Hooke's law. The numerical implementation is provided, but it was initially expected to show non-conserved energy, which did not occur as anticipated. Participants suggest plotting energy as a function of time to better illustrate the system's non-conservative nature. A modification to the equations was proposed to improve the accuracy of the simulation. Ultimately, the conversation highlights the importance of correctly implementing the equations and analyzing energy conservation in numerical modeling.
acadian
Messages
3
Reaction score
0
Hello!

An assignment for my computational modeling course is to demonstrate the use of the Standard Euler method for modeling a simple harmonic oscillator; in this case, a mass attached to the end of a spring.

I have the two coupled first-order differential equations satisfying hookes law: dx/dt = v, and dv/dt = -(k/m)*x

The numerical solutions of which are v(t + dt) = v(t) - k*x(t)*dt / m, and x(t + dt) = x(t) + v(t)*dt to model the velocity and position, respectively.

In a computer program, I've represented this as:

x = x + v*dt;
v = v - k*x*dt / m;
t = t + dt;

with dt = 0.04, m = 1, k = 1, and initial values v = 0, t = 0, and x = 5.

The Purpose of the exercise is to demonstrate how the standard Euler Method is non-stable and results in non-conserved energy.

When iterating the above Euler method for sufficiently large periods of time, I've expected x to grow larger after each period but my numerical method above is acting like a conserved-energy Improved Euler method (Euler-cromer)? Please see attached plot.
 

Attachments

  • Screen Shot 2015-02-09 at 10.59.10 PM.png
    Screen Shot 2015-02-09 at 10.59.10 PM.png
    15.8 KB · Views: 1,364
Last edited:
Mathematics news on Phys.org
acadian said:
Hello!

An assignment for my computational modeling course is to demonstrate the use of the Standard Euler method for modeling a simple harmonic oscillator; in this case, a mass attached to the end of a spring.

I have the two coupled first-order differential equations satisfying hookes law: dx/dt = v, and dv/dt = -(k/m)*x

The numerical solutions of which are v(t + dt) = v(t) - k*x(t)*dt / m, and x(t + dt) = x(t) + v(t)*dt to model the velocity and position, respectively.

In a computer program, I've represented this as:

x = x + v*dt;
v = v - k*x*dt / m;
t = t + dt;

with dt = 0.04, m = 1, k = 1, and initial values v = 0, t = 0, and x = 5.

The Purpose of the exercise is to demonstrate how the standard Euler Method is non-stable and results in non-conserved energy.

When iterating the above Euler method for sufficiently large periods of time, I've expected x to grow larger after each period but my numerical method above is acting like a conserved-energy Improved Euler method (Euler-cromer)? Please see attached plot.

Your plot shows only a few cycles, where the method should probably be "good enough." What is the energy as a function of time? Why not plot the energy error as a function of time? Error = PE_0 - (KE + PE) -- When you plot the difference, it will be much more obvious that the system is non-conservative.
 
acadian said:
In a computer program, I've represented this as:

x = x + v*dt;
v = v - k*x*dt / m;
t = t + dt;
This does not implement the system of equations you have. Pay close attention to the time-dependence of the variables.
 
Quantum Defect said:
Your plot shows only a few cycles, where the method should probably be "good enough."
Euler's method is bad enough that the error is clearly noticeable even after two cycles!
 
DrClaude said:
This does not implement the system of equations you have. Pay close attention to the time-dependence of the variables.
... I missed that!
 
Thank you!

Modification:

e = (v*v/2) + (x*x/2);
a = -k*x / m;
x = x + v*dt;
v = v + a*dt;
t = t + dt;

Plotting e, t and x produces the following:
 

Attachments

  • Screen Shot 2015-02-10 at 6.01.05 PM.png
    Screen Shot 2015-02-10 at 6.01.05 PM.png
    15.3 KB · Views: 1,375
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Suppose ,instead of the usual x,y coordinate system with an I basis vector along the x -axis and a corresponding j basis vector along the y-axis we instead have a different pair of basis vectors ,call them e and f along their respective axes. I have seen that this is an important subject in maths My question is what physical applications does such a model apply to? I am asking here because I have devoted quite a lot of time in the past to understanding convectors and the dual...
Back
Top