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!