is it possible to make this code shorter?
이전 댓글 표시
I have coded something to calculate the needed spring stifness. now i want to make the code shorter because the code is a lot of copy past, only i dont know how/if it is possible to make it shorter. the code that i have made can you see here bellow, rad is an array.
r1=x(1)/2*cos(rad(1))
r2=x(2)/2*cos(rad(1)+rad(2))
r3=x(3)/2*cos(rad(1)+rad(2)+rad(3))
r4=x(4)/2*cos(rad(1)+rad(2)+rad(3)+rad(4))
r5=x(5)/2*cos(rad(1)+rad(2)+rad(3)+rad(4)+rad(5))
r6=x(6)/2*cos(rad(1)+rad(2)+rad(3)+rad(4)+rad(5)+rad(6))
r7=x(7)/2*cos(rad(1)+rad(2)+rad(3)+rad(4)+rad(5)+rad(6)+rad(7))
r21=2*r1+r2
r31=2*(r1+r2)+r3
r41=2*(r1+r2+r3)+r4
r51=2*(r1+r2+r3+r4)+r5
r61=2*(r1+r2+r3+r4+r5)+r6
r71=2*(r1+r2+r3+r4+r5+r6)+r7
mg1=Fz(1)*r1+Fz(2)*r21+Fz(3)*r31+Fz(4)*r41+Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg2=Fz(2)*r21+Fz(3)*r31+Fz(4)*r41+Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg3=Fz(3)*r31+Fz(4)*r41+Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg4=Fz(4)*r41+Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg5=Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg6=Fz(6)*r61+Fz(7)*r71
mg7=Fz(7)*r71
mg=[mg1 mg2 mg3 mg4 mg5 mg6 mg7]
c=-mg./rad
If some one can help me with it or has a link to a page where this is explaind i will be very happy.
댓글 수: 3
What is rad?
help rad
Davide Masiello
2022년 11월 9일
Fz is also not defined.
Please show all the info necessary to help.
luuk loijen
2022년 11월 9일
편집: luuk loijen
2022년 11월 9일
채택된 답변
추가 답변 (2개)
Steven Lord
2022년 11월 9일
0 개 추천
One function that will be of use to you is cumsum. This will let you avoid variables with numbered names like r1, r2, r3, etc. In general defining a numbered sequence of variables is discouraged.
David Hill
2022년 11월 9일
r=x/2.*cos(cumsum(rad));
R=[r(1),2*cumsum(r(1:6))+r(2:7)];
mg=flip(cumsum(flip(Fz.*R)));
c=-mg./rad;
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!