Numeric integration on 34s



#2

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.


#3

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


#4

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.


#5

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


#6

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?


#7

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

A default library is a decent idea.


- Pauli


#8

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?


#9

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


#10

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.


#11

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.


#12

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

#13

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.


#14

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.


#15

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.


#16

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

- Pauli


#17

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.

#18

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

- Pauli


#19

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

Should have written down the others. :-)


#20

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

#21

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


#22

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


#23

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


#24

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

#25

Here is the PPC ROM IG routine write-up:

PPC ROM IG Routine


#26

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


Possibly Related Threads...
Thread Author Replies Views Last Post
  Integration question and "RPN" mode comment Craig Thomas 16 765 12-05-2013, 02:32 AM
Last Post: Nick_S
  HP Prime suggestion to avoid Numeric/Symbolic confusion Chris Pem10 4 263 11-19-2013, 05:49 AM
Last Post: bluesun08
  Prime as laptop numeric keypad Jose Gonzalez Divasson 1 164 10-31-2013, 02:25 AM
Last Post: debrouxl
  WP34s integration trapped in infinite loop Bernd Grubert 25 911 10-17-2013, 08:50 AM
Last Post: Dieter
  Using units in Numeric Solver Harold A Climer 1 170 10-13-2013, 10:44 AM
Last Post: Tim Wessman
  HP Prime integration Richard Berler 1 176 10-06-2013, 10:52 PM
Last Post: Helge Gabert
  integration on 39gII emulator Wes Loewer 29 1,029 06-07-2013, 05:58 PM
Last Post: Chris Smith
  WP-34S Integration Richard Berler 15 617 03-08-2013, 02:29 AM
Last Post: Walter B
  HP 34S integration Richard Berler 16 578 02-18-2013, 04:42 PM
Last Post: Marcus von Cube, Germany
  Full (for now) Numeric CAS section up Eddie W. Shore 0 103 12-06-2012, 01:22 AM
Last Post: Eddie W. Shore

Forum Jump: