|
Calling Sequence
|
|
evalf(expression)
evalf[n](expression)
|
|
Parameters
|
|
expression
|
-
|
expression to be evaluated
|
n
|
-
|
(optional) posint; specifies number of digits
|
|
|
|
|
Basic Information
|
|
•
|
This help page contains complete information about the evalf command. For basic information on the evalf command, see the evalf help page.
|
|
|
Description
|
|
•
|
The evalf command numerically evaluates expressions (or subexpressions) involving constants (for example, Pi, exp(1), and gamma) and mathematical functions (for example, exp, ln, sin, arctan, cosh, GAMMA, and erf.
|
|
To perform numerical integration, use the evalf command in conjunction with the Int or int command. For more information, see the evalf/Int help page.
|
|
To perform numerical summation, use the evalf command in conjunction with the Sum or sum command. For more information, see the evalf/Sum help page.
|
|
|
Output
|
|
•
|
The evalf command returns a floating-point or complex floating-point number or expression.
|
|
Numeric Precision
|
|
•
|
You can control the precision of all numeric computations using the environment variable Digits. By default, Digits is assigned the value 10, so the evalf command uses 10-digit floating-point arithmetic.
|
|
You can assign any positive integer that does not exceed the kernelopts(maxdigits) value to Digits. For more information, see kernelopts.
|
•
|
To specify the numeric precision for an evalf computation without changing the value of Digits, use the optional index in the calling sequence, that is, evalf[n](expression).
|
|
|
|
Constants and Mathematical Functions
|
|
•
|
You can extend the evalf command interface to evaluate new constants and functions.
|
|
To define a new constant K, you must define a procedure `evalf/constant/K`. Then the calling sequence evalf(K) and more general evalf(expression) calling sequences, in which expression contains K, invoke `evalf/constant/K`().
|
|
If you define a procedure `evalf/f`, the calling sequence evalf(`f`(arguments)) and more general evalf(expression) calling sequences, in which expression contains `f`(arguments), invoke `evalf/f`(arguments).
|
|
Note: To call `evalf/f`(arguments) if floating-point arguments are specified (without requiring the use of unevaluation (right) quotes), use type checking. For an example, see the Examples section.
|
•
|
Most initially known mathematics functions automatically invoke the corresponding evalf subfunction if they are passed a floating-point argument.
|
|
|
Examples
|
|
>
|
|
| (2) |
| (3) |
>
|
|
| (4) |
|
Numeric Integration
|
|
|
To compute a numeric approximation to an unevaluated integral returned by the Int or int command, use the evalf command.
|
>
|
|
| (5) |
>
|
|
|
|
Numeric Summation
|
|
|
To compute a numeric approximation to an unevaluated sum returned by the Sum or sum command, use the evalf command.
|
>
|
|
| (8) |
>
|
|
|
|
When not to use evalf
|
|
|
There are times when using the evalf command causes problems mathematically. For example, the rounding error in floating-point values can give incorrect solutions to further computations. The following example does not work if you evaluate to a floating-point value too early in the computation.
|
>
|
|
>
|
|
|
|
Adding a New Function and evalf Subroutine
|
|
|
When you specify floating-point arguments in a call to the sin function, it automatically returns a floating-point result.
|
| (13) |
|
You can define a new function and corresponding evalf subroutine such that the evalf subroutine is called if the arguments are of floating-point type. To clearly show which code Maple uses, in the following example, the mathematical function returns 2 times the argument for non-floating-point input and 2.01 times the argument for floating-point input.
|
>
|
NewFunction := proc(x)
local s;
if type(x,'complex'('float')) then
s := evalf('NewFunction'(x));
if type(s,'complex'('float')) then
return s;
end if;
end if;
return 2*x;
end proc:
|
>
|
`evalf/NewFunction` := proc(xx)
local x;
x := evalf(xx);
if not type(x, 'complex'('float')) then
return 'NewFunction'(x);
end if;
return 2.01*x;
end proc:
|
|
You can always use the evalf subroutine by directly calling it or placing unevaluation quotes around instances of the function name.
|
|
|
|
|