I can't figure out why the S matrix is computed to have the same value for every element - 0.7654. All other matrices (A through E) are computed correctly.
format short
x=[-0.9239, -0.6533, 0, 0.6533, 0.9239, 0.6533, 0, -0.6533]; %yj
y=[0, 0.6533, 0.9239, 0.6533, 0, -0.6533, -0.9239, -0.6533]; %yi
X=[-0.9239, -0.9239, -0.3827, 0.3827, 0.9239, 0.9239, 0.3827, -0.3827]; %Xj
Xone=[-0.9239, -0.3827, 0.3827, 0.9239, 0.9239, 0.3827, -0.3827, -0.9239];%Xj+1
Y=[-0.3827, 0.3827, 0.9239, 0.9239, 0.3827, -0.3827, -0.9239, -0.9239]; %Yj
Yone=[0.3827, 0.9239, 0.9239, 0.3827, -0.3827, -0.9239, -0.9239, -0.3827]; %Yj+1
phi=pi/180*[90, 45, 0, 315, 270, 225, 180, 135]; %phi
for i=1:8
for j=1:8
A(i,j)=-(x(i)-X(j)).*cos(phi(j)) - (y(i)-Y(j)).*sin(phi(j));
B(i,j)=(x(i)-X(j)).^2 + (y(i)-Y(j)).^2;
C(i,j)=sin(phi(i) - phi(j));
D(i,j)=(y(i)-Y(j)).*cos(phi(i)) - (x(i)-X(j)).*sin(phi(i));
E(i,j)=(x(i)-X(j)).*sin(phi(j)) - (y(i)-Y(j)).*cos(phi(j));
S(i,j)=((Xone(j)-X(j))^2 + (Yone(j)-Y(j))^2)^0.5;
end
end

 채택된 답변

Matt Fig
Matt Fig 2011년 5월 8일

0 개 추천

The way you have defined S, it is independent of index i.
S(i,j) = ((Xone(j)-X(j))^2 + (Yone(j)-Y(j))^2)^0.5; % No i appears.
Therefore it will have the same value for every element of each column. Did you mean to have no dependence on index i?

댓글 수: 4

Jeffrey
Jeffrey 2011년 5월 8일
There is no i dependence. I don't know where to stick S.
What's weird is that I tried to use the command window all by itself to compute
((Xone(3)-X(3))^2+(Yone(3)-Y(3))^2)^0.5 and the answer was still 0.7654.
Thanks Matt for your help once again.
Matt Fig
Matt Fig 2011년 5월 8일
It isn't that weird when you look at the way the numbers match up in your arrays. Break it down step by step:
T = [((Xone-X).^2).' ((Yone-Y).^2).'] % Look at row sums...
T = sum(T,2)
sqrt(T)
Jeffrey
Jeffrey 2011년 5월 8일
Yup. Oops. Thanks Matt.
One quick question...would it be helpful to insert, say A=zeros(8,8) etc before the for loops?
Matt Fig
Matt Fig 2011년 5월 8일
You probably wouldn't notice the difference with such small arrays. But in general, this will make your code faster. This code can be vectorized with multiple calls to BSXFUN, but simply removing the inner FOR loop and dynamically pre-allocating the arrays would probably help speed-wise:
cosphi = cos(phi);
sinphi = sin(phi);
Sv = ((Xone-X).^2 + (Yone-Y).^2).^0.5;
for ii = 8:-1:1
A2(ii,:) = -(x(ii)-X).*cosphi - (y(ii)-Y).*sinphi;
B2(ii,:) = (x(ii)-X).^2 + (y(ii)-Y).^2;
C2(ii,:) = sin(phi(ii) - phi);
D2(ii,:) = (y(ii)-Y).*cos(phi(ii)) - (x(ii)-X).*sin(phi(ii));
E2(ii,:) = (x(ii)-X).*sinphi - (y(ii)-Y).*cosphi;
S2(ii,:) = Sv;
end

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2011년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by