numeric.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. NUMERIC
  2. Herbert Melenk
  3. Konrad--Zuse--Zentrum fuer Informationstechnik Berlin
  4. E--mail: Melenk@sc.zib--berlin.de
  5. The NUMERIC package implements some numerical (approximative)
  6. algorithms for REDUCE, based on the REDUCE rounded mode arithmetic.
  7. These algorithms are implemented for standard cases. They should
  8. not be called for ill-conditioned problems; please use standard
  9. mathematical libraries for these.
  10. 1 Syntax
  11. 1.1 Intervals, Starting Points
  12. Intervals are generally coded as lower bound and upper bound
  13. connected by the operator `..', usually associated to a variable in
  14. an equation. E.g.
  15. x= (2.5 .. 3.5)
  16. means that the variable x is taken in the range from 2.5 up to 3.5.
  17. Note, that the bounds can be algebraic expressions, which, however,
  18. must evaluate to numeric results. In cases where an interval is
  19. returned as the result, the lower and upper bounds can be extracted
  20. by the PART operator as the first and second part respectively. A
  21. starting point is specified by an equation with a numeric righthand
  22. side, e.g.
  23. x=3.0
  24. If for multivariate applications several coordinates must be
  25. specified by intervals or as a starting point, these specifications
  26. 1
  27. 2 MINIMA 2
  28. can be collected in one parameter (which is then a list) or they
  29. can be given as separate parameters alternatively. The list form
  30. is more appropriate when the parameters are built from other REDUCE
  31. calculations in an automatical style, while the flat form is more
  32. convenient for direct interactive input.
  33. 1.2 Accuracy Control
  34. The keyword parameters accuracy=a and iterations=i, where a and i
  35. must be positive integer numbers, control the iterative algorithms:
  36. -a
  37. the iteration is continued until the local error is below 10 ; if
  38. that is impossible within i steps, the iteration is terminated with
  39. an error message. The values reached so far are then returned as
  40. the result.
  41. 1.3 tracing
  42. Normally the algorithms produce only a minimum of printed output
  43. during their operation. In cases of an unsuccessful or unexpected
  44. long operation a trace of the iteration can be printed by setting
  45. on trnumeric;
  46. 2 Minima
  47. The Fletcher Reeves version of the steepest descent algorithms is
  48. used to find the minimum of a function of one or more variables.
  49. The function must have continuous partial derivatives with respect
  50. to all variables. The starting point of the search can be
  51. specified; if not, random values are taken instead. The steepest
  52. descent algorithms in general find only local minima.
  53. Syntax:
  54. NUMMIN (exp,var [=val ][,var [=val ]...]
  55. 1 1 2 2
  56. [,accuracy=a][,iterations=i])
  57. or
  58. 3 ROOTS OF FUNCTIONS/ SOLUTIONS OF EQUATIONS 3
  59. NUMMIN (exp,{var [=val ][,var [=val ]...]}
  60. 1 1 2 2
  61. [,accuracy=a][,iterations=i])
  62. where exp is a function expression,
  63. var ,var ,... are the variables in exp and val ,val ,... are
  64. 1 2 1 2
  65. the (optional) start values.
  66. MIN tries to find the next local minimum along the descending
  67. path starting at the given point. The result is a list
  68. with the minimum function value as first element followed by
  69. a list of equations, where the variables are equated to the
  70. coordinates of the result point.
  71. Examples:
  72. num_min(sin(x)+x/5, x);
  73. {4.9489585606,{X=29.643767785}}
  74. num_min(sin(x)+x/5, x=0);
  75. { - 1.3342267466,{X= - 1.7721582671}}
  76. % Rosenbrock function (well known as hard to minimize).
  77. fktn := 100*(x1**2-x2)**2 + (1-x1)**2;
  78. num_min(fktn, x1=-1.2, x2=1, iterations=200);
  79. {0.00000021870228295,{X1=0.99953284494,X2=0.99906807238}}
  80. 3 Roots of Functions/ Solutions of Equations
  81. An adaptively damped Newton iteration is used to find an
  82. approximative zero of a function, a function vector or the solution
  83. of an equation or an equation system. Equations are internally
  84. converted to a difference of lhs and rhs such that the Newton
  85. method (=zero detection) can be applied. The expressions must have
  86. continuous derivatives for all variables. A starting point for the
  87. iteration can be given. If not given, random values are taken
  88. 3 ROOTS OF FUNCTIONS/ SOLUTIONS OF EQUATIONS 4
  89. instead. If the number of forms is not equal to the number of
  90. variables, the Newton method cannot be applied. Then the minimum of
  91. the sum of absolute squares is located instead.
  92. With ON COMPLEX solutions with imaginary parts can be found, if
  93. either the expression(s) or the starting point contain a nonzero
  94. imaginary part.
  95. Syntax:
  96. NUM_SOLVE (exp ,var [=val ][,accuracy=a][,iterations=i])
  97. or 1 1 1
  98. NUM_SOLVE ({exp ,... ,exp },var [=val ],... ,var [=val ]
  99. 1 n 1 1 1 n
  100. [,accuracy=a][,iterations=i])
  101. or
  102. NUM_ SOLVE ({exp ,... ,exp },{var [=val ],... ,var [=val ]}
  103. 1 n 1 1 1 n
  104. [,accuracy=a][,iterations=i])
  105. where exp ,... ,exp are function expressions,
  106. 1 n
  107. var ,... ,var are the variables,
  108. 1 n
  109. val ,... ,val are optional start values.
  110. 1 n
  111. SOLVE tries to find a zero/solution of the expression(s).
  112. Result is a list of equations, where the variables are equated
  113. to the coordinates of the result point.
  114. The Jacobian matrix is stored as side effect the shared
  115. variable JACOBIAN.
  116. Example:
  117. num_solve({sin x=cos y, x + y = 1},{x=1,y=2});
  118. {X= - 1.8561957251,Y=2.856195584}
  119. jacobian;
  120. [COS(X) SIN(Y)]
  121. [ ]
  122. 4 INTEGRALS 5
  123. [ 1 1 ]
  124. 4 Integrals
  125. For the numerical evaluation of univariate integrals over a finite
  126. interval the following strategy is used:
  127. 1. If the function has an antiderivative in close form which is
  128. bounded in the integration interval, this is used.
  129. 2. Otherwise a Chebyshev approximation is computed, starting with
  130. order 20, eventually up to order 80. If that is recognized as
  131. sufficiently convergent it is used for computing the integral
  132. by directly integrating the coefficient sequence.
  133. 3. If none of these methods is successful, an adaptive multilevel
  134. quadrature algorithm is used.
  135. For multivariate integrals only the adaptive quadrature is used.
  136. This algorithm tolerates isolated singularities. The value
  137. iterations here limits the number of local interval intersection
  138. levels. Accuracy is a measure for the relative total discretization
  139. error (comparison of order 1 and order 2 approximations).
  140. Syntax:
  141. NUMINT (exp,var =(l ..u )[,var =(l ..u )... ]
  142. 1 1 1 2 2 2
  143. [,accuracy=a][,iterations=i])
  144. where exp is the function to be integrated,
  145. var ,var ,... are the integration variables,
  146. 1 2
  147. l ,l ,... are the lower bounds,
  148. 1 2
  149. u ,u ,... are the upper bounds.
  150. 1 2
  151. Result is the value of the integral.
  152. Example:
  153. num_int(sin x,x=(0 .. pi));
  154. 2.0000010334
  155. 5 ORDINARY DIFFERENTIAL EQUATIONS 6
  156. 5 Ordinary Differential Equations
  157. A Runge-Kutta method of order 3 finds an approximate graph for
  158. the solution of a ordinary differential equation real initial value
  159. problem.
  160. Syntax:
  161. NUMODESOLVE (exp,depvar=dv,indepvar=(from..to)
  162. [,accuracy=a][,iterations=i])
  163. where
  164. exp is the differential expression/equation,
  165. depvar is an identifier representing the dependent variable
  166. (function to be found),
  167. indepvar is an identifier representing the independent
  168. variable,
  169. exp is an equation (or an expression implicitly set to zero)
  170. which contains the first derivative of depvar wrt indepvar,
  171. from is the starting point of integration,
  172. to is the endpoint of integration (allowed to be below from),
  173. dv is the initial value of depvar in the point indepvar=from.
  174. The ODE exp is converted into an explicit form, which then is
  175. used for a Runge Kutta iteration over the given range. The
  176. number of steps is controlled by the value of i (default: 20).
  177. If the steps are too coarse to reach the desired accuracy in
  178. the neighborhood of the starting point, the number is increased
  179. automatically.
  180. Result is a list of pairs, each representing a point of the
  181. approximate solution of the ODE problem.
  182. Example:
  183. num_odesolve(df(y,x)=y,y=1,x=(0 .. 1), iterations=5);
  184. {{0.0,1.0},{0.2,1.2214},{0.4,1.49181796},{0.6,1.8221064563},
  185. 6 BOUNDS OF A FUNCTION 7
  186. {0.8,2.2255208258},{1.0,2.7182511366}}
  187. Remarks:
  188. -- If in exp the differential is not isolated on the lefthand
  189. side, please ensure that the dependent variable is explicitly
  190. declared using a DEPEND statement, e.g.
  191. depend y,x;
  192. otherwise the formal derivative will be computed to zero by
  193. REDUCE.
  194. -- The REDUCE package SOLVE is used to convert the form into an
  195. explicit ODE. If that process fails or has no unique result,
  196. the evaluation is stopped with an error message.
  197. 6 Bounds of a Function
  198. Upper and lower bounds of a real valued function over an interval
  199. or a rectangular multivariate domain are computed by the operator
  200. BOUNDS. The algorithmic basis is the computation with inequalities:
  201. starting from the interval(s) of the variables, the bounds are
  202. propagated in the expression using the rules for inequality
  203. computation. Some knowledge about the behavior of special functions
  204. like ABS, SIN, COS, EXP, LOG, fractional exponentials etc. is
  205. integrated and can be evaluated if the operator BOUNDS is called
  206. with rounded mode on (otherwise only algebraic evaluation rules are
  207. available).
  208. If BOUNDS finds a singularity within an interval, the evaluation is
  209. stopped with an error message indicating the problem part of the
  210. expression.
  211. Syntax:
  212. BOUNDS (exp,var =(l ..u )[,var =(l ..u )...])
  213. 1 1 1 2 2 2
  214. BOUNDS (exp,{var =(l ..u )[,var =(l ..u )...]})
  215. 1 1 1 2 2 2
  216. where exp is the function to be investigated,
  217. 7 CHEBYSHEV CURVE FITTING 8
  218. var ,var ,... are the variables of exp,
  219. 1 2
  220. l ,l ,... and u ,u ,... specify the area (intervals).
  221. 1 2 1 2
  222. BOUNDS computes upper and lower bounds for the expression in
  223. the given area. An interval is returned.
  224. Example:
  225. bounds(sin x,x=(1 .. 2));
  226. {-1,1}
  227. on rounded;
  228. bounds(sin x,x=(1 .. 2));
  229. 0.84147098481 .. 1
  230. bounds(x**2+x,x=(-0.5 .. 0.5));
  231. - 0.25 .. 0.75
  232. 7 Chebyshev Curve Fitting
  233. The operator family Chebyshev-... implements approximation and
  234. (a,b)
  235. evaluation of functions by the Chebyshev method. Let T (x)
  236. be the Chebyshev polynomial of order n transformed to the ninterval
  237. (a,b). Then a function f(x) can be approximated in (a,b) by a
  238. series
  239. N (a,b)
  240. f(x)?si=0 c T (x)
  241. i i
  242. The operator Chebyshev-fit computes this approximation and returns a
  243. list, which has as first element the sum expressed as a polynomial
  244. 7 CHEBYSHEV CURVE FITTING 9
  245. and as second element the sequence of Chebyshev coefficients c .
  246. i
  247. Chebyshevdf and Chebyshevint transform a Chebyshev coefficient list
  248. into the coefficients of the corresponding derivative or integral
  249. respectively. For evaluating a Chebyshev approximation at a given
  250. point in the basic interval the operator Chebysheveval can be used.
  251. Note that Chebyshev-eval is based on a recurrence relation which is
  252. in general more stable than a direct evaluation of the complete
  253. polynomial.
  254. CHEBYSHEVFIT (fcn,var=(lo..hi),n)
  255. CHEBYSHEVEVAL (coeffs,var=(lo..hi),var=pt)
  256. CHEBYSHEVDF (coeffs,var=(lo..hi))
  257. CHEBYSHEVINT (coeffs,var=(lo..hi))
  258. where fcn is an algebraic expression (the function to be
  259. fitted), var is the variable of fcn, lo and hi are numerical
  260. real values which describe an interval (lo<hi), n is the
  261. approximation order,an integer >0, set to 20 if missing, pt is
  262. a numerical value in the interval and coeffs is a series of
  263. Chebyshev coefficients, computed by one of CHEBYSHEVCOEFF, -DF
  264. or -INT.
  265. Example:
  266. on rounded;
  267. w:=chebyshev_fit(sin x/x,x=(1 .. 3),5);
  268. 3 2
  269. w := {0.03824*x - 0.2398*x + 0.06514*x + 0.9778,
  270. {0.8991,-0.4066,-0.005198,0.009464,-0.00009511}}
  271. chebyshev_eval(second w, x=(1 .. 3), x=2.1);
  272. 0.4111
  273. 8 GENERAL CURVE FITTING 10
  274. 8 General Curve Fitting
  275. The operator NUMFIT finds for a set of points the linear combination
  276. of a given set of functions (function basis) which approximates
  277. the points best under the objective of the least squares criterion
  278. (minimum of the sum of the squares of the deviation). The solution
  279. is found as zero of the gradient vector of the sum of squared
  280. errors.
  281. Syntax:
  282. NUMFIT (vals,basis,var=pts)
  283. where vals is a list of numeric values,
  284. var is a variable used for the approximation,
  285. pts is a list of coordinate values which correspond to var,
  286. basis is a set of functions varying in var which is used for
  287. the approximation.
  288. The result is a list containing as first element the function which
  289. approximates the given values, and as second element a list of
  290. coefficients which were used to build this function from the basis.
  291. Example:
  292. % approximate a set of factorials by a polynomial
  293. pts:=for i:=1 step 1 until 5 collect i$
  294. vals:=for i:=1 step 1 until 5 collect
  295. for j:=1:i product j$
  296. num_fit(vals,{1,x,x**2},x=pts);
  297. 2
  298. {14.571428571*X - 61.428571429*X + 54.6,{54.6,
  299. - 61.428571429,14.571428571}}
  300. num_fit(vals,{1,x,x**2,x**3,x**4},x=pts);
  301. 4 3
  302. 8 GENERAL CURVE FITTING 11
  303. {2.2083333234*X - 20.249999879*X
  304. 2
  305. + 67.791666154*X - 93.749999133*X
  306. + 44.999999525,
  307. {44.999999525, - 93.749999133,67.791666154,
  308. - 20.249999879,2.2083333234}}