HOW CAN I SOLVE THIS?

1 回表示 (過去 30 日間)
kevin liu
kevin liu 2021 年 3 月 6 日
編集済み: Jan 2021 年 3 月 9 日
  2 件のコメント
Jan
Jan 2021 年 3 月 6 日
This is a homework question. So please post, what you have tried so far and ask a specific question about Matlab.
kevin liu
kevin liu 2021 年 3 月 6 日
x=1;
while(1<1+x)
x=x*0.5; %using bisection method
while(1/x*x~=1)
x
continue;
end
end
but after running it, i got nothing...

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

回答 (1 件)

Jan
Jan 2021 年 3 月 7 日
What are the possible numbers in IEEE 754 floating point values between 1.0 and 2.0?
x = 1.0 + k * 2^-52 with k = 0, 1, ... 2^52
Run a loop over k to find a value with x * (1 / x) ~= 1.
  9 件のコメント
kevin liu
kevin liu 2021 年 3 月 9 日
oh i see......
Thank you so much~~~
but I have a question, how to avoid such errors in computer (x*(1/x))=!1?
Jan
Jan 2021 年 3 月 9 日
編集済み: Jan 2021 年 3 月 9 日
This is not an error. Computers store numbers with a limited precision. This has effects on arithmetics when using them. Famous examples:
0.1 + 0.1 + 0.1 == 0.3 % FALSE
any((0:0.1:1) == 0.3) % FALSE
1e17 + 1 - 1e17 == 1 % FALSE, it is 0
1e17 - 1e17 + 1 == 1 % TRUE
Welcome to the world of numerical maths. The first chapters of text books for this field of science explain the effects and teach methods to cope with them.
The alternativ is to use symbolic maths or numbers with unlimited precision. The later will exhaust the memory as soon as an irrational number is used, because you need an infinite amout of RAM to store it.
When I started to study, the loop took 1 day to find the first match of 1 * (1/x) ~= 1. Now Matlab takes seconds only. Running the full range over 2^52 elements will still take far too long. You can determine the concerned numbers by a formula also, but it is not trivial.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by