The Museum of HP Calculators

HP Forum Archive 20

[ Return to Index | Top of Index ]

Numeric integration on 34s
Message #1 Posted by Gene Wright on 13 June 2011, 4:49 p.m.

Ok, so I started trying the examples from the IG routine in the PPC rom manual using the built-in integration function on the 34s.

Less than nice results. :-) Of course, this was expected.

So, I then decided...why not just adapt the PPC ROM IG routine and key it into the 34s.

Everything keyed properly with a couple of exceptions. The HP 41 puts numbers on one program line while the 34s puts each digit on a separate line. Not usually a problem even though not as readable as the HP 41 way.

However, it did cause a problem until I spotted it in the IG listing.

Lines 17 and 18 in IG are E and then 2, which enters a 1 and then a 2 into Y and X, respectively.

My first attempt was to key a 1 then ENTER and a 2 and go back and delete the ENTER. Oops. That made the 1 2 a 12. So, if you have things like 1 and 2 to be entered as two numbers, the ENTER is needed. No hidden "null" like on the HP 41 series.

That said, IG runs like a charm with one change (Ok, some other changes too like RCL L for LastX, but those are not real changes):

IG looked for alpha labels for the function to be integrated. I found that XEQ ind 10 with a 3 letter alpha label inside errored out...so I changed the program to simply XEQ 99 and used label 99 for the function.

It is very fast, but even the 34s is taking its time right now solving the integral from 0 to 1 of x^(-1/2). The "Answer" is 2. So far, after about 4-5 minutes, the 34s has 1.9999678... and it is still going. Of course, I put it in SCI 9, so I am being a real pain to the poor 34s.

The program IG on the 34s takes 93 program steps/lines. If you want to do numerical integration, that is probably worth it.

I plan to show the results from the IG program and the built-in integration command when I can.

Hey 34s team... those "internal" constants in the manual for the 34s integration... do they take up more/less space than a 93 step program? Or is it that the constants are not in a similar memory location than an IG program-like function would be?

Edited: 13 June 2011, 4:52 p.m.

      
Re: Numeric integration on 34s
Message #2 Posted by Paul Dale on 13 June 2011, 6:06 p.m.,
in response to message #1 by Gene Wright

Quote:
IG looked for alpha labels for the function to be integrated. I found that XEQ ind 10 with a 3 letter alpha label inside errored out...so I changed the program to simply XEQ 99 and used label 99 for the function.

We cannot distinguish alpha data from numbers at the moment. There are two commands [alpha]XEQ and XEQ[alpha] that do what you want and more. The internal command has a special call user routine function available that can call alpha or numeric labels.

Quote:
Hey 34s team... those "internal" constants in the manual for the 34s integration... do they take up more/less space than a 93 step program? Or is it that the constants are not in a similar memory location than an IG program-like function would be?

93 steps is 186 bytes. The constants take eight bytes each so 208 bytes for the magic constants. The integration program isn't small either -- see xrom.c. I doubt 93 steps is enough once you have to start dealing with the user's code returning NaN's and infinite results and such but it wouldn't be a huge amount more.

However, the big limitation is the internal integration command is capped at five persistent registers (i.e. there are only five registers that will survive execution of the user's routine). It don't have any more scratch registers beyond the four level stack but more could be made available at a pinch -- they wouldn't survive the user's code being executed however.

If you code meets the register requirement, please send it to me and I'll replace the internal integration routine. Actually, send it anyway and it can go into the library :-)

- Pauli

            
Re: Numeric integration on 34s
Message #3 Posted by Gene Wright on 13 June 2011, 6:37 p.m.,
in response to message #2 by Paul Dale

Hey Pauli.

It is simply the IG routine from the PPC ROM manual. Surely you have that? :-)

Actually, check ur mail since I am sending you the PPC ROM program listing.

And, note that this program as written WILL use user memory registers. Quite a few of them.

So, to have this "in rom" will mean that it will use memory registers.

Edited: 13 June 2011, 6:41 p.m.

                  
Re: Numeric integration on 34s
Message #4 Posted by Paul Dale on 13 June 2011, 8:13 p.m.,
in response to message #3 by Gene Wright

Thanks, file received. I don't have a PPC manual although I do have a module. I think it is on the DVDs though.

I really don't want internal commands touching user's registers unless there is a specific and well defined purpose that cannot be abused. This isn't the case here unfortunately.

- Pauli

                        
Re: Numeric integration on 34s
Message #5 Posted by Marcus von Cube, Germany on 14 June 2011, 2:56 a.m.,
in response to message #4 by Paul Dale

How many registers are used? Any of them just constants stored for convenience in registers? Any of them only used internally between executions of user code and can be lost if the user code program contains a STOP (which will kill RAM)?

If it really will not fit nicely within the internal (X)ROM constraints, we can still create a "default library" which gets it. The default library can then be made available with the flash download so that it is automatically included when flashing the calc. It will occupy one of the PSTO slots, of course.

Pauli, Walter, what do you think?

                              
Re: Numeric integration on 34s
Message #6 Posted by Paul Dale on 14 June 2011, 3:12 a.m.,
in response to message #5 by Marcus von Cube, Germany

Not a chance of it fitting in XROM space. Way too many registers used.

A default library is a decent idea.

- Pauli

                                    
Re: Numeric integration on 34s
Message #7 Posted by Marcus von Cube, Germany on 14 June 2011, 3:19 a.m.,
in response to message #6 by Paul Dale

Pauli,

I've high hopes of your typing skills. If you provide a library file (either salvaged from the calculator via SAM-BA or created on the emulator) I'll take care of its inclusion in the default build. We can put all the stuff in the "library" folder in it.

Anybody else willing to provide useful programs for a default library?

                                          
Re: Numeric integration on 34s
Message #8 Posted by Paul Dale on 14 June 2011, 3:21 a.m.,
in response to message #7 by Marcus von Cube, Germany

Most of the stuff in the library folder shouldn't be included. Much of it is a copy of the xrom code of the same function. I.e. useful for explaining how the internals work and how some of the special commands are used but that is all.

- Pauli

                                                
Re: Numeric integration on 34s
Message #9 Posted by Marcus von Cube, Germany on 14 June 2011, 4:43 a.m.,
in response to message #8 by Paul Dale

I agree, but e.g. the n queens benchmark or the code breaker game are likely candidates as is the improved integrator.

We need some input from the community.

                                                      
Re: Numeric integration on 34s
Message #10 Posted by Jeff O. on 14 June 2011, 11:18 a.m.,
in response to message #9 by Marcus von Cube, Germany

Here is a new version of the code breaker game that works with the latest versions of wp34s. Also includes a couple of prompts and/or messages that were not in the earlier version.

                                                            
Re: Numeric integration on 34s
Message #11 Posted by Marcus von Cube, Germany on 14 June 2011, 11:24 a.m.,
in response to message #10 by Jeff O.

Thanks Jeff! I leave it to Pauli to integrate it into the package.

            
Test Results: 34s built-in integration vs. PPC ROM IG routine
Message #12 Posted by gene wright on 14 June 2011, 1:59 p.m.,
in response to message #2 by Paul Dale

Below are the results from comparing the WP 34s built-in integration function vs. the PPC ROM converted IG routine.

PPC ROM			
IG Routine		         WP 34s	        WP 34s
Example	Function	         Built-in 	IG Program value
1   0 to 1: 4/(x^2+1)	         3.141593	3.141593
2   0 to 1: x^(1/2)	         0.666671	0.666667
3   0 to 1: Sin(PI*X)	         0.636620	0.636620
4   0 to 1: LN(x)	        -0.999147       -1.000000
6   0 to 2: (x(4-x))^(1/2) 	 3.141620	3.151593
8   0 to 1: COS( LN(x))	         0.501097	0.500000
9   0 to 1: x^(-1/2)	         1.967500	2.000000
10  0 to 1: (1 - x^2)^(1/2)	 0.785405	0.785398
12 -1 to 1: ((1-x^2)(2-x))^(1/2) 2.203398	2.203346

The tests were fairly maddening. The 34s integrator gets its results very quickly, but sometimes is just off enough to irritate you. :-) The IG program takes longer most of the time but gets more "accurate" values.

                  
Re: Test Results: 34s built-in integration vs. PPC ROM IG routine
Message #13 Posted by Marcus von Cube, Germany on 14 June 2011, 4:29 p.m.,
in response to message #12 by gene wright

The wp34s integrator uses a fixed number of points and does not iterate. It should be fairly easy to create a subinterval algorithm that divides the integration interval in two halves and integrates each part with the built in command. This can be redone several times until the combined results do not change any more.

                        
Re: Test Results: 34s built-in integration vs. PPC ROM IG routine
Message #14 Posted by gene wright on 14 June 2011, 4:32 p.m.,
in response to message #13 by Marcus von Cube, Germany

Well, the 34s integrator did a good job in many circumstances, but things like the quick result of 0.501097 to the COS(LN(X)) integral were disappointing as was the 1.9675 result to the SQRT(X) integral.

the next task I plan to try is a matrix series modeled after the M1 - M5 routines from the PPC ROM and the RRM application program from the PPC ROM.

Now THAT would make a good standard library program, IMO.

                              
Re: Test Results: 34s built-in integration vs. PPC ROM IG routine
Message #15 Posted by Paul Dale on 14 June 2011, 5:32 p.m.,
in response to message #14 by gene wright

I was thinking JMB's matrix library would be well suited for this.

- Pauli

                                    
Re: Test Results: 34s built-in integration vs. PPC ROM IG routine
Message #16 Posted by gene wright on 14 June 2011, 5:39 p.m.,
in response to message #15 by Paul Dale

Ah, but given my slim programming skills :-) it is much more likely that I can re-write the PPC ROM routines to the 34s. Now if JMB or someone else can rewrite the Matrix library... Ha!

Seriously, matrix functions are "the" missing piece to the 34s, as it is now...as the developers know well. A library that can do systems of equations, determinants (of course to do the systems) and inverses would be a great thing to fill that gap.

                  
Re: Test Results: 34s built-in integration vs. PPC ROM IG routine
Message #17 Posted by Paul Dale on 14 June 2011, 5:33 p.m.,
in response to message #12 by gene wright

Any idea what the error estimates returned by the 34s's integrator are?

- Pauli

                        
Re: Test Results: 34s built-in integration vs. PPC ROM IG routine
Message #18 Posted by gene wright on 14 June 2011, 5:42 p.m.,
in response to message #17 by Paul Dale

Well, for the COS(LN(X)) integral, the Y stack level contains 0.002169.

Should have written down the others. :-)

                              
Re: Test Results: 34s built-in integration vs. PPC ROM IG routine
Message #19 Posted by Paul Dale on 14 June 2011, 6:21 p.m.,
in response to message #18 by gene wright

The answer + or - the error estimate brackets the true integral nicely in this case at least.

What's needing is a wrapper that uses the built in integrator and subdivides the interval until the sum of the error estimates is small enough.

- Pauli

      
Re: Numeric integration on 34s
Message #20 Posted by fhub on 14 June 2011, 6:55 a.m.,
in response to message #1 by Gene Wright

Quote:
...right now solving the integral from 0 to 1 of x^(-1/2). The "Answer" is 2. So far, after about 4-5 minutes, the 34s has 1.9999678...
Hi,

I'm wondering how the WP34S (and also the PPC-ROM) can calculate this numeric integral at all, when the integrand x^(-1/2) is not defined at the left endpoint x=0?

Is there any code in the integration routine which checks for such 'undefined' function values, or is this integration algorithm just using function values at some midpoints of the sub-intervals?
Can these algorithms for numeric integration (used in the WP34S and PPC-ROM) be found anywhere?

Franz

            
Re: Numeric integration on 34s
Message #21 Posted by Paul Dale on 14 June 2011, 7:03 a.m.,
in response to message #20 by fhub

The 34s's integration code doesn't evaluate at the end points.

It uses a 10/21 point Gauss-Kronrod quadrature.

The freely available source code (in xrom.c) implements the algorithm.

More information can be found on the Internet by visiting your friendly search engine.

- Pauli

                  
Re: Numeric integration on 34s
Message #22 Posted by fhub on 14 June 2011, 7:18 a.m.,
in response to message #21 by Paul Dale

Quote:
The 34s's integration code doesn't evaluate at the end points.
Well, that's good for such improper integrals! ;-)

Thanks for the info, I know a few integration methods but Gauss-Kronrod was new to me.

Franz

                        
Re: Numeric integration on 34s
Message #23 Posted by Paul Dale on 14 June 2011, 7:31 a.m.,
in response to message #22 by fhub

Gauss-Kronrod quadratures are pretty much the standard these days. Their main benefit is that the function is evaluated for the Kronrod quadrature and the Gauss points are a subset. Thus you get two different order estimates of the integral which gives you an idea of the error.

Generally they are used with a fairly adaptive wrapper that increases the number of quadrature points -- this can be done while reusing the existing function evaluations if the points are carefully chosen. The 34s isn't this sophicated however.

Quite an interesting lump of theory :-)

- Pauli

            
Re: Numeric integration on 34s
Message #24 Posted by gene wright on 14 June 2011, 7:11 a.m.,
in response to message #20 by fhub

Here is the PPC ROM IG routine write-up:

PPC ROM IG Routine

                  
Re: Numeric integration on 34s
Message #25 Posted by fhub on 14 June 2011, 7:24 a.m.,
in response to message #24 by gene wright

Quote:
Here is the PPC ROM IG routine write-up:

PPC ROM IG Routine


Thanks, very interesting and detailled article! :-)
Of course Romberg is one of the standard methods, but so far I didn't know that it could also be used for improper integrals.

Franz


[ Return to Index | Top of Index ]

Go back to the main exhibit hall