HP Forums
Trapezoidal Rule or Simpsons Rule? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Trapezoidal Rule or Simpsons Rule? (/thread-4305.html)

Trapezoidal Rule or Simpsons Rule? - DrD - 07-05-2015 10:49 PM

I'm trying to gather ideas to implement a function such as the Trapezoidal Rule, Simpsons Rule (or something similar) on the Prime.

How to supply an input function, then using values from a list, is central to this problem. Specifically, I want to be able to (programmatically) input functions, values for the lower and upper range, and the number of intervals needed, for solving them. A program might begin something like this:


EXPORT Trap(f,a,b,n)
  local h=(b-a)/n, x; 
  L1:=MAKELIST(X,X,a,b,1/n);  // To create a linear range of function values from a to b. 

//  The following is the objective, but I haven't been able to tweak it for the Prime:

//  I1=(h/2)*(2*sum(f(x))-f(x(1))- f(x(n+1)));  // where x is from the list, L1 


The input variables are the function (f), lower range (a), upper range (b), and the number of intervals (n).

If you can help, thanks!


RE: Trapezoidal Rule or Simpsons Rule? - roadrunner - 07-06-2015 12:33 AM

Dr. D.

Have you seen this:


it seems similar to what you want.


RE: Trapezoidal Rule or Simpsons Rule? - DrD - 07-06-2015 11:24 AM

That is what I am trying to do, except that I want to pass-in parameters, instead of using the INPUT() statement.

The difference is that, with the INPUT() command, the type of variable CAN be specified. In Eddie's program, the variable f is type [8], which is what I need, but I want to pass the function, f, directly to the program at run time:

EXPORT Trap(f,a,b,n)

This is the issue I'm facing at the moment. It might not be possible at this firmware revision, or, at least, I don't know how to specify input typing in the example above.

Thank you for taking time to respond with that link! Input() may be the only way I can get a function into the program, for now.


RE: Trapezoidal Rule or Simpsons Rule? - Arno K - 07-07-2015 05:55 PM

Perhaps you want something like this:

local l;
IF (type(g)==DOM_SYMBOLIC  OR type(g)== DOM_FUNC)
     IF type(g)==DOM_SYMBOLIC THEN
   return (2*╬úLIST(l)-g(a)-g(b))*(b-a)/(2*n);
   ELSE return ("f must be symbolic or function!");
works fine, you may enter: numint(x^2,1,2,20), provides 2.33375
or, after f(x):=x^3, numint(f,1,2,20), this results in 3.751875
the type-check in front allows only matching types, but the error message is not returned when it is executed the first time, that is numint(5,1,2,20) returns itself on the first run.
You can easily modify it to compute via Simpsons rule in using a fifth Parameter: mode and then use some if construction.

RE: Trapezoidal Rule or Simpsons Rule? - DrD - 07-07-2015 10:15 PM

Well done! There's lots of good info in your approach, and I appreciate your contribution.


I was puttering around also, and made a request (to Tim) to enhance calling parameters to allow specifying the TYPE of input like this:

Export Trap(f[8],a[0],b[0]n[0]), etc. We'll see if that ever see's the light of day!

This way the direct entry of a function (TYPE [8]) would be possible. To keep things non-CAS this approach also works:


//  Composite Trapezoidal Rule
//  Inputs:  f=function to solve, USE ""!
//           a:=start of interval
//           b:=end of interval
//           n:= nbr of steps in interval
//  Returns: Trapezoidal function value in R

EXPORT trap(f,a,b,n)
  local h,x;
  R:=(h/2) * ( 2*sum(F1(x)) - F1(x(2)) - F1(x(n+1)) ) ;
 return R;

RE: Trapezoidal Rule or Simpsons Rule? - Arno K - 07-08-2015 07:39 AM

Yes, your non-CAS approach is good, too. I like to avoid side-effects,so I tend to use local vars instead of globals, so that things I used/prepared and forgot their position (was it F1 or F2?) keep intact.

RE: Trapezoidal Rule or Simpsons Rule? - DrD - 07-08-2015 10:46 AM

It's nice to have choices. I'm finding that I prefer to use the built-in globals, more so, lately. Not only do they make programs more efficient, but the more they get used, the easier it is for me to remember to clear them, before next use. Another advantage is that they are pre-TYPED, which alleviates that extra concern. In this case, I was using the contents of F1 for further activities, like the Function App, which came in handy for this; as well as for command line evaluations, for the many things I tried that didn't work so well!