While loop: iterate until the numbers past the decimal are fixed. Problem

2 Ansichten (letzte 30 Tage)
Maitiumc
Maitiumc am 3 Apr. 2017
Kommentiert: Maitiumc am 4 Apr. 2017
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 Kommentare
Greg
Greg am 3 Apr. 2017
Unrelated to the question:
"Error" is a builtin MATLAB function. It is very bad practice to override that as a variable name.
Maitiumc
Maitiumc am 4 Apr. 2017
Noted! I'll changed that. In your opinion, does it seem like what I'm trying to do is possible without knowing what the answer of the integral is?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 4 Apr. 2017
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.
  2 Kommentare
Maitiumc
Maitiumc am 4 Apr. 2017
I understand that it will depend on the rounding, but still not sure how I can use the N+1 decimal place. Say I have:
4.39054126693,4.39054050159,4.39053999807 after 4.39053999807 the first 5 after the dp don't change. So if I was to take 4.39053999807 and round it to 5dp I'll get 4.39054, but it doesn't matter as I'm looking for minimum iteration gets me to 4.39053999807, which is the first instance at which 4.39053... appears.
I'm just about ready to give up on this. Though your answer did get me thinking of one last possible solution that I think will work in this case, but I am not sure if it will work in all cases.
If I isolate the 6th dp, when this is zero, the next iteration should(?) decrease the overall value and the 5th dp will decrease by 1, the 6th increases from 0 to 9 (or less depending on the decrement)?
So the stop point I am looking for is one iteration after the first 5 numbers after the decimal are equal AND the 6th dp is zero?
I realise this won't cover instances when the decrements are such enough that the 6th dp never reaches zero at the point the first 5 stop changing. Perhaps I also need to take into account the decrements each step?
Maitiumc
Maitiumc am 4 Apr. 2017
I've tried it and it works...for that method/function/set of limits. Plugging it into a different method and it will once again fail to produce the final answer correct to 5dp
r = fix(x(n)*1e6)/1e1;
x6dp = rem(r,1)*10;
%find the difference between the results on each step
if n>1 %cannot index n-1 = 0
err = x(n)-x(n-1);
if err<errLim && x6dp<=5
err=1;
end
end
I've tried just about everything I can think of now, even truncating it, converting it to strings and back again, comparing it etc etc. Pretty desperate for some help on this, any advice is greatly appreciated.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by