% ECE 594d - Winter 2010 - Homework 1 (edited 1/9/10, katiebyl)
% Underactuation and controllability. Cart-pole system.
%
% ------------- Comments -----------------------------
% To run the NONLINEAR case, (in part "b"), you should copy:
%    cartpole_linear.m
% to a new file called:
%    cartpole_nonlinear.m
% Then edit cartpole_nonlinear.m, so that it simulates the
% true (nonlinear) dynamics of the cartpole system.
% These two functions will be used by "ode45" in MATLAB to simulate 
% linearized and nonlinear versions of the system dynamics.


%------------ Code starts here -------------------------
% Cart-pole dynamics.
% State variables, X, are:
%    X = [x; theta; dx/dt; dtheta;dt]

% Start system with some "initial condition", X0:
x0 = 0;             % in meters
theta0 = 15*pi/180; % in radians <-- value to change in part "c"
dx0 = 0;
dtheta0 = 0;
X0 = [x0; theta0; dx0; dtheta0];
Tfinal = 5; % simulate for 10 seconds...

% ---------- Simulate linear dynamics; plot results. -------
[tlin,Xlin] = ode45(@cartpole_linear,[0 Tfinal],X0);

figure(1); clf
subplot(211)
plot(tlin,Xlin(:,1),'b-'); hold on; grid on % plot theta(t)
xlabel('time (s)'); ylabel('x = X(1) (m)');
title('Cart position')

subplot(212)
plot(tlin,Xlin(:,2),'b-'); hold on; grid on % plot theta(t)
xlabel('time(s)'); ylabel('\theta = X(2) (radians)');
title('Pendulum position')

orient tall % to use full page when printing out the figure

