Message Boards Message Boards

0
|
4347 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Precision, lower boundary of

Posted 11 years ago
Hi,
I wrote the following program, to find out the root of a function. I want to have more than a thousand numbers behind the comma but have the problem, that in the line (*Test*) I can not enter a lower border smaller than 10^{-60}. Is this simply the smallest "Test" number "For" can work with? Or can I increase the Precision somehow?

Greets Nick

The Source Code is:
 Clear["Global`*"]
 (*Errormessage*)CheckAbort[
  (*initial values and function*)a = 0; b = 2; f[x_] = Cos[x];
  If[
   (*Conditions when coputation should be abborted*)(f[a] > 0 \[And]
      f[b] > 0) \[Or] (f[a] < 0 \[And] f[b] < 0),
   (*When condition true do this*)Abort[],
   (*else do this*) m = (x1 + x2)/2; For[
    (*initial value for Loop*)
   If[f[a] > 0, x1 = a; x2 = b, x1 = b; x2 = a],
   (*Test*)Abs[x2 - x1] > 10^-60,
   (*decides which of the two values x1 and x2 is set to m*)
   If[f[m] >= 0, x1 = m, x2 = m]]
  ;(*Output*)N[m, 100]
  ]
, "Falsch"(*error Message*)]
POSTED BY: Nickel Paulsen
4 Replies
Posted 11 years ago
I got this intervall nesting principle from a math book and wanted to test it out. In the book is mentioned that this process might not converge as fast as others, so this is the reason, like you also said, for the long computing time. 

but anyway thanks a lot for your help!


Edit:

I have got an other problem:

if my f(x) is calculated with a error of 3*10^{-17} (because I wrote it in a series that was aborted after a specific n out of the natural numbers) and the last output I get is the middle of an Intelvall with the lengh 10^{-15}. Why is the total error of my root  (plus minus) 0.5*10^{-15} like it is written in the book? Don't I have to consider the error of the series?
POSTED BY: Nickel Paulsen
Posted 11 years ago
In your particular example you can reduce $MaxExtraPrecision from 1000 down to about 400 and usually not have an error. That will save less than 25% of your time.

Since you seem to only be using the Abort if
Sign[f[a]]==Sign[f[b]]
and might not have any other reasons for Abort during your calculation you might be able to remove the CheckAbort and Abort and just test the two signs before beginning your loop, but changing that does not seem to save any significant time.

Your For loop is written in an unusual way and not like most you would see as examples. Unusual coding can sometimes take more time, but changing that does not seem to save any significant time.

Since you are doing bisection you will only reduce the width of the interval by 1/2 on each iteration. Since you want an interval of less than 10^-400 this will require many iterations and a long time.

If your function is smooth and well behaved then you might replace bisection with Newton or another method which more quickly converges and would require fewer iterations. That could still use the For that you require. That may be the best way to greatly reduce the time needed, but increasing the risk that a bad function would not converge with Newton.
POSTED BY: Bill Simpson
Posted 11 years ago
 Clear["Global`*"];
 Block[{$MaxExtraPrecision = 1000},
  CheckAbort[
   a = 0; b = 2; f[x_] := Cos[x];
   If[(f[a] > 0 \[And] f[b] > 0) \[Or] (f[a] < 0 \[And] f[b] < 0),
    Abort[],
    m = (x1 + x2)/2;
    For[If[f[a] > 0, x1 = a; x2 = b, x1 = b; x2 = a], Abs[x2 - x1] > 10^-400, If[f[m] >= 0, x1 = m, x2 = m]];
    N[m, 100]
  ], "Falsch"]
]
POSTED BY: Bill Simpson
Posted 11 years ago
nice! This works fine. But now it takes a really long time to compute. The important stuff that I want to hold on is the "For" command and one line above. Have I put the Commands around "For" in a way that they slow down the "For" process because it needs to check every looping time for abort and so on??

Can I improve the code in terms of computin time without deleting the "For" command?

greets Nickel
POSTED BY: Nickel Paulsen
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract