Trapezoidal Rule or Simpsons Rule?
07-05-2015, 10:49 PM
Post: #1
 DrD Senior Member Posts: 1,132 Joined: Feb 2014
Trapezoidal Rule or Simpsons Rule?
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:

Code:
 EXPORT Trap(f,a,b,n) BEGIN   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  END;

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

If you can help, thanks!

-Dale-
07-06-2015, 12:33 AM
Post: #2
 roadrunner Senior Member Posts: 398 Joined: Jun 2015
RE: Trapezoidal Rule or Simpsons Rule?
Dr. D.

Have you seen this:

http://edspi31415.blogspot.com/2015/05/h...a-4th.html

it seems similar to what you want.

07-06-2015, 11:24 AM
Post: #3
 DrD Senior Member Posts: 1,132 Joined: Feb 2014
RE: Trapezoidal Rule or Simpsons Rule?
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)
BEGIN
END;

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.

-Dale-
07-07-2015, 05:55 PM (This post was last modified: 07-07-2015 08:19 PM by Arno K.)
Post: #4
 Arno K Senior Member Posts: 456 Joined: Mar 2015
RE: Trapezoidal Rule or Simpsons Rule?
Perhaps you want something like this:
Code:
 #cas numint(g,a,b,n):= BEGIN local l; IF (type(g)==DOM_SYMBOLIC  OR type(g)== DOM_FUNC)   THEN     l:=MAKELIST(X,X,a,b,(b-a)/n);      IF type(g)==DOM_SYMBOLIC THEN      g:=unapply(g,x);      END;    l:=apply(g,l);    return (2*ΣLIST(l)-g(a)-g(b))*(b-a)/(2*n);    ELSE return ("f must be symbolic or function!");   END; END; #end
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.
Arno
07-07-2015, 10:15 PM (This post was last modified: 07-08-2015 10:48 AM by DrD.)
Post: #5
 DrD Senior Member Posts: 1,132 Joined: Feb 2014
RE: Trapezoidal Rule or Simpsons Rule?
Well done! There's lots of good info in your approach, and I appreciate your contribution.

-Dale-

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:

Code:
 //  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) BEGIN   local h,x;   F1:=f;   h:=(b-a)/n;     x:=MAKELIST(X,X,a,b,1/n);   R:=(h/2) * ( 2*sum(F1(x)) - F1(x(2)) - F1(x(n+1)) ) ;  return R; END;
07-08-2015, 07:39 AM
Post: #6
 Arno K Senior Member Posts: 456 Joined: Mar 2015
RE: Trapezoidal Rule or Simpsons Rule?
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.
Arno
07-08-2015, 10:46 AM
Post: #7
 DrD Senior Member Posts: 1,132 Joined: Feb 2014
RE: Trapezoidal Rule or Simpsons Rule?
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!

-Dale-
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)