IF statement wrongly checked

1 回表示 (過去 30 日間)
Nicola Caldognetto
Nicola Caldognetto 2021 年 4 月 7 日
編集済み: Khalid Mahmood 2021 年 4 月 7 日
I have wrote a simple matlab script that calculate a function given 2 variables and tell me when it's smaller than a given value and save the two initial variables that have triggered the IF statements in a matrix; code below
clear all
close all
f = 17.5*10^9;
a = 3*10^-2;
b = 2*10^-2;
c = 3*10^8;
j = 1;
k = 0;
modi = zeros(2,25);
for m = 1:1:5
for n = 1:1:5
f_mn = (c/2)*(sqrt(((m/a)^2)+((n/b)^2)));
if f_mn < f
modi(1,j+k)=n;
modi(2,j+k)=m;
j+1;
end
end
k+1;
end
I don't know if I'm dumb or I'm missing something but, the first iteration of the code (m=1 and n=1) should produced a f_mn=9.01e+09 which is definetly smaller than f=1.75e+10 but the code doesn't enter the IF statement...

採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 7 日
j+1;
That line retrieves the current value of j and invokes the plus() method between j and 1 (whatever that happens to be for the datatype of j), assigns the result to the ans variable, and then discards the result otherwise, since the semi-colon says not to display the result.
That line does not increase j by 1, as it does not assign to j.
  2 件のコメント
Nicola Caldognetto
Nicola Caldognetto 2021 年 4 月 7 日
ok, that was the error but I don't get why, at least on the first iteration, it doesn't enter the IF statement
Walter Roberson
Walter Roberson 2021 年 4 月 7 日
It did enter the if body.
clear all
close all
f = 17.5*10^9;
a = 3*10^-2;
b = 2*10^-2;
c = 3*10^8;
j = 1;
k = 0;
modi = zeros(2,25);
for m = 1:1:5
for n = 1:1:5
f_mn = (c/2)*(sqrt(((m/a)^2)+((n/b)^2)));
if f_mn < f
fprintf('if entered m = %d, n = %d\n', m, n);
modi(1,j+k)=n;
modi(2,j+k)=m;
j+1;
end
end
k+1;
end
if entered m = 1, n = 1 if entered m = 1, n = 2 if entered m = 2, n = 1 if entered m = 3, n = 1

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

その他の回答 (1 件)

Khalid Mahmood
Khalid Mahmood 2021 年 4 月 7 日
編集済み: Khalid Mahmood 2021 年 4 月 7 日
clear all
%close all, %no need to close all, as nothing was opened by this code.. no figure, port etc
f = 17.5*10^9; %same as f=1.75e+10
a = 3*10^-2; %same as a=3e-2
b = 2*10^-2; %same as b=2e-2;
c = 3*10^8; %same as c=3e8
j = 1;
k = 0;
i=1; %for saving values of successive f_mn
modi = zeros(2,25);
for m = 1:1:5
for n = 1:1:5
f_mn(i) = (c/2)*(sqrt(((m/a)^2)+((n/b)^2))); %store in array, to view all values in history of f_mn calculations
if f_mn(i) < f
modi(1,j+k)=n;
modi(2,j+k)=m;
j=j+1; % Must update j by assigning new value to j
end
i=i+1;
end
k=k+1 % Must update k by assigning new value to k
end
f=get(0,'Format') %store old format of display
format compact %display in compact format
format short e %additionally, short exponential format
f_mn
modi
format(f) %restore format
  4 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 7 日
... and change to
format(f)
Khalid Mahmood
Khalid Mahmood 2021 年 4 月 7 日
編集済み: Khalid Mahmood 2021 年 4 月 7 日
I checked format in R2020a. f=format; and format f; commands both dont work.
instead use:
f = get(0,'Format')
format short e
format compact
.. and at last
format(f)

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by