% Input the connectivity of the members
% Row number corresponds to the member number
% First column indicates joint number at one end
% Second column indicates joint number at the other end
M= [1 2;2 3; 3 4;2 4;2 5;4 5; 1 5];
% Vector definition for the angle is first column to second column
% Angles are measured from the x axis, so horizontal members are zero
% For example, member #2 goes from joint 1 to joint 3 so the angle is zero
v= [90; 90; -30; 0; -30; -90; 0];
% Reactions are listed by joint number in col 1 and angle in col 2
R= [1 90; 5 90; 5 0];
% Loads are listed by joint number in col 1, angle in col 2, 
% and magnitude in column #3
L= [2 180 100; 3 0 700];
% Number of rows and columns in the square matrix A, (where Ax=b)
n=2*max(M(:));
A=zeros(n,n);
b=zeros(n,1);
% Loop through all the joints (j) and make two equations 
% Sum of forces in the X = 0 and Sum of forces in the y =0
for j=1:n
    % Go through all the members (i) and include their coefficients
    % in each equation as appropriate
    for i=1:length(M)
        % If the member is connected at the joint listed in column 1
        % then we assume the angle listed for the member points outward
        if M(i,1)==j
            A(2*j-1,i)=A(2*j-1,i)+cos(v(i)*pi/180);
            A(2*j,i)=A(2*j,i)+sin(v(i)*pi/180);
        else
            % If the member is connected at the joint listed in column 2
            % then we assume the angle listed for the member points inward
            if M(i,2)==j
            A(2*j-1,i)=A(2*j-1,i)-cos(v(i)*pi/180);
            A(2*j,i)=A(2*j,i)-sin(v(i)*pi/180);
            end
        end
    end
    % Go through all the reaction forces and include their coefficients 
    % in each equation as appropriate
    for r=1:3
        if R(r,1)==j
            A(2*j-1,length(M)+r)=A(2*j-1,length(M)+r)+cos(R(r,2)*pi/180);
            A(2*j,length(M)+r)=A(2*j,length(M)+r)+sin(R(r,2)*pi/180);
        end
    end
    [rowsL colsL]=size(L);
    for r=1:rowsL
        if L(r,1)==j
            % The load coefficients are negative since they had to be moved
            % to the left hand side of the statics equation
            b(2*j-1)=-L(r,3)*cos(L(r,2)*pi/180);
            b(2*j)=-L(r,3)*sin(L(r,2)*pi/180);
        end
    end
end
% Once the system is set up properly, solving for the loads and reactions
% is a very simple expression
x=A\b