NUMERIC.TST 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. on errcont;
  2. bounds (x,x=(1 .. 2));
  3. bounds (2*x,x=(1 .. 2));
  4. bounds (x**3,x=(1 .. 2));
  5. bounds (x*y,x=(1 .. 2),y=(-1 .. 0));
  6. bounds (x**3+y,x=(1 .. 2),y=(-1 .. 0));
  7. bounds (x**3/y,{x=(1 .. 2),y=(-1 .. -0.5)});
  8. bounds (x**3/y,x=(1 .. 2),y=(-1 .. -0.5));
  9. % unbounded expression (pole at y=0)
  10. bounds (x**3/y,x=(1 .. 2),y=(-1 .. 0.5));
  11. on rounded;
  12. bounds(e**x,x=(1 .. 2));
  13. bounds((1/2)**x,x=(1 .. 2));
  14. off rounded;
  15. bounds(abs x,x=(1 .. 2));
  16. bounds(abs x,x=(-3 .. 2));
  17. bounds(abs x,x=(-3 .. -2));
  18. bounds(sin x,x=(1 .. 2));
  19. on rounded;
  20. bounds(sin x,x=(1 .. 2));
  21. bounds(sin x,x=(1 .. 10));
  22. bounds(sin x,x=(1001 .. 1002));
  23. bounds(log x,x=(1 .. 10));
  24. bounds(tan x,x=(1 .. 1.1));
  25. bounds(cot x,x=(1 .. 1.1));
  26. bounds(asin x,x=(-0.6 .. 0.6));
  27. bounds(acos x,x=(-0.6 .. 0.6));
  28. bounds(sqrt(x),x=(1 .. 1.1));
  29. bounds(x**(7/3),x=(1 .. 1.1));
  30. bounds(x**y,x=(1 .. 1.1),y=(2 .. 4));
  31. off rounded;
  32. % MINIMA (steepest descent)
  33. % Rosenbrock function (minimum extremely hard to find).
  34. fktn := 100*(x1^2-x2)^2 + (1-x1)^2;
  35. num_min(fktn, x1=-1.2, x2=1, accuracy=6);
  36. % infinitely many local minima
  37. num_min(sin(x)+x/5, x=1);
  38. % bivariate polynomial
  39. num_min(x^4 + 3 x^2 * y + 5 y^2 + x + y, x=0.1, y=0.2);
  40. % ROOTS (non polynomial: damped Newton)
  41. num_solve (cos x -x, x=0,accuracy=6);
  42. % automatically randomized starting point
  43. num_solve (cos x -x,x, accuracy=6);
  44. % syntactical errors: forms do not evaluate to purely
  45. % numerical values
  46. num_solve (cos x -x, x=a);
  47. num_solve (cos x -a, x=0);
  48. num_solve (sin x = 0, x=3);
  49. % blows up: no real solution exists
  50. num_solve(sin x = 2, x=1);
  51. % solution in complex plane(only fond with complex starting point):
  52. on complex;
  53. num_solve(sin x = 2, x=1+i);
  54. off complex;
  55. % blows up for derivative 0 in starting point
  56. num_solve(x^2-1, x=0);
  57. % succeeds because of perturbed starting point
  58. num_solve(x^2-1, x=0.1);
  59. % bivariate equation system
  60. num_solve({sin x=cos y, x + y = 1},{x=1,y=2});
  61. on rounded,evallhseqp;
  62. sub(ws,{sin x=cos y, x + y = 1});
  63. off rounded,evallhseqp;
  64. % temporal member of the Barry Simon test sequence
  65. sys :={sin (x) + y^2 + log(z) = 7,
  66. 3*x + 2^y - z^3 = -90,
  67. x^2 + y^2 + z^(1/2) = 16};
  68. sol:=num_solve(sys,{x=1,y=1,z=1});
  69. on rounded;
  70. for each s in sys collect sub(sol,lhs s-rhs s);
  71. off rounded;
  72. clear sys,sol;
  73. % 2 examples taken from Nowak/Weimann (Tech.Rep TR91-10, ZIB Berlin)
  74. % #1: exp/sin combination
  75. on rounded;
  76. sys := {e**(x1**2 + x2**2)-3, x1 + x2 - sin(3(x1 + x2))};
  77. num_solve(sys,x1=0.81, x2=0.82);
  78. sub(ws,sys);
  79. % 2nd example (semiconductor simulation), here computed with
  80. % intermediate steps printed
  81. alpha := 38.683;
  82. ni := 1.22e10;
  83. v := 100;
  84. d := 1e17;
  85. sys := { e**(alpha*(x3-x1)) - e**(alpha*(x1-x2)) - d/ni,
  86. x2,
  87. x3,
  88. e**(alpha*(x6-x4)) - e**(alpha*(x4-x5)) + d/ni,
  89. x5 - v,
  90. x6 - v};
  91. on trnumeric;
  92. num_solve(sys,x1=1,x2=2,x3=3,x4=4,x5=5,x6=6,iterations=100);
  93. off trnumeric;
  94. clear alpha,ni,v,d,sys;
  95. off rounded;
  96. % INTEGRALS
  97. num_int( x**2,x=(1 .. 2),accuracy=3);
  98. % 1st case: using formal integral
  99. needle := 1/(10**-4 + x**2);
  100. num_int(needle,x=(-1 .. 1),accuracy=3); % 312.16
  101. % no formal integral, but easy Chebyshev fit
  102. num_int(sin x/x,x=(1 .. 10));
  103. % using a Chebyshev fit of order 60
  104. num_int(exp(-x**2),x=(-10 .. 10),accuracy=3); % 1.772
  105. % cases with singularities
  106. num_int(1/sqrt x ,x=(0 .. 1),accuracy=2); % 1.999
  107. num_int(1/sqrt abs x ,x=(-1 .. 1),iterations=50); % 3.999
  108. % simple multidimensional integrals
  109. num_int(x+y,x=(0 .. 1),y=(2 .. 3));
  110. num_int(sin(x+y),x=(0 .. 1),y=(0 .. 1));
  111. % some integrals with infinite bounds
  112. on rounded; % for the error function
  113. num_int(e^(-x) ,x=(0 .. infinity)); % 1.000
  114. 2/sqrt(pi)* num_int(e^(-x^2) ,x=(0 .. infinity)); % 1.00
  115. 2/sqrt(pi)* num_int(e^(-x^2), x=(-infinity .. infinity)); % 2.00
  116. num_int(sin(x) * e^(-x), x=(0 .. infinity)); % 0.500
  117. off rounded;
  118. % APPROXIMATION
  119. %approximate sin x by a cubic polynomial
  120. num_fit(sin x,{1,x,x**2,x**3},x=for i:=0:20 collect 0.1*i);
  121. % approximate x**2 by a harmonic series in the interval [0,1]
  122. num_fit(x**2,1 . for i:=1:5 join {sin(i*x)},
  123. x=for i:=0:10 collect i/10);
  124. % approximate a set of points by a polynomial
  125. pts:=for i:=1 step 0.1 until 3 collect i$
  126. vals:=for each p in pts collect (p+2)**3$
  127. num_fit(vals,{1,x,x**2,x**3},x=pts);
  128. % compute the approximation error
  129. on rounded;
  130. first ws - (x+2)**3;
  131. off rounded;
  132. % ODE SOLUTION (Runge-Kutta)
  133. depend(y,x);
  134. % approximate y=y(x) with df(y,x)=2y in interval [0 : 5]
  135. num_odesolve(df(y,x)=y,y=2,x=(0 .. 5),iterations=20);
  136. % same with negative direction
  137. num_odesolve(df(y,x)=y,y=2,x=(0 .. -5),iterations=20);
  138. % giving a nice picture when plotted
  139. num_odesolve(df(y,x)=1- x*y**2 ,y=0,x=(0 .. 4),iterations=20);
  140. % system of ordinary differential equations
  141. depend(y,x);
  142. depend(z,x);
  143. num_odesolve(
  144. {df(z,x) = y, df(y,x)= y+x},
  145. {z=2, y=4},
  146. x=(0 .. 5),iterations=20);
  147. %----------------- Chebyshev fit -------------------------
  148. on rounded;
  149. func := x**2 * (x**2 - 2) * sin x;
  150. ord := 15;
  151. cx:=chebyshev_fit(func,x=(0 .. 2),ord)$
  152. cp:=first cx;
  153. cc:=second cx;
  154. for u:=0 step 0.2 until 2 do write
  155. "x:",u," true value:",sub(x=u,func),
  156. " Chebyshev eval:", chebyshev_eval(cc,x=(0 .. 2),x=u),
  157. " Chebyshev polynomial:",sub(x=u,cp);
  158. % integral
  159. % integrate coefficients
  160. ci := chebyshev_int(cc,x=(0 .. 2));
  161. % compare with true values (normalized absolute term)
  162. ci0:=chebyshev_eval(ci,x=(0 .. 2),x=0)$
  163. ifunc := int(func,x)$ if0 := sub(x=0,ifunc);
  164. for u:=0 step 0.2 until 2 do write
  165. {u,sub(x=u,ifunc) - if0,
  166. chebyshev_eval(ci,x=(0 .. 2),x=u) - ci0};
  167. % derivative
  168. % differentiate coefficients
  169. cd := chebyshev_df(cc,x=(0 .. 2))$
  170. % compute coefficients of derivative
  171. cds := second chebyshev_fit(df(func,x),x=(0 .. 2),ord)$
  172. % compare coefficients
  173. for i:=1:ord do write {part(cd,i),part(cds,i)};
  174. clear func,ord,cc,cx,cd,cds,ci,ci0;
  175. off rounded;
  176. end;