% newpendulum  -  function creates new view for animated pendulum

% (c) 2014, Philip D Loewen; history at end of file

function hlist = newpendulum(titletext)

g       = 9.80665;      % Standard gravity, in m/s^2, a little joke at 6 sig figs.
L       = g;
omega0  = sqrt(g/L);
periodT = 2*pi/omega0;

mainfig    = figure('Name',titletext);
annotation('textbox', [0 0.9 1 0.1], ...
    'String', titletext, ...
    'EdgeColor', 'none', ...
    'HorizontalAlignment', 'center');


phaseplane = subplot(3,3,[1 2 4 5 7 8]); hold on;
pendulum   = subplot(3,3,[3 6]); hold on;
energy     = subplot(3,3,[9]); hold on;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axes(phaseplane);
dot0 = plot(0,0,'k.','Visible','off');
axis equal;
set(gca,'XLim',1.6*pi*[-1,1],'YLim',1.6*pi*[-1,1]);
grid on;
hold on;
title('Phase Plane View of Pendulum State');
xlabel('Rod angle \theta, radians CCW from straight down');
ylabel('Angular velocity \omega=d\theta/dt, radians per second');

t = 0;
statedot = plot(0,0,'r.','MarkerSize',18);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axes(pendulum);
plot(0,0,'k.','MarkerSize',14);
axis equal;
set(gca,'XLim',1.1*L*[-1,1],'YLim',1.1*L*[-1,1]);
grid on;
hold on;

x =  0;
y = -L;
rod = plot([0,x],[0,y],'k','LineWidth',1);
rbob= 0.5;
tbob= linspace(0,2*pi,31);
bob = fill(x + rbob*cos(tbob),y + rbob*sin(tbob),'r');
title('Pendulum position');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axes(energy);

PEbar = fill([0,0,0,0],0.5*[0,0,1,1],'r');
hold on;
KEbar = fill([0,0,0,0],0.5*[0,0,1,1],'g');
set(gca,'YLim',[-1,3]/4);
Etot  = plot([0,0],get(gca,'YLim'),'c','LineWidth',2);
grid on;
xlabel('PE is red, KE is green, PE+KE is cyan');
title('Energy of point mass in pendulum bob');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hlist = {mainfig, ...
         phaseplane, pendulum, energy, ...
         statedot, ...
         rod, bob, ...
         PEbar, KEbar, Etot, ...
         L, rbob, g};
return;


% Version history
%  2014-11-05: First draft, named pendmovies.m
%  2014-11-06: Subplots of different sizes
%  2014-11-15: Global title using 'annotation'
