% Script to calculate the deflection (y) of a cantilever beam
% subject to a linearly decreasing load (W)
% A. Trani (Spring 2022)

% W = load at station x (N/m)
% Wo = maximum load at station x=0 (N/m)
% E = Modulus of elasticity (N/m-m)
% I = Moment of inertia (m-m-m-m)
% x = beam station = distance from datum point (wall) to any point on the
%       beam (m)
% l = beam length (m)
% y = beam deflection at any station (m)

% Deflection equation
% y = -Wo * x.^2 / (120*E * I * length_of_beam) .* (10 * length_of_beam^3 ...
%         - 10 * length_of_beam^2 .*x + 5 * length_of_beam .* x.^2 - x.^3); Deflection equation

% Beam properties 

Wo = 6000;                          % Applied load Newtons/m
I = 0.001;                             % Moment of intertia - meters to the fourth power
length_of_beam = 12;         % Beam length in meters
material = 'Steel';
% material = 'Aluminum';

% Let us add a branching statement (IF statement)

if strcmp(material,'Steel')            % We ask Matlab to do a string comparison between variable material and "Steel"
        E = 200e9;                           % N/m-m - value for Steel = 200e9
elseif strcmp(material,'Aluminum') 
        E = 70e9;                           % N/m-m - value for Aluminum = 70e9
else
        E = 100e9;                           % N/m-m - value for other material
end

% Create a vector x with beam stations equally spaced at intervals of 0.025
% meters

% x = linspace(0,length_of_beam,100);        % 100 points to the end of the beam
x = 0:0.025:length_of_beam;                        % vector of stations from the wall to the end of the beam

% Calculate deflection (y) of the beam at any point in the beam length

y = -Wo * x.^2 / (120*E * I * length_of_beam) .* (10 * length_of_beam^3 ...
    - 10 * length_of_beam^2 .*x + 5 * length_of_beam .* x.^2 - x.^3);

% Plot the deflection for each station

plot(x,y,'o-r')
xlabel('Station (meters)','fontsize',30)
ylabel('Deflection (meters)','fontsize',30)
grid

