##### Sections
You are here: Home Beam Deflection via Superposition

# Beam Deflection via Superposition

The following files demonstrate one way to make a generic beam deflection program. Examples of rich data structures to describe various types of loads are shown.

All the following Matlab code files are stored in a single directory.

superpos2.m (main program):

```  % Superposition example using Matlab structures

% Define beam properties (not the best use of data structures)

beam.E=30e6; % psi
beam.L=80;   % in
beam.I=4580; % in^4

% Define loads (better use of data structures -- we potentially have
% several loads that have common traits like positions and magnitudes,
% and we want to apply them to the beam in similar ways).

loads(1).mag=10000; % in-pound (counterclockwise moments are positive)
loads(1).position=35; % in from left end

loads(2).position=40; % in from left end

superposition_calculate;

figure
plot(x1,-v_total,'bo-');
xlabel('Position (in)')
ylabel('Deflection (in)')
title('Superposition Example')
```

superposition_calculate.m (calculate deflection curve for beam):

```  % superposition_calculate.m

% Most of this code was in superposition.m before, but has been split off
% from the calling script to illustrate how you can reduce the student or
% end user's exposure to the internals of the code. All they have to worry
% about is the syntax for defining their beam geometry and loads.

equations(1).type='point';
equations(1).deflection='((P*b*x)/(6*E*I*L)).*(L^2-b^2-x.^2)';
equations(2).type='moment';
equations(2).deflection='((M*x)/(6*E*I*L)).*(6*a*L-3*a^2-2*L^2-x.^2)';

x1=linspace(0,beam.L,20);
v_total=zeros(size(x1));

E=beam.E;
I=beam.I;
L=beam.L;

case 'point'
b=L-a;
x=x1(find(x1<=a));
whichequation=find(strcmp({equations.type},'point'));
v1=eval(equations(whichequation).deflection);
x=L-x1(find(x1>a));
b=a;
v2=eval(equations(whichequation).deflection);
v=[v1 v2];
v_total=v_total+v;

case 'moment'
b=L-a;
x=x1(find(x1<=a));
whichequation=find(strcmp({equations.type},'moment'));
v1=eval(equations(whichequation).deflection);
x=L-x1(find(x1>a));
a=b;
v2=eval(equations(whichequation).deflection);
v=[v1 -v2];
v_total=v_total+v;

otherwise

end
end
```

Results:

```  >> superpos2
>>
```

Generated graph: Filed under: 