% This is the cart-pole system used by Barto et. al. 
% It is understood that we can ignore the friction effects
%
% plant.m 
%
function xt1=gplant(xt,ft)
xt1=[0; 0; 0; 0];
% the paper says g=-9.8; but in Barto's program, 9.8 was used and so is here.
g=9.8; mc=1.0; mp=0.1; m=mc+mp; l=0.5; TAU=0.02; four3rd=4/3; FORCE_MAG=10;

theta=xt(1);		theta_dot=xt(2);
x=xt(3);				x_dot=xt(4);
force=FORCE_MAG*sign(ft-0.5); 
costheta=cos(theta); sintheta=sin(theta);
temp = (force + mp*l*theta_dot^2*sintheta)/m;
thetaacc=(g*sintheta - costheta*temp)/(l*(four3rd - mp*costheta^2/m));
xacc = temp - mp*l*thetaacc*costheta/m;

% update the four states
x1=x+TAU*x_dot;
x1_dot=x_dot+TAU*xacc;
theta1=theta+TAU*theta_dot;
theta1_dot=theta_dot+TAU*thetaacc;
xt1=[theta1; theta1_dot; x1; x1_dot];
