Can't make this for loop work...

조회 수: 5 (최근 30일)
Carlen
Carlen 2015년 3월 22일
이동: DGM 2023년 9월 13일
I am trying to make this loop through so that I get a J1, J2, J3, and J4 matrix.
s1 = -.5773;
t1 = -.5773;
s2 = -.5773;
t2 = .5733;
s3 = .5773;
t3 = -.5773;
s4 = .5773;
t4 = .5773;
W1 = 1;
W2 = 1;
W3 = 1;
W4 = 1;
x1 = 3;
y1 = 2;
x2 = 5;
y2 = 2;
x3 = 5;
y3 = 5;
x4 = 3;
y4 = 4;
X_c = [x1 x2 x3 x4];
Y_c = [y1;y2;y3;y4];
for i = 1:4
J_i = (1/8)* X_c *[0 1-ti ti-si si-1; ...
ti-1 0 si+1 -si-ti; si-ti -si-1 0 ti+1; ...
1-si si+ti -ti-1 0] *Y_c
end
Sorry it has been a while since I have written any code and I am very inefficient.
  댓글 수: 1
Stephen23
Stephen23 2015년 3월 23일
편집: Stephen23 2023년 9월 12일
Do not use dynamically names variables in MATLAB. Put your variables in a cell array or structure instead.

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

답변 (1개)

dpb
dpb 2015년 3월 23일
"...this loop through so that I get a J1, J2, J3, and J4 matrix"
Don't do that! See the FAQ for why not and "more better" ways...
  댓글 수: 4
Carlen
Carlen 2015년 3월 23일
이동: DGM 2023년 9월 13일
Thanks for the reply. I am certainly a beginner to coding and some of this stuff is a bit over my head. It looks like I will have to do some more research on this and maybe get some external help.
Stephen23
Stephen23 2015년 3월 23일
이동: DGM 2023년 9월 13일
It is not clear why you need to define all of these values as separate variables. It would be much neater and simpler if you just did this:
W = [1,1,1,1];
x = [3,5,5,3];
y = [2,2,5,4];
... etc.
And then you could also use MATLABs very powerful code vectorization abilities to make the rest of your code neater and faster too.

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

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by