AME 301: Differential Equations, Vibrations and Control

Sample C Code

This program numerically solves and graphs the solution of the differential equation
    x'' + 4x' + 2x = sin(2.45 t) with
    x(0) = 0, x'(0) = 1.11,
for t = 0 to 50.

It uses simple Euler integration.

To compile it on a unix machine, type:

    gcc sample.c -o sample -lm
To execute it, type:
, if necessary).

It should work in Windows as well, but you will have to remove the "system" functions and plot the data some other way.

The plot is stored in a file called "plot.eps". It is overwritten every time the program is executed.

Sample Mathematica Code

This solves the same equations as the previous example.

In Mathematica type:

soln = NDSolve[{x''[t] + 4 x'[t] + 2 x[t] == Sin[2.45 t], x[0] == 0, x'[0] == 1.11}, x, {t, 0, 50}];
Plot[Evaluate[x[t] /. soln], {t, 0, 50}]

Sample Matlab Code

Note: this code solves different equations than the C or Matlab code! The "Second file" contains the right hand side of the equations, so the equations are:
     y'(1) = -b/m*y(1)+y(2)/m
     y'(2) = lam*kp*(vd - y(1)) - lam*y(2)
Have these two files in your current directory or path. To integrate the system type 'car' (assuming the first file is named car.m). (It's called 'car' since it is a very simple model for a cruise control for a car). First file and second file.

Last modified: October 20, 2003.