This example shows how MATLAB commands can be used to simulate a pendulum using the discrete-time approximation of the nonlinear model.
Matlab m-file example3.m:% % Filename: example3.m % % Description: This m-file demonstrates how matlab can be used to perform % the discrete-time simulation of the nonlinear pendulum model. % clear; clf; % clear matlab memory and figure m = 5.0; l = 0.5; g = 9.81; b = 1.3; % define system parameters x0 = [-0.2; 0.1]; % initial conditions tmax = 5; % maximum time in simulation xk = x0; T = 0.01; % define initial conditions & time step plot(0,xk(1),'o'); % plot initial condition hold; for k = 1:tmax/T, % perform 'disc-time' simulation Torque = sin(pi*k*T); xk(1) = xk(1) + T*xk(2); xk(2) = xk(2) + T/(m*l*l)*(Torque - b*xk(2) - m*g*l*sin(xk(1))); plot(k*T,xk(1),'o'); end; hold; xlabel('tMATLAB Plot Generated:'); ylabel('y[k] = theta[k]'); title('Pendulum Position for Sinusoidal Input'); grid;