've written in Maple to set up and solve the differential equation for a simple harmonic oscillator and plot the solution. To incorporate the Hamiltonian into my worksheet, I needed to define the kinetic energy and potential energy of the system, and then combine them to express the total Hamiltonian. After that, I can calculate and plot the kinetic, potential, and total energies over time. Then I plotted the kinetic energy, potential energy, and total energy over time
Hamiltonian Energy
In a linear simple harmonic oscillator (SHO), the Hamiltonian energy represents the total energy of the system, which is a combination of its kinetic and potential energy.
1. |
Position and Momentum:
|
• |
Let be the displacement of the oscillator from its equilibrium position.
|
• |
Let be the momentum of the oscillator.
|
2. |
Hamiltonian ( ):
|
• |
For a linear SHO, it is given by:

|
Kinetic energy depends on the mass of the object and its velocity. In physics, it's expressed as:

where:
• |
is the mass of the oscillator,
|
• |
*of*momentum, p = mv, be*can*energy*kinetic*re*the-as*expressed; T = p^2/(2*m)](/ViewTemp.ashx?f=4065368_1729814832/f1e5f6b89abfb1ec755bbab43b66819f.gif)
This equation shows that kinetic energy increases with the square of the momentum.
|
4. |
Potential Energy ( )
|
Potential energy in a simple harmonic oscillator arises from the restoring force of the spring. The potential energy is given by:


• |
is the spring constant, and
|
• |
is the displacement from the equilibrium position.
|
This means that the further the mass is displaced from equilibrium, the greater the potential energy stored in the system.
> |

|
> |

|

|
(1) |
> |

|

|
(2) |
> |

|

|
(3) |
> |
![plot_1 := subs(A = 1, sol); plotsresult := plot([rhs(plot_1)], t = 0 .. 2, legend = ["0.1cos(10 t)"], color = [red])](/ViewTemp.ashx?f=4065368_1729814832/26b5f696fdd18e8a60b9dfa9836ecc88.gif)
|
> |
![directionfield := DEplot([diff(x(t), t) = v(t), diff(v(t), t) = -omega^2*x(t)], [x(t), v(t)], t = 0 .. 10, x = -2 .. 2, v = -10 .. 10, arrows = medium, title = "Direction Field for Simple Harmonic Oscillator", axes = boxed)](/ViewTemp.ashx?f=4065368_1729814832/38fb5870864b463f706ef9e2c3117fc2.gif)
|
> |

|

|
(4) |
> |

|

|
(5) |
> |
![phase_plot := plot([[eval(x1, t = tval), eval(v1, t = tval), tval = 0 .. 10], [eval(x2, t = tval), eval(v2, t = tval), tval = 0 .. 10], [eval(x3, t = tval), eval(v3, t = tval), tval = 0 .. 10], [eval(x4, t = tval), eval(v4, t = tval), tval = 0 .. 10]], style = line, title = "Phase Portrait for Simple Harmonic Oscillator", labels = ["x (Displacement)", "v (Velocity)"], color = ["red", "blue", "green", "orange"], legend = ["A=1, v0=0", "A=0.5, v0=0", "A=-1, v0=0", "A=0.7, v0=0.5"], axes = boxed)](/ViewTemp.ashx?f=4065368_1729814832/ed81a5b0a069bccc4b530113d9d85ace.gif)
|
> |

|
> |

|

|
(6) |
> |
![energy_plot := plot([eval(T), eval(V), eval(H)], t = 0 .. 2, color = [red, blue, green], legend = ["Kinetic Energy", "Potential Energy", "Total Energy"], title = "Energy Exchange in Simple Harmonic Oscillator", labels = ["Time (s)", "Energy (Joules)"])](/ViewTemp.ashx?f=4065368_1729814832/2a0e32ee6266d35d46bf67f0c8f0f183.gif)
|
> |

|
|
Download Hamiltonian_Energy.mw
Now, in the second worksheet, I tried to create a procedure to calculate the Hamiltonian Energy for a general example. I have set delta = 0
: This removes the damping effect, leaving only the spring constant k
and mass m
as factors. Now the equation represents a pure simple harmonic oscillator:
The Hamiltonian (total energy) should be conserved, meaning it will remain constant over time without damping. The plot of H(t)
should show a straight line, indicating that the kinetic and potential energy sum does not change. But as you can see the energy increases over time. I can not figured out where is my mistake in this code
Any guidance or examples of implementation would be greatly appreciated!
Thank you!
Download Hamiltoninan_procedure_.mw