%E5_13PN is a script to produce phase portraits,  using the mouse, 
% of the uncontrooled nonlinear system as well as a phase poratrait of the closed-loop system
% with the nonlinear plant model driven by an optimal controller constructed using
% a linear design plant. 
% In order to obtain a phase portrait of the controlled system, go to the file
% HW4NON and uncomment the equations of the closed-loop system
% and comment the open-loop system equations.
% Here, we construct the phase portrait of the uncontrolled nonlinear system.
%The result is Figure 5.14 of Example 5.13.

% last modified  by S. H. Zak on November 20, 2002

clear
clc

disp('                 ------------------------------------------------------------------')
disp('You are about to construct interactively a phase portrait')
disp('of the uncontrolled nonlinear system as well as a phase portrait of the closed-loop system')
disp('with the nonlinear plant model driven by an optimal controller constructed using')
disp('a linear design plant model.')
disp('In order to obtain a phase portrait of the controlled system, go to the file')
disp('HW4NON and uncomment the equations of the closed-loop system')
disp('and comment the open-loop system equations.')
disp('Here, we construct the phase portrait of the uncontrolled nonlinear system.')
disp('The result is Figure 5.14 of Example 5.13.')
disp('When ready to start constructing a phase portrait, press ENTER, maximize')
disp('the figure window, select an initial condition, press the LEFT BUTTON, and')
disp('repeat the process of selecting initial condtion. To stop, press the RIGHT BUTTON.')
pause

%t0=0;
%tf=20;
tspan=[0 20];
x0=[-.1 .1]';
button=1;
% we now draw internal axes
p=10*[-1 0;1 0];
axis([-10 10 -10 10])
hold on
axis square
plot([-10 10],[0 0])
plot([0 0],[-10 10])
xlabel('{\it{x}}_1','Fontsize',12)
ylabel('{\it{x}}_2','Fontsize',12)
title('Phase portait of a nonlinear system of Example 5.13.','Fontsize',12)
set(gca,'Fontsize',12)
    while(button==1)
       [t,x]=ode23s('hw4non',tspan,x0);
      plot(x(:,1),x(:,2))
      [x1,x2,button]=ginput(1);
      x0=[x1 x2]';
    end