HP Forums
RPN Programming - Printable Version

+- HP Forums (https://archived.hpcalc.org/museumforum)
+-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html)
+--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html)
+--- Thread: RPN Programming (/thread-170019.html)



RPN Programming - hpnut - 07-30-2010

I wonder why HP choose to introduce difficult examples in RPN programming? for example, the HP30b modules have these examples:

1. calculating digits of PI
2. finding prime factors of an integer
3. base conversions for bases 2-10

http://h10025.www1.hp.com/ewfrf/wc/document?docname=c02047259&tmp_task=useCategory&lc=en&dlc=en&cc=us&lang=en&product=4031712

why can't they use examples like celcius to fahrenheit or solving quadratic equations like in the earlier HP 41C manuals?

I am still at a loss programming the new HP calculators from HP 35S onwards.




Re: RPN Programming - bill platt - 07-31-2010

35s really is mostly the same as the 41C, to me. The odd parts are the new aspects with gotos that don't fit with the 32s paradigm. But it is still fundamentally the same as a 41, no?

Why not "simple" examples? Well, the two you gave are built-in functions, so that wouldn't be particularly useful, would it? May most of the "simple stuff" is already taken up with built-ins!

I think the base conversion program is very useful for learning the programming.


Re: RPN Programming - Don Shepherd - 07-31-2010

I think the idea was just to include some examples that most folks could relate to, so that they could see how programming is done on this machine, because it is somewhat different than "standard RPN" as many of us know it.

Here is a list of some "gotcha's" that you probably need to know about the 30b in order to program it effectively. Once you get used to some of its weird habits, I think you will like it, especially its speed.

The prime factor program shown is not the fastest in the world, but it is simple. Any regular member of this forum who knows RPN should be able to write a quadratic equation solver with little effort, especially since it won't need any conditional statements, typically. The 30b conditionals are a bit tricky until you understand how they work.


Re: RPN Programming - Gene Wright - 07-31-2010

Probably because there are examples of simpler programs elsewhere.

Examples of assigning programs to keys.

Examples of using HP solve.

etc.

then again, I'm certain that if the modules ONLY had the quadratic equation and F->C and C->F, more people here would have complained that the examples were too simple.

So, here you go. RPN mode assumed.

C->F... key celsius temperature, press = = while in the program catalog to execute.

LBL 0
9
x
5
/
3
2
+
R/S
F->C...key fahrenheit temperature, press = = while in the program catalog to execute.
LBL 1
3
2
-
5
x
9
/
R/S
For a quadratic, I would use the solver, not a program.

The learning module for using HP Solve shows several examples of how to do this.


HP Solve module


Example 1: Finding the roots of y = (x-2)^2 - 1

Example 2: Finding the roots of y = (x-2)^2 + 1

I can't help the typos in this web document.

Hope these help.

Edited: 31 July 2010, 11:53 a.m.


Re: RPN Programming - Don Shepherd - 08-01-2010

Quote:
For a quadratic, I would use the solver, not a program.

On the 30b, to use the solver you're going to have to write a program anyhow, and if I'm going to write a program for this problem I'll implement the quadratic formula which will give me both roots without having to "guess." That's the one thing I've always disliked about the HP solver, it makes you guess in order to get both roots, and if you guess, you probably already know the answer!


Re: RPN Programming - Gene Wright - 08-01-2010

True, true.

But if someone is unsure about programming enough that they really need examples for converting F->C and C->F, then a short program in the solver for X^2-4X+4 is probably easier to type in and run than a longer one to give both roots at once.

But, I do agree with your point, certainly. :-)


Re: RPN Programming - Don Shepherd - 08-01-2010

Thanks, Gene. That's one thing I like about the NSpire, it's SOLVE function. Now, I know the NSpire is a CAS and the 30b is not, but if you type in SOLVE(x^2+5x+6=0,x) it comes back with x = -2 or -3. Now that's what a solver ought to do!


Rootfinding function (SOLVE) and guesses - Karl Schneider - 08-02-2010

Quote:
That's the one thing I've always disliked about the HP solver, it makes you guess in order to get both roots, and if you guess, you probably already know the answer!

I can't concur with that statement. The idea behind the 'guesses' is to bound the interval for the root of interest. The user may know the approximate value of that root, but requires automated numerical iteration in order to efficiently "home in" on it.

Also, periodic functions may have an infinite number of real-valued roots.

From your subsequent post:

Quote:
...if you type in SOLVE(x^2+5x+6=0,x) it comes back with x = -2 or -3. Now that's what a solver ought to do!

Sure, but that's a quadratic function with two exactly-specified, easily calculated roots (which may or may not be real-valued). A solver can be programmed to find all roots of a polynomial function; the convenient "SOLVE POLYNOMIAL" screen of the HP-48G series, as well as the PROOT function of the HP-71B Math Pac, will do so.

-- KS


Edited: 4 Aug 2010, 11:33 p.m. after one or more responses were posted


Re: Rootfinding function (SOLVE) and guesses - Don Shepherd - 08-02-2010

Karl, I guess we'll have to agree to disagree. Yeah, the user may have an idea of the value of the second root, but he may not also and will have to do more work with the solver to find it. I think the real value of the HP solver is finding solutions to first-degree equations with multiple variables, where you know the values of all variables except one and it will calculate and tell you that one. And I like the approach used in machines like the 17bii where you just enter the equation (don't have to set it to 0) and provide values for the known variables and it tells you the unknown. I like that approach more than requring you to write a program, as in the 30b for example.

But, truth be told, I think the method I like best is doing it in my head, like completing the square or factoring or using the quadratic formula manually. I've read that doing mental things as you get older (I'll be 60 in a couple of weeks) keeps you from getting Alzheimer's disease, plus it's just fun!




Re: Rootfinding function (SOLVE) and guesses - Ángel Martin - 08-04-2010

Quote:
I've read that doing mental things as you get older keeps you from getting Alzheimer's disease, plus it's just fun!

I sure hope that's true - my ultimate anti-Alzheimer trick is 41-MCODE, highly recommended. :)


Re: Rootfinding function (SOLVE) and guesses - Karl Schneider - 08-05-2010

Don --

Quote:
Karl, I guess we'll have to agree to disagree. Yeah, the user may have an idea of the value of the second root, but he may not also and will have to do more work with the solver to find it.

I took your sentiment of not liking the 'make a guess' requirement in more general terms, as opposed to pertaining specifically to quadratic solutions.

I still maintain, though, that the user ought to be able to deal adroitly with quadratics, even working them out manually in lieu of programming a solution. The best way for a calculator to return both/all solutions automatically, without the benefit of guesses entered by the user, is with a polynomial-solving function.

Oftentimes, one mathematical quadratic solution will have no practical use in a real-world problem.

Quote:
I think the real value of the HP solver is finding solutions to first-degree equations with multiple variables, where you know the values of all variables except one and it will calculate and tell you that one.

They need not be 'first degree' equations; the multi-variable equation can be of any form. RPN-based HP solvers will use two 'user-supplied' guesses from stack contents and the stored value of the 'solve' variable. I would assume that the stored value (if present) is the starting point for the HP-17B/27S solver.

If the guesses are the same, the solver will decide in which direction to proceed. If the solver is to succeed, neither guess must cause a math error.

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/articles.cgi?read=556

-- KS


Edited: 6 Aug 2010, 1:40 a.m. after one or more responses were posted


Re: Rootfinding function (SOLVE) and guesses - Bart (UK) - 08-05-2010

I think the problem is again what the older generation complains so much about - people want to just punch problems into computers and get answers, losing all inkling of whether the answer makes sense. This has been discussed so often on this forum. The same goes in this case where calculators can be programmed to give answers to simpler quadratics without guesses, but for more complex problems there can be a multitude of answers and you can be sure the calculator giving you definite solutions will give the ones you don't want. I think working with a calculator that forces you to guess even for the simple quadratics, teaches you to do it right for the more complex ones.


Re: Rootfinding function (SOLVE) and guesses - Namir - 08-05-2010

Solving for the roots of polynomials (with real coefficients) using the Lin-Bairstow algorithm requires no guesses for the roots. This algorithm will find all of real and complex roots of the polynomial. Unfortunately, when it comes to non-polynomial functions, at least one guess for the root is required to tell, whatever algorithm being used, where to start looking.

Namir


Re: Rootfinding function (SOLVE) and guesses - Don Shepherd - 08-05-2010

Quote:
This has been discussed so often on this forum.

I think we can all agree with that!

Here's what I think. It's not the calculator's responsibility to make sure that the person pushing its buttons understands the answer it provides. That's probably somebody's responsibility, but not the machine's. If a solver application can find a solution without the button-pusher providing a guess first, do it. Personally, I'd like the solver to do this for a quadratic equation: recognize that it's a quadratic, get the coefficients of x^2 and x and the constant term (a, b, and c), apply the quadratic formula, and return the two solutions (if there are two solutions) in x and y. Now, there may be a legitimate reason that the solver has not taken this approach in the past, so that's OK. But maybe it's time to update the solver (radical idea).

I teach my middle school students to find the solutions to quadratics by factoring, if possible, then completing the square, then applying the quadratic formula, and I think that's good because it makes them think (and work!). Once they leave school, they will probably never have to do such things again in their life (similar to box-and-whiskers plots, don't get me started on those), but the process of doing it now exercises their brains in ways that video games cannot.


Re: Rootfinding function (SOLVE) and guesses - Thomas Klemm - 08-05-2010

Quote:
using the Lin-Bairstow algorithm requires no guesses for the roots

However you need to give initial values (guesses) for the quadratic polynomial. Also Wikipedia states:

Quote:
As first quadratic polynomial one may choose the normalized polynomial formed from the leading three coefficients of f(x),

Is there a reason for that choice? In my experience it just doesn't matter much, what you chose.

For those interested I once wrote a program for the HP-11C.
However the trick using L.R. to solve the linear equation might not always work with later HP models since n is checked to be positive.

My first encounter with this algorithm was with an article in PRISMA, the magazine of the former CCD. The program was written for the HP-41C (of course) and I didn't understand a single thing, what was going on. It must have been around 1984. Unfortunately I've lost the copy of the article but maybe someone in this forum can still find it.

Jean-Marc Baillard contributed a solution to the MoHPC HP-41C Software Library:
Polynomials for the HP-41

cf. g) Quadratic factors ( Bairstow's method )

Kind regards

Thomas


Re: Rootfinding function (SOLVE) and guesses - Namir - 08-05-2010

Thomas,

The implementation I use of Lin-Bairstow internally provides the initial guesses for the quadratic coefficients. The caller to the Lin-Bairstow subroutine needs not supply any initial guesses.

Thank you sooooo much for the HP-11C listing your provided. I was looking for such an implementation that uses no memory registers. Good job!!

Also thanks to the link to Baillard's excellent 41C listings. I was thinking of writing a (new) Bairstow method. The listing is interesting/

Namir

Edited: 5 Aug 2010, 3:55 p.m.


Re: Rootfinding function (SOLVE) and guesses - Thomas Klemm - 08-05-2010

The images in the Wikipedia-article suggest that there are initial guesses that might lead to divergence for certain polynomials. Do you know how this is avoided in the implementation you use? Are there best initial guesses that can be easily computed from the coefficients as suggested? Do you know the reason for this suggestion?

My implementation of course uses some memory registers but I tried to use as few as possible. Otherwise you run out of memory for the program. IIRC it's possible to solve polynomials up to degree 7, probably even a little more if you sacrifice the quadratic solver program at LBL A.

It's nice you like the program.


Re: Rootfinding function (SOLVE) and guesses - Namir - 08-05-2010

I emailed you the listing of a VB SUB that implements the Lin-Bairstow algorithm. The museum email server may have converted some characters. You can write me at nshammas{AT]aol[DOT].com if you want me to send you the entire Excel file.

Namir


Re: Rootfinding function (SOLVE) and guesses - Thomas Klemm - 08-06-2010

From the listing you sent me:

     ' start the main Lin-Bairstow iteration loop
' initialize the counter and guesses for the
' coefficients of quadratic factor:
'
' p(x) = x2 + alfa1 * x + beta1
'

alfa1 = 1
beta1 = 1

It appears the same initial values are used as in the example of my solution:

1      STO 7
STO 8

Remark: Please note the slight difference in the definition of the remainder polynomial:

Wikipedia:

cx + d

My solution:

R(x) = b1 ( x + p ) + b0

It turns out to make the algorithm more concise since there's no special case in calculating b0 (in my solution) as it would be for:

d = a0 - v b0

Please note also the shift of indexes by 2 when comparing the quotient:

Q(x) = bn xn-2 + bn-1 xn-3 + ... + b4 x2 + b3 x + b2

These kind of subtleties may have a considerable impact on the code.


Re: Rootfinding function (SOLVE) and guesses - Karl Schneider - 08-06-2010

Quote:
Personally, I'd like the solver to do this for a quadratic equation: recognize that it's a quadratic, get the coefficients of x^2 and x and the constant term (a, b, and c), apply the quadratic formula, and return the two solutions (if there are two solutions) in x and y.

This requires that the equation be entered in full detail. And, what if the terms had not been gathered -- should the calc still be able to recognize it as a quadratic? Any sophisticated solution will entail CAS.

I still say that the ideal solution is a polynomial solver, like that of the HP-48G and its descendants: Choose "Solve Polynomial" from the SOLVE menu, enter [1 5 6], hit "Solve", and read -2 and -3 as the answers. A PC might be a better tool if the problem is more complicated than that.

A quadratic-equation problem came up at work recently. After failing for a short while at simply reckoning an operating point, I derived a quadratic expression, programmed it as an equation on the HP-32SII, and quickly solved it numerically, knowing what a realistic answer would look like. (The other solution, which I computed manually, was indeed unrealistic -- well outside the operating limits of the equipment. The HP-32SII found that one, too, with suitable guesses steering it in that direction.)

-- KS


Edited: 8 Aug 2010, 2:21 a.m. after one or more responses were posted


Re: Rootfinding function (SOLVE) and guesses - Don Shepherd - 08-06-2010

Yeah, trying to determine if a solver program represents a quadratic equation would be problematic, given all the possibilities and different coding styles of users. So I admit, that's a bad idea.

One thing I like about the SOLVE command on the TI-Nspire is that it is general purpose. It solves simple equations, quadratic and higher order equations, and systems of equations (what we called simultaneous equations when I was in high school, I always wondered why they changed the name). And it doesn't require you to set the whole thing equal to zero.

solve (x-12=30,x) yields x=42
solve (x^2+5x+6=0,x) yields x=-2 and x=-3
solve (x+y=11 and x-y=-3,x,y) yields x=4 and y=7

There is also a CSOLVE command for complex numbers, but I've never used it.

Edited: 7 Aug 2010, 3:01 a.m.