Post Reply 
Trapezoidal Rule or Simpsons Rule?
07-07-2015, 05:55 PM (This post was last modified: 07-07-2015 08:19 PM by Arno K.)
Post: #4
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Trapezoidal Rule or Simpsons Rule? - DrD - 07-05-2015, 10:49 PM
RE: Trapezoidal Rule or Simpsons Rule? - Arno K - 07-07-2015 05:55 PM



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