(12C) Solve f(x)=0 with modified Regula Falsi method

03252018, 05:58 PM
(This post was last modified: 03262018 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 nonnegative 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 

03262018, 06:26 AM
(This post was last modified: 03262018 07:52 AM by Gamo.)
Post: #2




RE: (12C) Solve f(x)=0 with modified Regula Falsi method
I test this to find a root of a polynomial of this equation.
f(x)=X^4  4X^3 + 8X^2 + 20X  65 Program Method: ENTER ENTER ENTER 4  x 8 + x 20 + x 65  FIX 9 Initial Guess used: 0 ENTER 3 R/S > instant answer > 2.236067977 My favorite X^X=Y also work flawlessly. For this equation somehow not working. Solve X^3 = 3^X Rewrite equation to X^3  3^X = 0 Program Method: ENTER ENTER 3 Y^X X<>Y 3 X<>Y Y^X  The answer is 2.478052679 I try initial guess 1 ENTER 3 R/S > return 3 press R/S again > Error 0 Did I do something wrong? I try on the Newton's Method and work OK Gamo 

03262018, 07:43 AM
(This post was last modified: 03262018 10:08 AM by Dieter.)
Post: #3




RE: (12C) Solve f(x)=0 with modified Regula Falsi method
(03262018 06:26 AM)Gamo Wrote: Solve X^3 = 3^X One answer is 2,47805. But there are two. (03262018 06:26 AM)Gamo Wrote: I try initial guess 1 ENTER 3 R/S > return 3 Sure. X=3 is a solution: 3^3 = 3^3. (03262018 06:26 AM)Gamo Wrote: press R/S again > Error 0 Why do you press R/S again? What do you want to do this way? If you press R/S again you restart the solver with two identical guesses 3 and 3. So the calculated secant is a horizontal line that will never cross the xaxis and a division by zero will occur. That's why the two guesses must be different. However, the program can be changed so that it does accept two identical guesses if these are already a root of f(x). See below. (03262018 06:26 AM)Gamo Wrote: Did I do something wrong? You simply didn't notice that 3 is one of the two solutions. ;)  If you enter 1 and 3 as initial guesses the program correctly returns X=3.  Of course the program can also calculate the other root: simply start with different guesses, for instance 1 and 2,5 => 2,478052679. BTW, the exact root is 2,4780526802883..., but with 10 digit precision everything between 2,478052678 and ...683 evaluates to f(x)=0. If you start with 1 and 2,8 or 1 and 2,9 you will get these results. Edit: here is a modified version that works even with two identical guesses if these are a root of f(x). f(x) now starts at line 76 and ends with GTO 10. Code: 01 STO 2 store b Additional feature: in the f(x) code, x now can be addressed with "RCL 3". So your example can be coded like this: 3 Y^X 3 RCL 3 Y^X – Dieter 

03262018, 01:04 PM
Post: #4




RE: (12C) Solve f(x)=0 with modified Regula Falsi method
The modify version also used Register 3 in the program, is that OK to use in the
f(x) program with RCL 3 ? I try both Newton's and Regula Falsi methods on Modern 12C+ and on Official 12C App on Android OS. Both devices run both methods equally fast of about one second. For Newton's Method I find that the program is shorter is only about 41 lines of code versus 75 lines of the Regula Falsi Method program. Newton's Method is more cumbersome to input all the necessary data for regular user such as Store Initial Guess, Store tolerance Regula Falsi Method is very easy to input data, user only put two initial guesses then R/S that's all. I like to know how long it take to compute with the older version of the HP12C For comparison Newton's Method run slow on HP11C but with Regula Falsi this method run faster and very reliable it can take almost any equation with ease. Gamo 

03262018, 04:37 PM
(This post was last modified: 03262018 04:51 PM by Dieter.)
Post: #5




RE: (12C) Solve f(x)=0 with modified Regula Falsi method
(03262018 01:04 PM)Gamo Wrote: The modify version also used Register 3 in the program, is that OK to use in the Yes, x is always stored in R3 before the function is called. So you can use "RCL 3" for every occurence of x. And of course x it is also passed in the Xregister. ;) (03262018 01:04 PM)Gamo Wrote: Newton's Method is more cumbersome to input all the necessary data for regular user such as Store Initial Guess, Store tolerance Take a look at the Newton version I posted in the respective thread. It's the same 41 lins but the user only has to store the tolerance. Of course this program can also be modified so that it uses the same method as the Regula Falsi program: compare the last two rounded approximations. This way there is no need to enter a tolerance. In other words: all this can also be done with the Newton program. (03262018 01:04 PM)Gamo Wrote: I like to know how long it take to compute with the older version of the HP12C For comparison Newton's Method run slow on HP11C but with Regula Falsi this method run faster and very reliable it can take almost any equation with ease. I can't say anything about execution times on a classic hardware 12C but beware: for every method there are cases where the algorithm will be slow or even fail. There is no universal method that will always return an exact result reliably and fast. Dieter 

03262018, 10:24 PM
Post: #6




RE: (12C) Solve f(x)=0 with modified Regula Falsi method
This is part of the reason why I love the HP12C so much. Programming. This program really made my day!


03272018, 06:13 PM
(This post was last modified: 03272018 06:13 PM by Dieter.)
Post: #7




RE: (12C) Solve f(x)=0 with modified Regula Falsi method
(03262018 04:37 PM)Dieter Wrote:(03262018 01:04 PM)Gamo Wrote: Newton's Method is more cumbersome to input all the necessary data for regular user such as Store Initial Guess, Store tolerance Gamo and Carsen: There is a new version of the Newton solver with a modified exit condition I just posted in the respective thread. You do not have to prestore anything and the danger of an infinite loop (which may happen sooner as one might expect) is (mostly) avoided – take a look at the examples. I like this approach better than the previous ones. Dieter 

« Next Oldest  Next Newest »

User(s) browsing this thread: 1 Guest(s)