2 /* Numerical Integration Demo for QUANC8*/
4 /* SETUP_AUTOLOAD(QQ,QUANC8); */
6 /* show the time and the garbage collection time, and use the
7 GC-demon to make better use of flonum space */
10 /* IF STATUS(FEATURE,ITS)=TRUE THEN LOAD("GCDEMN"); */
12 /* Sample highly oscillatory problem */
14 g(x):=(mode_declare(x,float),sin(1.0/x));
16 /* inefficient way to use QUANC8 */
18 quanc8(g(x),x,0.01,0.1);
20 /* give G as the thing to compile, then a semi-colon to read it in */
23 /* see how much faster now, using fast version of QUANC8 */
27 /* note that romberg doesn't easily manage oscillatory behaviors */
29 errcatch(romberg('g,0.01,0.1));
31 /* a function with two large and narrow peaks,
32 so hard to integrate with a fixed step-size */
34 h(x):=(mode_declare(x,float),1.0/(.0001+(x-.3)^2)+1.0/(.0025+(x-.9)^2));
35 /* give H and a semi-colon in response */
38 /* the exact answer for integrate(h(x),x) is
39 100.0*atan(100.0*x-30.0)+20.0*atan(20.0*x-18.0) */
40 /* the exact answer for integrate(h(x),x,0.0,1.0) is 361.847622.
41 note that the romberg result is more accurate in this case */
43 /* but at a large time cost compared to adaptive */
45 /* reduce QUANC8's relative error tolerance by a factor of 10 */
47 /* does not take much longer, and gives much better result now */
49 /* put QUANC8_RELERR back to 1.0e-4 */
51 /* the exact answer for integrate(h(x),x,0.0,10.0) is 372.33606.
52 while too tough for fixed step size method */
53 errcatch(romberg('h,0.0,10.0));
54 /*QUANC8 is still super speedy even now */
57 /* double integral of sample function exp(x)*sin(y)
58 from x=0.0 to 2.0, y=0.0 to 1.0 */
59 /* you must be sure that the quanc8 source is loaded
60 (load("qq");) before you translate a call to quanc8 in a function,
61 else an error in macro expansion will occur when it can't figure
62 out if the 3 or 4 arg version is used */
64 /* the exact answer for
65 integrate(integrate(exp(x)*sin(y),x,0.0,2.0),y,0.0,1.0) is 2.93703434.
66 integrate over y to get the final answer */
67 quanc8(quanc8(exp(x)*sin(y),x,0.0,2.0),y,0.0,1.0);
69 p():=quanc8(lambda([y],mode_declare(y,float),
70 quanc8(lambda([x],mode_declare(x,float),
77 /* give p and a semi-colon */