Changeset 7510:2bb01534eab0
- Timestamp:
- 11/11/07 03:38:42 (6 years ago)
- Branch:
- default
- File:
-
- 1 edited
-
sage/calculus/calculus.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/calculus/calculus.py
r7471 r7510 1913 1913 maximum_num_subintervals=200): 1914 1914 r""" 1915 Return a numerical approximation to the integral of self from 1916 a to b. 1915 Return a floating point machine precision numerical 1916 approximation to the integral of self from a to b, computed 1917 using floating point arithmetic and the GSL scientific 1918 library. 1917 1919 1918 1920 INPUT: … … 1955 1957 sage: numerical_integral(f, 0, 1) # random low-order bits 1956 1958 (0.52848223225314706, 6.8392846084921134e-07) 1959 1960 Note that in exotic cases where floating point evaluation of 1961 the expression leads to the wrong value, then the output 1962 can be completely wrong: 1963 sage: f = exp(pi*sqrt(163)) - 262537412640768744 1964 1965 Despite appearance, f is really very close to 0, but one 1966 gets a nonzero value since the definition of float(f) is 1967 that it makes all constants inside the expression floats, then 1968 evaluates each function and each arithmetic operation 1969 using float arithmetic: 1970 sage: float(f) 1971 -480.0 1972 1973 Computing to higher precision we see the truth: 1974 sage: f.n(200) 1975 -0.00000000000074992740280181431112064614366496792309675391526978827185055 1976 sage: f.n(300) 1977 -0.000000000000749927402801814311120646143662663009137292462589621789352092802261939388897590086687280282 1978 1979 Now numerically integrating, we see why the answer is wrong: 1980 sage: f.nintegrate(x,0,1) 1981 (-480.00000000000011, 5.3290705182007538e-12, 21, 0) 1982 1983 It is just because every floating point evaluation of return 1984 -480.0 in floating point. 1985 1986 Important note: using GP/PARI one can compute numerical 1987 integrals to high precision: 1988 sage: gp.eval('intnum(x=17,42,exp(-x^2)*log(x))') 1989 '2.565728500561051482917356396 E-127' 1990 sage: old_prec = gp.set_real_precision(50) 1991 '2.5657285005610514829173563961304785900147709554020 E-127' 1992 1993 Note that the input function above is a string in PARI 1994 syntax. 1957 1995 """ 1958 1996 v = self._maxima_().quad_qags(var(x),
Note: See TracChangeset
for help on using the changeset viewer.
