From: jhmeyers@miu.edu (John H Meyers)
Newsgroups: comp.sys.hp48
Subject: NI - Numerical Integration helper
Date: 9 Apr 1998 04:37:17 GMT
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Lines: 71
Message-ID: <6ghj9t$q1j$1@news.iastate.edu>
References: <6g3nj7$rc@bgtnsc02.worldnet.att.net>
Reply-To: jhmeyers@miu.edu
NNTP-Posting-Host: vax1.mum.edu
Xref: republic.btigate.com comp.sys.hp48:9101

In article <6g3nj7$rc@bgtnsc02.worldnet.att.net>,
Adam Krieg <ootanbon@worldnet.att.net> writes:

> I am trying to numerically integrate a [complicated] function.  To save
> time, I collect some parts of the integrand as symbols.  For example,
> I might take 'exp(-x^2)' and store it as 'M' in my directory.  When I
> integrate M symbolically with respect to X, it treats M as a constant.
> I was wondering if there is something that can be done to make the
> Integrator search through the directory to see if the symbols input
> are functions of the independent variable, as the Solver does.

One way is to use the 'where' function [|] in the formula, e.g.
'M|(M=EXP(-SQ(X)))', but I suspect that this is not very appealing,
since it doesn't shorten the expression unless M appears more than once
(and it lengthens the computing time quite a bit).

Well, a nice internal system function ought to exist to help us,
since the plotting functions (as you note below) manage to handle this,
but all I could come up with at the moment is this User-RPL program:

%%HP: T(3); @ NI - Numeric Integration function.

@ Use when function is not explicit in the variable of integration,
@ e.g. 1 2 'EQ' 'X' NI  where 'EQ' contains a function of X.

@ Arguments are the same as for the built-in integral function:
@   start  end  'expression' 'variable'
@ Can also be used algebraically: 'NI(start,end,expr,var)'
@ [or 'NI(start,end,expr,QUOTE(var))' if 'var' already exists]

\<< \-> a b f v \<<
  \<< \-> ~ \<< ~ v STO f \->NUM \>> \>> \-> p \<<
  a b 'p(~)' '~' \.S \->NUM \>> \>> \>>

@ Unlike built-in numeric integration (but like plotting),
@ argument values are stored into the variable of integration, and
@ it is not necessary to set flag -3 to obtain numeric results.  Also,
@ with stack arguments, a <<program>> may be used in place of 'expression'

@ Due to User-RPL, computation will be slower (but will get the answer :)

@ Remember to set the number format (e.g. 2 FIX)
@ for desired accuracy (versus speed).

@ End of program file (snip here).

Note that '~' is an obsure variable name, used to avoid name conflicts;
arguments should also not contain *local* variable names in { a b f v p }
(but all global names are okay) -- this can of course be fixed by choosing
more obscure names for all of the local variables above, but I got lazy :)

> I was thinking of maybe graphing the function (Plot recognizes
> dependencies) and then finding the area underneath the curve.
> How does the accuracy compare with the Integration program?

  It should use the same internal function, so the accuracy should be
  the same; however, when using the AREA function on the graph, you
  mark the interval by moving the graphics cursor, which can only move
  by whole pixel jumps, so you can not set the limits as precisely.

So, who knows just what goes on when the function is integrated in the
"picture" environment, yet which the normal Integral function does not do?

Also note that if your function is defined as a program, you can plot it,
but you can neither get its area from the graph nor numerically
integrate it using the \.S command, whereas the above program can do so.

Anybody at HP Australia want to make a little note of this?

-----------------------------------------------------------
With best wishes from:   John H Meyers   <jhmeyers@mum.edu>