Page 353 - MATLAB an introduction with applications
P. 353

338 ———  MATLAB: An Introduction with Applications

                   Solution: This is a single-degree of freedom system problem with all initial conditions zero. The following
                   MATLAB program is executed to obtain the results.
                   % INITIAL VALUES
                   m=2;k=1;c=0.3;dt=0.1;
                   x0=0;x0d=0;
                   F0=2;
                   T=5;
                   x0dd=inv(m)*(F0-c*x0d-k*x0);
                   xprev=x0-(dt*x0d)+((dt^2)*x0dd/2);
                   a0=1/dt^2;a1=1/(2*dt);a2=2*a0;
                   mbar=a0*m+a1*c;
                      t=0;
                      v(1)=x0d;a(1)=x0dd;
                      i=1;
                      for t=0:dt:T+dt
                      X(i)=x0;
                      f=F0*(1-sin(0.5*t));
                   fbar=f+(a2*m-k)*x0+(a1*c-a0*m)*xprev;
                      x=inv(mbar)*fbar;
                      xprev=x0;
                      x0=x;
                      i=i+1;
                      p=i;
                      end
                      for i=2:p-1
                         if i<p-1
                         v(i)=(X(i+1)-X(i-1))/(2*dt);
                         a(i)=(X(i+1)-2*X(i)+X(i-1))/dt^2;
                         end
                      end
                      fprintf(‘\ntime\t\tdisplacement\tvelocity\tacceleration\n’);
                      i=1;
                      for t=0:dt:T
                         fprintf(‘%f\t%f\t%f\t%f\n’,t,X(i),v(i),a(i));
                         i=i+1;
                      end
                      t=[0:dt:T+dt];
                      plot(t,X,‘-p’);
                   xlabel(‘time(s)’);
   348   349   350   351   352   353   354   355   356   357   358