Why the I is always a constant

1 回表示 (過去 30 日間)
Zhiying Wang
Zhiying Wang 2019 年 9 月 29 日
回答済み: Walter Roberson 2019 年 9 月 29 日
I=zeros(1,1);
f=zeros(1,1);
for ky=-6:1:6
for kx=-6:1:6
for n=0:1:2
for m=0:1:2
if n==0&&m==0
f(ky+7,kx+7)=1
else
f(ky+7,kx+7)=f(ky+7,kx+7)+exp(i*10^(-16)*(2.16*(n+m)*kx+1.245*(n-m)*ky))
end
end
I(ky+7,kx+7)=abs(f(ky+7,kx+7)).^2
end
end
end
contourf(-6:1:6,-6:1:6,I,'LineStyle','none');
Why the I doesnot change with kx,ky?
  2 件のコメント
Adam Danz
Adam Danz 2019 年 9 月 29 日
編集済み: Adam Danz 2019 年 9 月 29 日
I formatted your code but more importantly, use smart indentation to make your code more readable and easy to manage. To smart-indent, select all of your code from within the editor (ctrl + a) and then press ctrl + i.
Adam Danz
Adam Danz 2019 年 9 月 29 日
In this part of your code, "i" is the imaginary number sqrt(-1). I have a feeling that variable is supposed to represent something else. When 'i' is not defined, matlab uses it to represent the imaginary unit.
exp(i*10^(-16)*(2.16*(n+m)*kx+1.245*(n-m)*ky))
% ^ here

サインインしてコメントする。

回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 9 月 29 日
編集済み: KALYAN ACHARJYA 2019 年 9 月 29 日
Befor answering the question, It seems you are messing with code and logic, if yes, figure out.
Now the answer of the question
Why the I is always a constant?
In each iterartion (except one) your code fulfil the "if else" condition and assigned the value of
f(ky+7,kx+7)=f(ky+7,kx+7)+exp(i*10^(-16)*(2.16*(n+m)*kx+1.245*(n-m)*ky))
The "m for loop" have no role to change the I elements value, it pass the last iterration value. Each iteration when the code run with I assignment, it carries the same f(ky+7,kx+7) value and pass the same to I
>> f(ky+7,kx+7)
ans =
9.0000 + 0.0000i
Hence
I(ky+7,kx+7)=abs(f(ky+7,kx+7)).^2
How 81?
>> f(ky+7,kx+7)
ans =
9.0000 + 0.0000i
>> ans^2
ans =
81.0000 + 0.0000i
>> abs(ans)
ans =
81
Here the issue is not with I, please ensure that f(ky+7,kx+7) is changing during iterations, hence "I" will get different elements as a result.
Good Luck!
  1 件のコメント
Zhiying Wang
Zhiying Wang 2019 年 9 月 29 日
But the kx ky should change the I elements value, why I does not change?

サインインしてコメントする。


Walter Roberson
Walter Roberson 2019 年 9 月 29 日
>> unique(f(:))
ans =
9 + 0i
9 - 4.93038065763132e-32i
9 + 4.93038065763132e-32i
9 - 9.86076131526265e-32i
9 + 9.86076131526265e-32i
9 - 3.888e-15i
9 + 3.888e-15i
9 - 3.888e-15i
9 + 3.888e-15i
9 - 7.776e-15i
9 + 7.776e-15i
9 - 7.776e-15i
9 + 7.776e-15i
9 - 1.1664e-14i
9 + 1.1664e-14i
9 - 1.1664e-14i
9 + 1.1664e-14i
9 - 1.5552e-14i
9 + 1.5552e-14i
9 - 1.5552e-14i
9 + 1.5552e-14i
9 - 1.944e-14i
9 + 1.944e-14i
9 - 1.944e-14i
9 + 1.944e-14i
9 - 2.3328e-14i
9 + 2.3328e-14i
9 - 2.3328e-14i
9 + 2.3328e-14i
Your f is changing -- in the imaginary component.
(2.16*(n+m)*kx+1.245*(n-m)*ky)
is not all that large compared to the 10^-16 that you are multiplying it by. Values that are on the order of 1E-16 * sqrt(-1) come out as 1 + small complex component when you exp() them. The complex components do not add up to much, so the real component (1) completely dominates. The result is that you get a matrix full of 9 + varying small complex component. When you take abs(f)^2 the 9^2 dominates and the approximately 1e-16 complex component squared does not contribute numerically to the calculation (too insignificant). Therefore I becomes constant, the non-constant part having underflowed out of the calculation.

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by