%% Truss stablity analysis: playground code. 
% Alex Townsend, April 2015 

%% Unstable treehouse: 
Locations = [ 0 0 ; 2 0 ; 1 1 ; 0 -1 ; 2 -1]; 
bar = [ 1 2; 2 3 ; 3 1 ; 1 4 ; 2 5 ]; 
support = [ 4 5 ]; 
A = constructA( Locations, support, bar );
plottruss( Locations, support, bar ) 

%% Is it stable: 
plotfall( A, Locations, support, bar ) 

%% Put in a truss: 
Locations = [ 0 0 ; 2 0 ; 1 1 ; 0 -1 ; 2 -1]; 
bar = [ 1 2; 2 3 ; 3 1 ; 1 4 ; 2 5 ; 4 2 ]; 
support = [ 4 5 ]; 
A = constructA( Locations, support, bar ); 
plotfall( A, Locations, support, bar ) 

%% Hexagon: 
clear all
r = exp( 1i*linspace(0,2*pi,7) ); r = r(1:end-1)'; 
Locations(:,1) = real(r); Locations(:,2) = imag(r); 
%Locations = [ 0 0 ; 1 0 ; 2 1 ; 1 2 ;  -1 ; 3 1 ; 3 0]; 
bar = [ 1 2; 2 3 ; 3 4 ; 4 5 ; 5 6 ; 6 1]; 
support = [  ]; 
plottruss( Locations, support, bar) 

%% Unstable modes:
A = constructA( Locations, support, bar ); 
plotfall( A, Locations, support, bar), shg

%% Add a few supports: 
support = [ 2 3 ]; 
plottruss( Locations, support, bar) 
A = constructA( Locations, support, bar );
plottruss( Locations, support, bar) 

%% 
plotfall( A, Locations, support, bar), shg

%% Tree House: 
Locations = [ 0 0 ; 2 0 ; 2 1 ; 0 -1 ; 2 -1 ; 3 1 ; 3 0]; 
bar = [ 1 2; 2 3 ; 3 1 ; 1 4 ; 2 5 ; 6 7 ; 3 6 ; 2 7]; 
support = [ 4 5 ]; 
A = constructA( Locations, support, bar ); 
plotfall( A, Locations, support, bar ), shg

%% Tree House with strengthing membrane: 
Locations = [ 0 0 ; 2 0 ; 2 1 ; 0 -1 ; 2 -1 ; 3 1 ; 3 0]; 
bar = [ 1 2; 2 3 ; 3 1 ; 1 4 ; 2 5 ; 6 7 ; 3 6 ; 2 7 ; 5 7]; 
support = [ 4 5 ]; 
A = constructA( Locations, support, bar ); 
plotfall( A, Locations, support, bar ), shg


%% Unstable treehouse: 
Locations = [ 0 0 ; 2 0 ; 1 1 ; 0 -1 ; 2 -1]; 
bar = [ 1 2; 2 3 ; 3 1 ; 1 4 ; 2 5 ]; 
support = [ 4 5 ]; 
A = constructA( Locations, support, bar );
plottruss( Locations, support, bar ) 

C = eye ( size(A, 1) );

B = A' * C * A; 

%% Triangle: 
Locations = [ 0 0 ; 1 0 ; 1/2 sqrt(3)/2]; 
bar = [ 1 2; 2 3 ; 3 1 ]; 
support = [ 1 2 ]; 
A = constructA( Locations, support, bar );
plottruss( Locations, support, bar ) 
axis equal 
C = eye ( size(A, 1) );

B = A' * C * A;
[U, S, V] = svd( B ); 

%% Stiffer columns: 
Locations = [ 0 0 ; 2 0 ; 0 1 ; 2 1 ; 1 2]; 
bar = [ 1 3; 3 5 ; 2 4 ; 4 5 ]; 
support = [ 1 2 ]; 
A = constructA( Locations, support, bar );
plottruss( Locations, support, bar ) 
axis equal 
C = eye ( size(A, 1) );

%% Stiffer columns, triangle roof: 
Locations = [ 0 0 ; 2 0 ; 0 1 ; 2 1 ; 1 2]; 
bar = [ 1 3; 3 5 ; 2 4 ; 4 5 ; 1 4 ; 2 3]; 
support = [ 1 2 ]; 
A = constructA( Locations, support, bar );
plottruss( Locations, support, bar ) 
axis equal 
C = eye ( size(A, 1) );
%C(5,5) = 1e-2; C(6,6) = 1e-2; 
%C(1,1) = 2;  C(2,2) = 2; 
%C(3,3) = 2;  C(4,4) = 2; 

B = A' * C * A;
[U, S, V] = svd( B ); 
diag( S )


%% Stiffer columns, different type of roof: 
Locations = [ 0 0 ; 2 0 ; 0 1 ; 2 1 ; 1 1.4]; 
bar = [ 1 3; 3 5 ; 2 4 ; 4 5 ; 1 4 ; 2 3]; 
support = [ 1 2 ]; 
A = constructA( Locations, support, bar );
plottruss( Locations, support, bar ) 
axis equal 
C = eye ( size(A, 1) );
C(5,5) = 1e-2; C(6,6) = 1e-2; 
%C(1,1) = 2;  C(2,2) = 2; 
C(3,3) = 2;  C(4,4) = 2; 

B = A' * C * A;
[U, S, V] = svd( B ); 
diag( S )

%% Stiffer columns wider base: 
Locations = [ -.3 0 ; 2.3 0 ; 0 1 ; 2 1 ; 1 1.4]; 
bar = [ 1 3; 3 5 ; 2 4 ; 4 5 ; 1 4 ; 2 3]; 
support = [ 1 2 ]; 
A = constructA( Locations, support, bar );
plottruss( Locations, support, bar ) 
axis equal 
C = eye ( size(A, 1) );
C(5,5) = 1e-2; C(6,6) = 1e-2; 
%C(1,1) = 2;  C(2,2) = 2; 
C(3,3) = 2;  C(4,4) = 2; 

B = A' * C * A;
[U, S, V] = svd( B ); 
diag( S )
