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
VBBV
VBBV 2022년 11월 5일
편집: VBBV 2022년 11월 5일
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

댓글을 달려면 로그인하십시오.

추가 답변 (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