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

1 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

VBBV
VBBV 2022-11-5
Mt=sum(M);
May be by doing this change

更多回答(1 个)

Jan
Jan 2022-11-5
编辑:Jan 2022-11-6
% 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 .

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by