Euler method for modeling simple harmonic oscillation

In summary, the conversation discusses the use of the Standard Euler method for modeling a simple harmonic oscillator, with the goal of demonstrating its non-stability and non-conservative energy behavior. The method is implemented in a computer program, but the error is noticeable even after just two cycles. After realizing a mistake in the code, the energy and position are plotted to show the non-conservative behavior.
  • #1
acadian
4
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,288
Last edited:
Mathematics news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
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!
 
  • #5
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!
 
  • #6
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,306

Related to Euler method for modeling simple harmonic oscillation

What is the Euler method for modeling simple harmonic oscillation?

The Euler method is a numerical approach used to approximate the solution of a differential equation. It involves dividing the time interval into small steps and using the slope of the function at each step to estimate the value of the function at the next step.

How is the Euler method applied to model simple harmonic oscillation?

To apply the Euler method to model simple harmonic oscillation, we first need to convert the second-order differential equation into two first-order differential equations. Then, we can use the Euler method to approximate the values of position and velocity at each time step.

What are the advantages of using the Euler method for modeling simple harmonic oscillation?

The Euler method is relatively simple to implement and requires minimal computational resources. It can also be used to model a wide range of differential equations, making it a versatile tool for scientists and engineers.

What are the limitations of using the Euler method for modeling simple harmonic oscillation?

The Euler method is a first-order method, which means that it can lead to significant errors in the approximation of the solution. This is especially true for stiff differential equations, where the step size needs to be very small to maintain accuracy.

Are there any alternative methods to the Euler method for modeling simple harmonic oscillation?

Yes, there are several alternative methods for modeling simple harmonic oscillation, such as the Runge-Kutta methods, the Adams-Bashforth methods, and the Verlet algorithm. These methods offer higher accuracy and stability compared to the Euler method, but they may also be more complex to implement.

Similar threads

  • General Math
Replies
7
Views
3K
  • General Math
2
Replies
38
Views
10K
Replies
7
Views
2K
Replies
4
Views
1K
Replies
4
Views
3K
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
13
Views
643
  • Introductory Physics Homework Help
Replies
16
Views
414
Replies
8
Views
3K
  • Classical Physics
2
Replies
36
Views
2K
Back
Top