Euler method for modeling simple harmonic oscillation

  • #1
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

Last edited:

Answers and Replies

  • #2
Quantum Defect
Homework Helper
Gold Member
495
116
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
DrClaude
Mentor
7,551
3,893
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
DrClaude
Mentor
7,551
3,893
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
Quantum Defect
Homework Helper
Gold Member
495
116
This does not implement the system of equations you have. Pay close attention to the time-dependence of the variables.
... I missed that!
 
  • #6
4
0
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

Related Threads on Euler method for modeling simple harmonic oscillation

  • Last Post
Replies
2
Views
3K
Replies
1
Views
2K
  • Last Post
Replies
5
Views
849
Replies
36
Views
15K
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
11
Views
2K
  • Last Post
Replies
15
Views
4K
  • Last Post
Replies
4
Views
648
  • Last Post
Replies
3
Views
2K
Top