Spring-Mass Harmonic Oscillator in MATLAB
Consider a spring-mass system shown in the figure below.
Applying F = ma in the x-direction, we get the following differential equation for the location x(t) of the center of the mass:
Unknown macro: {latex}
[
m \ddot
Unknown macro: {x}
+ k x =0
]
The initial conditions at t=0 are
Unknown macro: {latex}
[
x(0)=1,
]
and
Unknown macro: {latex}
[
v(0)=\dot
Unknown macro: {x}
̇(0)=0
]
The first condition above specifies the initial location x(0) and the second condition, the initial velocity v(0).
We'll solve this differential equation numerically, i.e. integrate it in time starting from the initial conditions at t=0, using MATLAB. We'll use Euler's method to perform the numerical integration. We'll also make a plot of the position vs. time and compare it to the analytical solution.