function plotTruss(Yr,bars,L,N,H)
% plotTruss(Yr,bars,L,N,H)
% Copyright 2015 The MathWorks, Inc.

% Make sure five inputs are passed in
narginchk(5,5)

% Displacements of nodes at each time step
dispX = Yr(:,1:2:end); % odd columns are x displacements
dispY = Yr(:,2:2:end); % even columns are y displacements

% Initial node locations
topCord = [(0:N)*L/N; zeros(1,N+1)]; % [x; y] coordinates of top cord
botCord = [(0:N-1)*L/N; ones(1,N)*(-H)]; % [x; y] coordinates of bottom cord
xyBase = [topCord,botCord]; % The truss is defined from bottom left corner counter clockwise

% Base locations
xyStartBase = xyBase(:,bars(:,5));
xyFinishBase = xyBase(:,bars(:,6));

% Figure out how many time steps to show
tsteps = size(dispX,1);
skpr = max(1,floor(tsteps/300));

% Loop over time and draw the deformed truss at each time step
for ii=1:skpr:tsteps
    % Where are the two ends of each truss element?
    xyStart = xyStartBase + [dispX(ii,bars(:,5)); dispY(ii,bars(:,5))];
    xyFinish = xyFinishBase + [dispX(ii,bars(:,6)); dispY(ii,bars(:,6))];
    
    % Plotting
    if ii == 1
        % First iteration, build the figure and line
        figure
        h = plot([xyStart(1,:); xyFinish(1,:)],[xyStart(2,:); xyFinish(2,:)],'linewidth',1.5);
        
        % Adjust the axes and title
        axis([0, 1.2*L, -2*H, H])
        set(gca,'XTick',[])
        set(gca,'YTick',[])
        title('Truss')
        
    elseif ishandle(h(1))
        % Otherwise update its Data
        for jj = 1:numel(h)
            % Update each line's data
            set(h(jj),'XData',[xyStart(1,jj) xyFinish(1,jj)]);
            set(h(jj),'YData',[xyStart(2,jj) xyFinish(2,jj)]);
        end
        
    else
        % Figure closed interactively
        break
        
    end
    
    % Force update and slow it down for animation
    pause(0.025)
    
end

end

