CDP Simulator: Mass Hanging from a Spring

Introduction

The purpose of this example is to demonstrate a basic CDP Simulator project. The project simulates a mass hanging from a spring.

"Mass-spring Harmonic Oscillator"

Mass-spring Harmonic Oscillator (Svjo / © CC BY-SA 3.0)

Click here to go to the more detailed explanation of the example.

This example includes:

  • A library project containing one simulator component,
  • A system project containing a simulator application which uses the simulator component created in the library project.

Project Overview

Consider a mass hanging from a spring. The mathematical model is:

`dot x = v`

`dot v = - x k/m - v b/m`

The position `x` denotes the displacement from spring equilibrium and `v` is the speed of the mass hanging from the spring. Parameter `m` specifies the mass of the body, `k` is the spring constant and `b` the damping coefficient.

A differential equation like this can be evaluated using CDP Simulator.

void MassSpring::EvaluateDiffEquations(double t)
{
    x.ddt = v;
    v.ddt = -x * k/m - v * b/m;
}

How to Run the Example

To run the example from CDP Studio, open Welcome mode and find it under Examples. Next, in Configure mode right click on the library project and select Build, then right click on the system project and select Run & Connect. See the Running the Example Project tutorial for more information.

The Graph application will now visualize the damped oscillation of the mass. The oscillation is described by the state variable SpringSimulatorApp.MassSpring.x.

For further information see the CDP Simulator manual.