Numeric integration on 34s - 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: Numeric integration on 34s (/thread-186091.html) Numeric integration on 34s - Gene Wright - 06-13-2011 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 - Paul Dale - 06-13-2011 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 - Gene Wright - 06-13-2011 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 - Paul Dale - 06-13-2011 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 - Marcus von Cube, Germany - 06-14-2011 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 - Paul Dale - 06-14-2011 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 - Marcus von Cube, Germany - 06-14-2011 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 - Paul Dale - 06-14-2011 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 - Marcus von Cube, Germany - 06-14-2011 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 - fhub - 06-14-2011 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 - Paul Dale - 06-14-2011 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 - Gene Wright - 06-14-2011 Here is the PPC ROM IG routine write-up: Re: Numeric integration on 34s - fhub - 06-14-2011 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 - fhub - 06-14-2011 Quote: Here is the PPC ROM IG routine write-up: 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 Re: Numeric integration on 34s - Paul Dale - 06-14-2011 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 - Jeff O. - 06-14-2011 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 - Marcus von Cube, Germany - 06-14-2011 Thanks Jeff! I leave it to Pauli to integrate it into the package. Test Results: 34s built-in integration vs. PPC ROM IG routine - Gene Wright - 06-14-2011 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 - Marcus von Cube, Germany - 06-14-2011 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 - Gene Wright - 06-14-2011 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 - Paul Dale - 06-14-2011 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 - Paul Dale - 06-14-2011 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 - Gene Wright - 06-14-2011 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 - Gene Wright - 06-14-2011 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 - Paul Dale - 06-14-2011 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