1
votes

Problem

I have to plot a beam/cantilever using Matlab. Where my inputs are:

  • Length of the beam
  • Position of the loads (input is a vector)
  • Forces of the load (input is a vector)
  • Whether is it a cantilever or not. Because I have different equations for calculating the displacement.

My Solution

I have come to an idea on how I can actually plot the cantilever, but I can not formulate it into a code in MATLAB. I have spent hours trying to write something on Matlab but I have gotten nowhere. (I am a novice to Matlab)

My solution is as follow: I have the formula for the displacement from starting position.

I can define a vector using loop for x coordinates until the given beam length. Hence, x=[0 ... L]

Then I want to define another vector where the difference is calculated (this is where I can't figure out Matlab)

y = [h, h - y(x1), h - y(x2), .... h - y(L)]

where h is the starting height, which I have thought to be defined as (y(x1) - y(L)) + 1, so that the graph then doesn't go into the negative axes. y(x) is the function which will calculate the displacement or fall of the beam.

Once that is done, then I can simply plot(x,y) and that would give me a graph of a shape of deflected beam for the given range from 0 to beam length. I have tested my theory on excel and it works as per the graph is concerned but I can not figure out implementation on Matlab.

My incomplete code

%Firstly we need the inputs

%Length of the beam
l = str2double(input('Insert the length of your beam: ', 's'));

%Now we need a vector for the positions of the load
a = [];
while 1
    a(end+1) = input('Input the coordinate for the position of your load: ');
    if length(a)>1; break; end
end

%Now we need a vector for the forces of the load
W = [];
while 1
    W(end+1) = input('Input the forces of your load: ');
    if length(W)>1; break; end
end

%
%
%
%Define the formula
y = ((W * (l - a) * x)/(6*E*I*l)) * (l^2 - x^2 - (l - a)^2);

%Where
E = 200*10^9;
I = 0.001;

%
%
%

%Now we try to plot
%Define a vector with the x values
vectx = [];
for i = 1:l
    vectx = [vectx i];
end

%Now I want to calculate displacement for each x value from vectx
vecty = [];
for i=1:l
    vecty=[10 - y(x(i)) i];
end

%Now I can plot all the information
plot(vectx, vecty)
hold on

%Now I plot the coordinate of the positions of the load
plot(load)
end

Really need some help/guidance. Would be truly grateful if someone can help me out or give me a hint :)

I have edited the question with further details

1
First of all, you should not use the function name length as a variable. Additionally you can define vectx directly by vectx=1:n (assuming that you changed the first line to n=str2double ...). When writting y(x1) do you mean the first entry of x or what exactly? - Irreducible
Is your question about the plot, or about calculating the beam deflection? - Zep
@Zep The question is about the plot, because I have the deflection formula. - MathCurious314
@Irreducible Yes exactly. Sorry for confusion. I have the vectx=[x1, x2, x3 .. L] and I want them to be used to calculate y =[h - y(x1), h - y(x2) ... h - y(L)]. - MathCurious314
The only thing I can not figure out is how can I use a loop to calculate the output from a function for multiple values of x? - MathCurious314

1 Answers

0
votes

There are several things that do not work in your example. For instance, parameters should be defined BEFORE you use them, so E and I should be defined before the deflection equation. And you should define x. I do not understand why you put your inputs whithin a while loop, if you stop it at length(a)>1;. You can remove the loop. You do not need a loop to calculate displacements, you can just use a substraction between vectors, like displacement = 10 - y. However, I do not understand what is H in your example; since your beam is initially at position 0, your displacement is just -y. Finally, your equation to calculate the deformed shape is wrong; it only accounts for the first part of the beam.

Here, try if this code works:

%Length of the beam
l = input('Insert the length of your beam: ');

%Now we need a vector for the positions of the load
a = input('Input the coordinate for the position of your load: ');

%Now we need a vector for the forces of the load
W = input('Input the forces of your load: ');


%Define the formula
E = 200*10^9;
I = 0.001;
% x Position along the beam
x = linspace(0,l,100);

b = l - a;
% Deflection before the load position
pos = x <= a;
y(pos) = ((W * b .* x(pos))/(6*E*I*l)) .* (l^2 - x(pos).^2 - b^2);

% Cantilever option
% y(pos) = W*x(pos).^2/(6*E*I).*(3*a-x(pos));


% Deflection after the load position
pos = x > a;
y(pos) = ((W * b )/(6*E*I*l)) .* (l/b*(x(pos)-a).^3 + (l^2 - b^2)*x(pos) - x(pos).^3);
% Cantilever option
% y(pos) = W*a^2/(6*E*I).*(3*x(pos)-a);

displacement = 10 - y;  % ???


% Plot beam
figure
plot(x , x .* 0 , 'k-')
hold on;

% Plot deflection
plot(x , y , '--')

% Plot load position
% Normalize arrow size as 1/10 of the beam length
quiver(a , 0 , 0 , sign(W) .* max(abs(y))/2)