(12C) Newton's Method
|
03-27-2018, 06:08 PM
(This post was last modified: 03-27-2018 06:23 PM by Dieter.)
Post: #5
|
|||
|
|||
RE: (12C) Newton's Method
(03-24-2018 01:31 PM)Dieter Wrote: Here is my attempt at a corrected version: Gamo, in the Regula Falsi thread you said that using the Newton solver is a bit cumbersome as – in the original version you posted – both the tolerance and an initial guess have to be prestored. The latter can be fixed easily (cf. the version I posted above), but also the exit condition can be simplified so that no user interaction is required. The above versions use a fixed tolerance that is prestored by the user. On the other hand my Regula Falsi program quits as soon at the last two approximation agree to display precision. Here is another option: the following Newton solver iterates until the relative difference between the last two approximations drops below 5 E–9. This should provide 8 correct digits, but since the Newton method converges quadratically, in most cases the result should be close to exact. Here is the code: Code: 01 STO 1 Try it with your example function 3^x – x^3 = 0. Code: g [GTO] 46 Now try this with an initial guess of 2: Code: 2 [R/S] => 2,478052683 You can always add one more iteration by pressing [R/S] again. In the above case the result remains the same, with 10 digit precision f(2,478052683) yields exactly 0. Now start with a guess of 2,5 which is even closer to the solution: Code: 2,5 [R/S] => 2,478052686 The result differs in the last digit. Now press [R/S] again and see what you get: Code: [R/S] => 2,478052680 See? The iteration oscillates between 2,478052680 and ...86. That's why it is not a good idea to continue until the last two approximations match exactly (which would have caused an infinite loop here). The method in the above program stops if they agree in at least 8 digits, which usually means that it will not get more accurate anyway. Nevertheless you can always press [R/S] again and see if the result improves. This way an infinite loops is avoided (well, at least in most cases). Try the function with a guess of 6 and you see that it would otherwise oscillate endlessly between 3,000000002 and 2,999999998. In detail: line 34-35 divides the last correction term by h, and since h = x / 1E+4 this yields the current relative error times 1E+4. Dividing by h instead of x automatically handles the x=0 case for which h is set to 1E–4. Then 2E+5 is added and subtracted again, which shifts a sufficiently small error beyond the 10th digit so that the result is zero if the error is small enough. Selecting a constant with more or less digits (2E+6 or 2E+4 etc.) changes the error threshold, but I think 2E+5 is just right for a 10-digit calculator while h = x / 1E+4. Dieter |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(12C) Newton's Method - Gamo - 03-24-2018, 01:10 PM
RE: (12C) Newton's Method - Dieter - 03-24-2018, 01:31 PM
RE: (12C) Newton's Method - Dieter - 03-27-2018 06:08 PM
RE: (12C) Newton's Method - Gamo - 03-25-2018, 03:05 AM
RE: (12C) Newton's Method - Dieter - 03-25-2018, 06:26 PM
RE: (12C) Newton's Method - Gamo - 03-28-2018, 04:23 AM
RE: (12C) Newton's Method - Carsen - 03-29-2018, 05:11 AM
RE: (12C) Newton's Method - Dieter - 03-29-2018, 05:04 PM
RE: (12C) Newton's Method - Gene - 03-29-2018, 05:23 PM
RE: (12C) Newton's Method - Gamo - 03-30-2018, 01:15 PM
RE: (12C) Newton's Method - Dieter - 03-30-2018, 02:00 PM
RE: (12C) Newton's Method - Gamo - 03-31-2018, 01:15 PM
RE: (12C) Newton's Method - Dieter - 03-31-2018, 05:27 PM
|
User(s) browsing this thread: 2 Guest(s)