npm.nicfv.com
    Preparing search index...

    Mass Spring Damper

    The differential equation for a system containing a mass, spring, and damper is:

    $$m\ddot{x} + b\dot{x} + kx = F(t)$$

    To use the DiffyQ library, we have to solve for the highest order derivative, which in this case is \(\ddot{x}\).

    $$\ddot{x} = \frac{1}{m} \left[ F(t) - b\dot{x} - kx \right]$$

    Since this is a 1-dimensional differential equation, we can create the simple DifferentialEquation class. It requires an equation and initial conditions.

    Since the highest order derivative is \(\ddot{x}\), that means this is a 2nd-order differential equation. Our initial conditions must contain \(x_{0}\) and \(\dot{x}_{0}\). \(\ddot{x}_{0}\) is not required because it will be calculated using the equation that we just solved for!

    In this example, we solve the differential equation for the first 10 seconds with a timestep of 0.01 seconds and print out some of the results.

    Follow these steps to create a new project workspace and install the smath dependency to run this example.

    # Create and open project folder
    mkdir Mass_Spring_Damper_demo
    cd Mass_Spring_Damper_demo
    # Initialize project and install dependencies
    npm init -y
    npm i smath@4.0.2
    # Create and open source file
    touch "Mass Spring Damper.mjs"
    open "Mass Spring Damper.mjs"

    Copy and paste this source code into Mass Spring Damper.mjs.

    import { DiffyQ } from 'smath';

    // System properties
    const m = 10;
    const b = 1;
    const k = 1;

    // Initial conditions at `t=0`
    const x0 = 0;
    const dx0 = 0;

    // Applied external force
    function F(t) {
    return Math.sin(t);
    }

    // Differential equation, solved for ddx
    function ddx(t, x, dx) {
    return 1 / m * (F(t) - b * dx - k * x);
    }

    // Create and solve the differential equation
    const mass_spring_damper = new DiffyQ.DifferentialEquation(ddx, x0, dx0);
    const data = mass_spring_damper.solve(0.01, 10);

    // Print out results, dx[0] is the "x" position, dx[1] is velocity,
    for (let i = 0; i < data.length; i += 100) {
    console.log(`t=${data[i].time}, x=${data[i].dx[0]}, dx=${data[i].dx[1]}, ddx=${data[i].dx[2]}`);
    }

    In Mass_Spring_Damper_demo/, execute Mass Spring Damper.mjs with NodeJS to generate an output.

    node "Mass Spring Damper.mjs"
    

    You should expect to see an output similar to the one below.

    t=0, x=0, dx=0, ddx=0
    t=1.0000000000000007, x=0.015186844885342202, dx=0.04373039131003868, ddx=0.07837666773996807
    t=2.0000000000000013, x=0.10080641175005328, dx=0.12592955960413474, ddx=0.06845078674686625
    t=2.99999999999998, x=0.2477580680140727, dx=0.15205966043329788, ddx=-0.025742213283638616
    t=3.9999999999999587, x=0.36944734648014627, dx=0.0752668578057819, ddx=-0.12019538506820034
    t=4.999999999999938, x=0.3787950293078895, dx=-0.058019203276057696, ddx=-0.1281560062029251
    t=5.9999999999999165, x=0.26817911008944284, dx=-0.14836022058230333, ddx=-0.040112854438116734
    t=6.999999999999895, x=0.11858890919692892, dx=-0.13264768568392585, ddx=0.06703777111192773
    t=7.999999999999874, x=0.029159345786451664, dx=-0.040533975209987555, ddx=0.10013258358587432
    t=8.999999999999853, x=0.031319671146589644, dx=0.033822386396621516, ddx=0.03476703251568479
    t=9.999999999999831, x=0.06586546768830946, dx=0.018682337471078895, ddx=-0.06289999867214363