% Calculates the reaction and shear diagram of the beam
% beam is cantilever on one side and supported on the other side
% A. Trani (October 11, 2013)

% Given the following quantities:

% W = 2000 lb
% a = 50 in
% b = 120 in
% L = a + b
% I = 120 in-in-in-in
% E = 29e6 lbf/in-in

W = 2000;               % load in pounds
a = 50;                    % left distance from load to single support (in)
b = 150;                 % right distance from load to cantilever section (in)
L = a + b;               % total beam length (in)
I = 50;                    % beam moment of inertia (in-in-in-in)
E = 29e6;               % Young's modulus (psi)

% Calculate the Reaction forces

R = W * (3*b^2*L-b^3) / (2*L^3);      % reaction at support
R1 = W * (3*a*L^2-a^3) / (2*L^3);     % reaction at wall

counter = 1;            % keeps track of the station count

% Start loop to compute Moments (M)

for x = 0:1:L
    if x < a
        Moment(counter) = W * x * (3*b^2*L - b^3) / (2*L^3);
        Deflection(counter) = -1/ (6*E*I) *(3*R *L^2 * x - R * x^3 - 3 *W *(L-a)^2 * x);
    else
        Moment(counter) = W * x * (3*b^2*L - b^3) / (2*L^3) - W*(x-a);
        Deflection(counter) = -1/ (6*E*I) *(R1 *(2*L^3 -3*L^2*x + x^3) - 3*W*a*(L-x)^2);
    end 
    distance(counter) = x;
    counter = counter + 1;
end

figure
plot(distance,Moment,'o--')
xlabel('Distance (in)','fontsize',24)
ylabel('Moment (lb-in)','fontsize',24)
grid

figure
plot(distance,Deflection,'o--')
xlabel('Distance (in)','fontsize',24)
ylabel('Deflection (in)','fontsize',24)
grid


