Code for graphing bending moment is giving the wrong graph and I don't know why, can anyone help?

11 views (last 30 days)
clear;
range=[0 10];
nweights=3;
w=150;
Wk=[1500 -3000 2500];
p=[3 5 7];
L=max(range);
M=0;
y=0;
M0=0;
sumM=0;
for x=0:L
for k=1:1:nweights
if(x>=0) && (x<=p(k))
M=(Wk(k)*x*(1-(p(k)/L)));
else
if(x>p(k)) && (x<=L)
M=(Wk(k)*x*(1-(p(k)/L))) - (Wk(k)*(x-p(k)));
end
end
end
Mt=sumM;
M0=(0.5*w*x*(L-x));
y(x+1)=M0 + Mt;
end
v=0:L;
figure;
plot(v,y);
xlabel('distance x from A (in m)');
ylabel('Bending moment M (in Nm)');
title('A graph showing variation of the bending moment with distance from A');
  3 Comments

Sign in to comment.

Accepted Answer

VBBV
VBBV on 5 Nov 2022
Mt=sum(M);
May be by doing this change
  1 Comment
VBBV
VBBV on 5 Nov 2022
Edited: VBBV on 5 Nov 2022
if(x>=0) & (x<=p(k))
M(k)=(Wk(k)*x*(1-(p(k)/L)));
else
if(x>p(k)) & (x<=L)
M(k)=(Wk(k)*x*(1-(p(k)/L))) - (Wk(k)*(x-p(k)));
end
end
And this too

Sign in to comment.

More Answers (1)

Jan
Jan on 5 Nov 2022
Edited: Jan on 6 Nov 2022
% sumM=0; % not here
for x=0:L
sumM=0; % but here
for k = 1:nweights
if x<=p(k)
M = Wk(k) * x * (1 - p(k) / L);
else % if x>p(k)
M = Wk(k) * x * (1 - p(k) / L) - Wk(k) * (x - p(k));
end
% Maybe here:
sumM = sumM + M;
end
Mt = sumM;
M0 = 0.5 * w * x * (L-x);
y(x+1) = M0 + Mt;
end
I've reduced the number of parentheses. Overdoing is not useful.
Checking for x >= 0 and x <= L is a waste of time in a loop over x=0:L .

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!