Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Include Page

...

MATLAB Google Analytics

...

MATLAB Google Analytics

Widget Connector
width600
urlhttps://www.youtube.com/watch?v=uXURYBVEv-c
height370
Widget Connector
width600
urlhttps://www.youtube.com/watch?v=IyRJmOd48NE
height370

The following video shows how to implement the Euler's integration method for one time-step.

Widget Connector
width600
urlhttps://www.youtube.com/watch?v=3khcp_ZjnaU
height370

We extend our script to perform the Euler integration over multiple time-steps by looping over the appropriate statements as shown in the video below.

Widget Connector
width600
urlhttps://www.youtube.com/watch?v=OaGJsamM8rw
height370

Our script saves only the final location of the mass. We are also interested in saving the intermediate locations so that we can make a plot of position vs. time. We do this by creating arrays for the position and time as shown in the video below.

Include Page
Spring-Mass System - Panel
Spring-Mass System - Panel

 

Note

If you get a "LaTex markup" error on this page, please reload the page to see the equations that use the Latex markup.


If you have never used MATLAB before, we recommend watching some of these videos from The MathWorks , in particular the Getting Started video. . You can go through the videos either before or after completing this tutorial.

Spring-Mass Harmonic Oscillator in MATLAB

Created using MATLAB R2013a

Problem Specification

Consider a spring-mass system shown in the figure below.



Image Added

Applying F = ma in the x-direction, we get the following differential equation for the location x(t) of the center of the mass:

Latex
\[
m \ddot{x} + k x =0
\]

The initial conditions at t=0 are

Latex
\[
x(0)=1,
\]

and

Latex
\[
v(0)=\dot{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. Some other topics covered in this tutorial are:

  • Making a plot of mass position vs. time and comparing it to the analytical solution
  • Separating out the Euler's method in a MATLAB "function"
  • Collecting multiple parameters in one box using "structures"

In the process, you'll be exposed to the following handy MATLAB utilities:

  • Debugger to understand and step through code
  • Code analyzer to check code
  • Profiler to time code


Go to Step 1: Euler Integration

Go to all MATLAB Learning Modules

...