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
채택된 답변
추가 답변(1개)
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 .
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!