Reputation: 19
This code is meant to calculate the deflection of a beam with the principal of superposition, where for every given position og a beam, all the individual influences of forces are calculated, and then summed together.
function deflection = beamSuperposition(positions, beamLength,loadPositions, loadForces, beamSupport)
%If several loads are applied, calculate each one individually and end with sum
x = positions;
l = beamLength;
a = loadPositions;
W = loadForces;
E = 200*10^9;
I = 0.001;
%For every position(x) element calculate the expression for all
%loadPositions(a) and their respective loadForce(W)
%Make sure a and W are of same size
%Make sure neither x or a ever surpass l
%Go trhough each element of x
for i = 1:length(x)
%For beamSupport = 1 only
while beamSupport == 1
%Go through each element of 'a' and calculate the deflection
for n = 1:length(a)
%There are two different equations to calculating the deflection depending on the position of the loadForce compared to the position of interest
while true
if x(i) < a(n)
y = (W(n)*(l-a(n))*x(i))*(l^2-x(i)^2-(l-a(n))^2)/(6*E*I*l);
end
if x(i) >= a(i)
y = (W(n)*a(n)*(l-x(i)))*(l^2-(l-x(i))^2-a(n)^2)/(6*E*I*l);
end
break
end
end
break
end
%Sum the values for each y element?!?
deflection = y
end
How can I make this work as intended? The output should be a vector y
with the same size as x
with the summed deflections for each x
value.
In use;
beamSuperposition([1,2,5],10,[6,5,3],[10,15,20],1)
Will return, if deflection = y
is not supressed,
deflection =
5.8333e-07
deflection =
1.0967e-06
deflection =
1.6500e-06
ans =
1.6500e-06
But should return values as a vector instead of the last value only
Upvotes: 1
Views: 105
Reputation: 1587
I've made some small changes. Firstly I've changed the central two if
statements to a single if else
statement. This removed an error you had
if x(i) >= a(i)
which should have probably read
if x(i) >= a(n)
Whilst there I also removed some of your while break
control flows which I didn't quite see the purpose of. You might want to compare.
Secondly I saved your outputs to the i
-th entry of y
. I also preallocated it so it doesn't change size in the loop.
Finally, as suggested by @StackPlayer I used ii
for the loop variable instead of i
. There is some discussion on the question Using i and j as variables in Matlab.
function deflection = beamSuperposition(positions, beamLength,loadPositions, loadForces, beamSupport)
%If several loads are applied, calculate each one individually and end with sum
x = positions;
l = beamLength;
a = loadPositions;
W = loadForces;
E = 200*10^9;
I = 0.001;
%For every position(x) element calculate the expression for all
%loadPositions(a) and their respective loadForce(W)
%Make sure a and W are of same size
%Make sure neither x or a ever surpass l
y = zeros(size(x));
%Go through each element of x
for ii = 1:length(x)
%For beamSupport = 1 only
if ( beamSupport == 1 )
%Go through each element of 'a' and calculate the deflection
for n = 1:length(a)
%There are two different equations to calculating the deflection depending on the position of the loadForce compared to the position of interest
if x(ii) < a(n)
y(ii) = (W(n)*(l-a(n))*x(ii))*(l^2-x(ii)^2-(l-a(n))^2)/(6*E*I*l);
else
y(ii) = (W(n)*a(n)*(l-x(ii)))*(l^2-(l-x(ii))^2-a(n)^2)/(6*E*I*l);
end
end
end
end
%Sum the values for each y element?!?
deflection = y;
end
Example use:
>> beamSuperposition([1,2,5],10,[6,5,3],[10,15,20],1)
ans =
1.0e-05 *
0.0583 0.1097 0.1650
Upvotes: 1
Reputation: 1490
First things first, avoid using i and j in MATLAB (they are used for the imaginary number).
In your second while loop, you have if conditions on "x" instead of "x(i)"
Upvotes: 0