While loop: iterate until the numbers past the decimal are fixed. Problem
2 ビュー (過去 30 日間)
古いコメントを表示
I'm using a numerical integration method to approximate an integral. I need to use a minimum number of iterations that give an answer that doesn't change before the first 5 decimal places. eg say the correct answer is 6.12345678
I would need to find the first instance at which my numerical method produces 6.12345 two time consecutively.
However I seem to only be able to find a way to first the first instance at which the first 5dp are the same, which may or may not be the correct result. For example, I realised the way I tried it didn't work when this happened:
say the true result is 2.45612746 there answer I am therefore looking is 2.45612
However the iteration stops early, giving me this result: 2.45620
So even though for a period the first 5 digits after the dp are .45620, they will still change further down the line. I'm not sure how I can get past that problem without using the known answer.
Here is what I'e tried:
while error>0
...
r = fix(x(j)*1e5)/1e5
r2 = fix(x(j-1)*1e5)/1e5
error= abs(r-r2)
end
2 件のコメント
Greg
2017 年 4 月 3 日
Unrelated to the question:
"Error" is a builtin MATLAB function. It is very bad practice to override that as a variable name.
回答 (1 件)
Walter Roberson
2017 年 4 月 4 日
To be sure that the N'th decimal place will not change, you need to know that as the N+1'st decimal place changes that alterations to it cannot result in the values rounding differently.
To 5 decimal places, 6.12345678 is not 6.12345: it is 6.12346 because of rounding.
2.45612746 to 5 decimal places is 2.45613
You need to rethink your stop point.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!