(12C) Solve f(x)=0 with modified Regula Falsi method
|
03-25-2018, 05:58 PM
(This post was last modified: 03-26-2018 11:35 AM by Dieter.)
Post: #1
|
|||
|
|||
(12C) Solve f(x)=0 with modified Regula Falsi method
Yesterday Gamo posted a Newton solver for the 12C, and I wondered if a Regula Falsi method was also possible. Since the 12C does not feature subroutine calls – which are usually required to evaluate f(x) – such a program is a bit tricky.
The following program implements a modified Regula Falsi method, here the Illinois variant. This is a well known algorithm that has been used since the days of the HP65. As the 12C does not support subroutines the following approach was used: The evaluation of f(x) is required for the two initial guesses a and b, i.e. for f(a) and f(b), and for the function value of the new approximation c, i.e. f(c). After the function value is calculated, the program in all three cases jumps back to a common return point (line 09). Here a flag decides how to continue: the flag is a number in R0 which can be negative, positive or zero. The first case encodes the f(a) call, so that the program continues with storing f(a) and calculates f(b) next. The second case is set for f(b) so that the program stores this value and proceeds with the start of the iteration loop. Finally the flag is set to zero, in this case the program knows that it returns from an f(c) call, so it continues with the respective part of the Regula Falsi algorithm. Here is the program. Please note: this is experimental code that has not seen much testing, so use it at your own risk, and please report any errors you find. Code: 01 STO 2 store b The program iterates until the last two approximation agree to display precision. So set FIX 6 if a result with six digit accuracy is desired. Please note that the 12C handles the display different from other HP calculators, for instance in cases where a number is so close to zero that other calculators would switch to scientific notation. So if the program does not stop, press [R/S] and start over with a different display setting. Usage: Enter program Enter code for f(x) as usual, starting at line 74 End f(x) code with GTO 09 Example function: e^x – 3x = 0 Code: g [GTO] 73 Set the desired accuracy via FIX mode. For instance, for six decimals set FIX 6. Enter 0 and 1 as initial guesses. Code: f 6 Now also find the root between 1 and 2. Code: 1 [ENTER] 2 [R/S] Note that f(a) and f(b) should have opposite signs. If this condition is not met the program generates an "Error 0" message. If you want to continue anyway, clear the error display and press 0 [R/S] (any other non-negative value will do as well). The last two approximations are returned in X and Y so that these can be viewed with [X<>Y]. Code: f 9 So the last approximation is correct in 9 digits, probably even in all 10. Another [R/S] restarts the iteration with these two values as initial guesses, and in fact this yields the same result 1,512134552 in both X and Y. Addendum: the mentioned Wikipedia article includes a different way of calculating the new approximation c which is said to have some numerical advantages. If you want to try this in the above program simply replace line 30...40 with the code below. Since the line count remains the same the is no need to adjust any GTOs. Code: .. ... Dieter |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(12C) Solve f(x)=0 with modified Regula Falsi method - Dieter - 03-25-2018 05:58 PM
RE: (12C) Solve f(x)=0 with modified Regula Falsi method - Gamo - 03-26-2018, 06:26 AM
RE: (12C) Solve f(x)=0 with modified Regula Falsi method - Dieter - 03-26-2018, 07:43 AM
RE: (12C) Solve f(x)=0 with modified Regula Falsi method - Gamo - 03-26-2018, 01:04 PM
RE: (12C) Solve f(x)=0 with modified Regula Falsi method - Dieter - 03-26-2018, 04:37 PM
RE: (12C) Solve f(x)=0 with modified Regula Falsi method - Dieter - 03-27-2018, 06:13 PM
RE: (12C) Solve f(x)=0 with modified Regula Falsi method - Carsen - 03-26-2018, 10:24 PM
|
User(s) browsing this thread: 1 Guest(s)