Use the ode 45 function to simulate the response to 3 \ddot{x}+\dot{x}+2 x=0 subject to the initial conditions x(0)=0, \dot{x}(0)=0.25 over the time interval 0 \leq t \leq 20.
The first step is to write the equation of motion in first-order form. This yields
\begin{aligned} & \dot{x}_1=x_2 \\ & \dot{x}_2=-\frac{2}{3} x_1-\frac{1}{3} x_2 \end{aligned}
Next, an M-file is created to store the equations of motion. An M-file is created by choosing a name, say, sdof.m, and entering
function xdot = sdof(t,x);
xdot = zeros(2,1);
xdot(1) = x(2);
xdot(2) = –(2/3)*x(1) – (1/3)*x(2);
Next, go to the command mode and enter
t0 = 0;tf = 20;
x0 = [0 0.25];
[t,x] = ode45(‘sdof’,[t0 tf],x0);
plot(t,x)
The first line establishes the initial, (t0), and final, (tf), times. The second line creates the vector containing the initial conditions x0. The third line creates the two vectors t, containing the time history, and x, containing the response at each time increment in t, by calling ode45 applied to the equations set up in sdof. The fourth line plots the vector x versus the vector t. This is illustrated in Figure 1.42.