pk-numer.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. \section{Numeric Package}
  2. \begin{Introduction}{Numeric Package}
  3. The numeric package supplies algorithms based on approximation
  4. techniques of numerical mathematics. The algorithms use
  5. the \nameref{rounded} mode arithmetic of REDUCE, including
  6. the variable precision feature which is exploited in some
  7. algorithms in an adaptive manner in order to reach the
  8. desired accuracy.
  9. \end{Introduction}
  10. \begin{Type}{Interval}
  11. Intervals are generally coded as lower bound and
  12. upper bound connected by the operator \name{..}, usually
  13. associated to a variable in an
  14. equation.
  15. \begin{Syntax}
  16. \meta{var} = \(\meta{low} .. \meta{high}\)
  17. \end{Syntax}
  18. where \meta{var} is a \nameref{kernel} and \meta{low}, \meta{high} are
  19. numbers or expression which evaluate to numbers with \meta{low}<=\meta{high}.
  20. \begin{Examples}
  21. x= (2.5 .. 3.5)
  22. \end{Examples}
  23. means that the variable x is taken in the range from 2.5 up to
  24. 3.5.
  25. \end{Type}
  26. \begin{Concept}{numeric accuracy}
  27. The keyword parameters \name{accuracy=a} and \name{iterations=i},
  28. where \name{a}and \name{i} must be positive integer numbers, control the
  29. iterative algorithms: the iteration is continued until
  30. the local error is below 10**{-a}; if that is impossible
  31. within \name{i} steps, the iteration is terminated with an
  32. error message. The values reached so far are then returned
  33. as the result.
  34. \end{Concept}
  35. \begin{Switch}{TRNUMERIC}
  36. Normally the algorithms produce only a minimum of printed
  37. output during their operation. In cases of an unsuccessful
  38. or unexpected long operation a \name{trace of the iteration} can be
  39. printed by setting \name{trnumeric} \name{on}.
  40. \end{Switch}
  41. \begin{Operator}{num_min}
  42. \index{minimum}\index{steepest descent}\index{Fletcher Reeves}
  43. The Fletcher Reeves version of the \name{steepest descent}
  44. algorithms is used to find the \name{minimum} of a
  45. function of one or more variables. The
  46. function must have continuous partial derivatives with respect to all
  47. variables. The starting point of the search can be
  48. specified; if not, random values are taken instead.
  49. The steepest descent algorithms in general find only local
  50. minima.
  51. \begin{Syntax}
  52. \name{num\_min}\(\meta{exp},
  53. \meta{var}[=\meta{val}] [,\meta{var}[=\meta{val}] ...
  54. [,accuracy=\meta{a}] [,iterations=\meta{i}]\)
  55. or
  56. \name{num\_min}\(exp, \{
  57. \meta{var}[=\meta{val}] [,\meta{var}[=\meta{val}] ...] \}
  58. [,accuracy=\meta{a}] [,iterations=\meta{i}]\)
  59. \end{Syntax}
  60. where \meta{exp} is a function expression,
  61. \meta{var} are the variables in \meta{exp} and
  62. \meta{val} are the (optional) start values.
  63. For \meta{a} and \meta{i} see \nameref{numeric accuracy}.
  64. \name{Num\_min} tries to find the next local minimum along the descending
  65. path starting at the given point. The result is a \nameref{list}
  66. with the minimum function value as first element followed by a list
  67. of \nameref{equation}\name{s}, where the variables are equated to the coordinates
  68. of the result point.
  69. \begin{Examples}
  70. num_min(sin(x)+x/5, x)&\{4.9489585606,\{X=29.643767785\}\}\\
  71. num_min(sin(x)+x/5, x=0)&\{ - 1.3342267466,\{X= - 1.7721582671\}\}
  72. \end{Examples}
  73. \end{Operator}
  74. \begin{Operator}{num_solve}
  75. \index{equation solving}\index{equation system}\index{Newton iteration}
  76. \index{root}
  77. An adaptively damped Newton iteration is used to find
  78. an approximative root of a function (function vector) or the
  79. solution of an \nameref{equation} (equation system). The expressions
  80. must have continuous derivatives for all variables.
  81. A starting point for the iteration can be given. If not given
  82. random values are taken instead. When the number of
  83. forms is not equal to the number of variables, the
  84. Newton method cannot be applied. Then the minimum
  85. of the sum of absolute squares is located instead.
  86. With \nameref{complex} on, solutions with imaginary parts can be
  87. found, if either the expression(s) or the starting point
  88. contain a nonzero imaginary part.
  89. \begin{Syntax}
  90. \name{num\_solve}\(\meta{exp}, \meta{var}[=\meta{val}][,accuracy=\meta{a}][,iterations=\meta{i}]\)
  91. or
  92. \name{num\_solve}\(\{\meta{exp},...,\meta{exp}\}, \meta{var}[=\meta{val}],...,\meta{var}[=\meta{val}]
  93. [,accuracy=\meta{a}][,iterations=\meta{i}]\)
  94. or
  95. \name{num\_solve}\(\{\meta{exp},...,\meta{exp}\}, \{\meta{var}[=\meta{val}],...,\meta{var}[=\meta{val}]\}
  96. [,accuracy=\meta{a}][,iterations=\meta{i}]\)
  97. \end{Syntax}
  98. where \meta{exp} are function expressions,
  99. \meta{var} are the variables,
  100. \meta{val} are optional start values.
  101. For \meta{a} and \meta{i} see \nameref{numeric accuracy}.
  102. \name{num_solve} tries to find a zero/solution of the expression(s).
  103. Result is a list of equations, where the variables are
  104. equated to the coordinates of the result point.
  105. The \nameindex{Jacobian matrix} is stored as side effect the shared
  106. variable \name{jacobian}.
  107. \begin{Examples}
  108. num_solve({sin x=cos y, x + y = 1},{x=1,y=2});&
  109. \{X= - 1.8561957251,Y=2.856195584\}\\
  110. jacobian;&
  111. \begin{multilineoutput}{5cm}
  112. [COS(X) SIN(Y)]
  113. [ ]
  114. [ 1 1 ]
  115. \end{multilineoutput}
  116. \end{Examples}
  117. \end{Operator}
  118. \begin{Operator}{num_int}
  119. \index{integration}
  120. For the numerical evaluation of univariate integrals
  121. over a finite interval the following strategy is used:
  122. If \nameref{int} finds a formal antiderivative
  123. which is bounded in the integration interval, this
  124. is evaluated and the end points and the difference
  125. is returned.
  126. Otherwise a \nameref{Chebyshev fit} is computed,
  127. starting with order 20, eventually up to order 80.
  128. If that is recognized as sufficiently convergent
  129. it is used for computing the integral by directly
  130. integrating the coefficient sequence.
  131. If none of these methods is successful, an
  132. adaptive multilevel quadrature algorithm is used.
  133. For multivariate integrals only the adaptive quadrature is used.
  134. This algorithm tolerates isolated singularities.
  135. The value \name{iterations} here limits the number of
  136. local interval intersection levels.
  137. \meta{a} is a measure for the relative total discretization
  138. error (comparison of order 1 and order 2 approximations).
  139. \begin{Syntax}
  140. \name{num_int}\(\meta{exp},\meta{var}=\(\meta{l} .. \meta{u}\)
  141. [,\meta{var}=\(\meta{l} .. \meta{u}\),...]
  142. [,accuracy=\meta{a}][,iterations=\meta{i}]\)
  143. \end{Syntax}
  144. where \meta{exp} is the function to be integrated,
  145. \meta{var} are the integration variables,
  146. \meta{l} are the lower bounds,
  147. \meta{u} are the upper bounds.
  148. Result is the value of the integral.
  149. \begin{Examples}
  150. num_int(sin x,x=(0 .. 3.1415926));& 2.0000010334
  151. \end{Examples}
  152. \end{Operator}
  153. \begin{Operator}{num_odesolve}
  154. \index{Runge-Kutta}\index{initial value problem}\index{ODE}
  155. The \name{Runge-Kutta} method of order 3 finds an approximate graph for
  156. the solution of real \name{ODE initial value problem}.
  157. \begin{Syntax}
  158. \name{num_odesolve}\(\meta{exp},\meta{depvar}=\meta{start},
  159. \meta{indep}=\(\meta{from} .. \meta{to}\)
  160. [,accuracy=\meta{a}][,iterations=\meta{i}]\)
  161. or
  162. \name{num_odesolve}\(\{\meta{exp},\meta{exp},...\},
  163. \{ \meta{depvar}=\meta{start},\meta{depvar}=\meta{start},...\}
  164. \meta{indep}=\(\meta{from} .. \meta{to}\)
  165. [,accuracy=\meta{a}][,iterations=\meta{i}]\)
  166. \end{Syntax}
  167. where
  168. \meta{depvar} and \meta{start} specify the dependent variable(s)
  169. and the starting point value (vector),
  170. \meta{indep}, \meta{from} and \meta{to} specify the independent variable
  171. and the integration interval (starting point and end point),
  172. \meta{exp} are equations or expressions which
  173. contain the first derivative of the independent variable
  174. with respect to the dependent variable.
  175. The ODEs are converted to an explicit form, which then is
  176. used for a Runge Kutta iteration over the given range. The
  177. number of steps is controlled by the value of \meta{i}
  178. (default: 20). If the steps are too coarse to reach the desired
  179. accuracy in the neighborhood of the starting point, the number is
  180. increased automatically.
  181. Result is a list of pairs, each representing a point of the
  182. approximate solution of the ODE problem.
  183. \begin{Examples}
  184. depend(y,x);\\
  185. num_odesolve(df(y,x)=y,y=1,x=(0 .. 1), iterations=5);&
  186. \begin{multilineoutput}
  187. \{\{0.0,1.0\},\{0.2,1.2214\},\{0.4,1.49181796\},\{0.6,1.8221064563\},
  188. \{0.8,2.2255208258\},\{1.0,2.7182511366\}\}\\
  189. \end{multilineoutput}
  190. \end{Examples}
  191. In most cases you must declare the dependency relation
  192. between the variables explicitly using \nameref{depend};
  193. otherwise the formal derivative might be converted to zero.
  194. The operator \nameref{solve} is used to convert the form into
  195. an explicit ODE. If that process fails or if it has no unique result,
  196. the evaluation is stopped with an error message.
  197. \end{Operator}
  198. \begin{Operator}{bounds}
  199. Upper and lower bounds of a real valued function over an
  200. \nameref{interval} or a rectangular multivariate domain are computed
  201. by the operator \name{bounds}. The algorithmic basis is the computation
  202. with inequalities: starting from the interval(s) of the
  203. variables, the bounds are propagated in the expression
  204. using the rules for inequality computation. Some knowledge
  205. about the behavior of special functions like ABS, SIN, COS, EXP, LOG,
  206. fractional exponentials etc. is integrated and can be evaluated
  207. if the operator \name{bounds} is called with rounded mode on
  208. (otherwise only algebraic evaluation rules are available).
  209. If \name{bounds} finds a singularity within an interval, the evaluation
  210. is stopped with an error message indicating the problem part
  211. of the expression.
  212. \begin{Syntax}
  213. \name{bounds}\(\meta{exp},\meta{var}=\(\meta{l} .. \meta{u}\)
  214. [,\meta{var}=\(\meta{l} .. \meta{u}\) ...]\)
  215. or
  216. \name{bounds}\(\meta{exp},\{\meta{var}=\(\meta{l} .. \meta{u}\)
  217. [,\meta{var}=\(\meta{l} .. \meta{u}\) ...]\}\)
  218. \end{Syntax}
  219. where \meta{exp} is the function to be investigated,
  220. \meta{var} are the variables of \meta{exp},
  221. \meta{l} and \meta{u} specify the area as set of \nameref{interval}\name{s}.
  222. \name{bounds} computes upper and lower bounds for the expression in the
  223. given area. An \nameref{interval} is returned.
  224. \begin{Examples}
  225. bounds(sin x,x=(1 .. 2));& -1 .. 1\\
  226. on rounded;\\
  227. bounds(sin x,x=(1 .. 2));& 0.84147098481 .. 1\\
  228. bounds(x**2+x,x=(-0.5 .. 0.5));& - 0.25 .. 0.75\\
  229. \end{Examples}
  230. \end{Operator}
  231. \begin{Concept}{Chebyshev fit}
  232. \index{approximation}
  233. The operator family \name{Chebyshev\_...} implements approximation
  234. and evaluation of functions by the Chebyshev method.
  235. Let \name{T(n,a,b,x)} be the Chebyshev polynomial of order \name{n}
  236. transformed to the interval \name{(a,b)}.
  237. Then a function \name{f(x)} can be
  238. approximated in \name{(a,b)} by a series
  239. \begin{verbatim}
  240. for i := 0:n sum c(i)*T(i,a,b,x)
  241. \end{verbatim}
  242. The operator \name{chebyshev\_fit} computes this approximation and
  243. returns a list, which has as first element the sum expressed
  244. as a polynomial and as second element the sequence
  245. of Chebyshev coefficients.
  246. \name{Chebyshev\_df} and \name{Chebyshev\_int} transform a Chebyshev
  247. coefficient list into the coefficients of the corresponding
  248. derivative or integral respectively. For evaluating a Chebyshev
  249. approximation at a given point in the basic interval the
  250. operator \name{Chebyshev\_eval} can be used.
  251. \name{Chebyshev\_eval} is based on a recurrence relation which is
  252. in general more stable than a direct evaluation of the
  253. complete polynomial.
  254. \begin{Syntax}
  255. \name{chebyshev\_fit}\(\meta{fcn},\meta{var}=\(\meta{lo} .. \meta{hi}\),\meta{n}\)
  256. \name{chebyshev\_eval}\(\meta{coeffs},\meta{var}=\(\meta{lo} .. \meta{hi}\),
  257. \meta{var}=\meta{pt}\)
  258. \name{chebyshev\_df}\(\meta{coeffs},\meta{var}=\(\meta{lo} .. \meta{hi}\)\)
  259. \name{chebyshev\_int}\(\meta{coeffs},\meta{var}=\(\meta{lo} .. \meta{hi}\)\)
  260. \end{Syntax}
  261. where \meta{fcn} is an algebraic expression (the target function),
  262. \meta{var} is the variable of \meta{fcn},
  263. \meta{lo} and \meta{hi} are
  264. numerical real values which describe an \nameref{interval} \meta{lo} < \meta{hi},
  265. the integer \meta{n} is the approximation order (set to 20 if missing),
  266. \meta{pt} is a number in the interval and \meta{coeffs} is
  267. a series of Chebyshev coefficients.
  268. \begin{Examples}
  269. on rounded;\\
  270. w:=chebyshev_fit(sin x/x,x=(1 .. 3),5);&
  271. \begin{multilineoutput}{6cm}
  272. w := \{0.03824*x^3 - 0.2398*x^2 + 0.06514*x + 0.9778,
  273. \{0.8991,-0.4066,-0.005198,0.009464,-0.00009511\}\}
  274. \end{multilineoutput}\\
  275. chebyshev_eval(second w, x=(1 .. 3), x=2.1);& 0.4111\\
  276. \end{Examples}
  277. \end{Concept}
  278. \begin{Operator}{num_fit}
  279. \index{approximation}\index{least squares}
  280. The operator \name{num_fit} finds for a set of
  281. points the linear combination of a given set of
  282. functions (function basis) which approximates the
  283. points best under the objective of the \name{least squares}
  284. criterion (minimum of the sum of the squares of the deviation).
  285. The solution is found as zero of the
  286. gradient vector of the sum of squared errors.
  287. \begin{Syntax}
  288. \name{num_fit}\(\meta{vals},\meta{basis},\meta{var}=\meta{pts}\)
  289. \end{Syntax}
  290. where \meta{vals} is a list of numeric values,
  291. \meta{var} is a variable used for the approximation,
  292. \meta{pts} is a list of coordinate values which correspond to
  293. \meta{var},
  294. \meta{basis} is a set of functions varying in \name{var} which is used
  295. for the approximation.
  296. The result is a list containing as first element the
  297. function which approximates the given values, and as
  298. second element a list of coefficients which were used
  299. to build this function from the basis.
  300. \begin{Examples}
  301. pts:=for i:=1 step 1 until 5 collect i$\\
  302. vals:=for i:=1 step 1 until 5 collect\\
  303. for j:=1:i product j$\\
  304. num_fit(vals,{1,x,x**2},x=pts);&
  305. \begin{multilineoutput}{6cm}
  306. \{14.571428571*X^2 - 61.428571429*X + 54.6,\{54.6,
  307. - 61.428571429,14.571428571\}\}
  308. \end{multilineoutput}
  309. \end{Examples}
  310. \end{Operator}