Movatterモバイル変換


[0]ホーム

URL:


WOLFRAM

Wolfram Language & System Documentation Center
Numerical Operations on Functions

Numerical Operations on Functions

Arithmetic
You can do arithmetic with the Wolfram Language just as you would on an electronic calculator.
This is the sum of two numbers:
Here the/ stands for division, and the^ stands for power:
Spaces denote multiplication in the Wolfram Language. The front end automatically replaces spaces between numbers with light gray multiplication signs:
You can use a* for multiplication if you want to:
You can type arithmetic expressions with parentheses:
x^y
power
-x
minus
x/y
divide
xyz
or
x*y*z
multiply
x+y+z
add
Arithmetic operations in the Wolfram Language.
Arithmetic operations in the Wolfram Language are grouped according to the standard mathematical conventions. As usual,2^3+4, for example, means(2^3)+4, and not2^(3+4). You can always control grouping by explicitly using parentheses.
This result is given in scientific notation:
You can enter numbers in scientific notation like this:
Or like this:
With the Wolfram Language, you can perform calculations with a particular precision, usually higher than an ordinary calculator. When given precise numbers, the Wolfram Language does not convert them to an approximate representation, but gives a precise result.
This gives the result in terms of rational numbers:
This gives the approximate numerical result:
This gives the approximate numerical result with 40 significant digits:
Numerical Mathematics in the Wolfram Language
One of the important features of the Wolfram Language is its ability to give you exact, symbolic results for computations. There are, however, computations where it is just mathematically impossible to get exact "closed form" results. In such cases, you can still often get approximate numerical results.
There is no "closed form" result for. The Wolfram Language returns the integral in symbolic form:
You can now take the symbolic form of the integral and ask for its approximate numerical value:
When the Wolfram Language cannot find an explicit result for something like a definite integral, it returns a symbolic form. You can take this symbolic form and try to get an approximate numerical value by applyingN.
By giving a second argument toN, you can specify the numerical precision to use:
If you want to evaluate an integral numerically in the Wolfram Language, then usingIntegrate and applyingN to the result is not the most efficient way to do it. It is better instead to use the functionNIntegrate, which immediately gives a numerical answer, without first trying to get an exact symbolic result. You should realize that even whenIntegrate does not in the end manage to give you an exact result, it may spend a lot of time trying to do so.
NIntegrate evaluates numerical integrals directly, without first trying to get a symbolic result:
IntegrateNIntegrate
definite integrals
SumNSum
sums
ProductNProduct
products
SolveNSolve
solutions of algebraic equations
DSolveNDSolve
solutions of differential equations
MaximizeNMaximize
maximization
Symbolic and numerical versions of some Wolfram Language functions.
The Uncertainties of Numerical Mathematics
The Wolfram System does operations like numerical integration very differently from the way it does their symbolic counterparts.
When you do a symbolic integral, the Wolfram System takes the functional form of the integrand you have given, and applies a sequence of exact symbolic transformation rules to it, to try and evaluate the integral.
However, when the Wolfram System does a numerical integral, after some initial symbolic preprocessing, the only information it has about your integrand is a sequence of numerical values for it. To get a definite result for the integral, the Wolfram System then effectively has to make certain assumptions about the smoothness and other properties of your integrand. If you give a sufficiently pathological integrand, these assumptions may not be valid, and as a result, the Wolfram System may simply give you the wrong answer for the integral.
This problem may occur, for example, if you try to integrate numerically a function which has a very thin spike at a particular position. The Wolfram System samples your function at a number of points, and then assumes that the function varies smoothly between these points. As a result, if none of the sample points come close to the spike, then the spike will go undetected, and its contribution to the numerical integral will not be correctly included.
Here is a plot of the function:
NIntegrate gives the correct answer for the numerical integral of this function from to:
If, however, you ask for the integral from to, with its default settingsNIntegrate will miss the peak near, and give the wrong answer:
NIntegrate tries to make the best possible use of the information that it can get about the numerical values of the integrand. Thus, for example, by default, ifNIntegrate notices that the estimated error in the integral in a particular region is large, it will take more samples in that region. In this way,NIntegrate tries to "adapt" its operation to the particular integrand you have given.
The kind of adaptive procedure thatNIntegrate uses is similar, at least in spirit, to whatPlot does in trying to draw smooth curves for functions. In both cases, the Wolfram System tries to go on taking more samples in a particular region until it has effectively found a smooth approximation to the function in that region.
The kinds of problems that can appear in numerical integration can also arise in doing other numerical operations on functions.
For example, if you ask for a numerical approximation to the sum of an infinite series, the Wolfram System samples a certain number of terms in the series, and then does an extrapolation to estimate the contributions of other terms. If you insert large terms far out in the series, they may not be detected when the extrapolation is done, and the result you get for the sum may be incorrect.
A similar problem arises when you try to find a numerical approximation to the minimum of a function. The Wolfram System samples only a finite number of values, then effectively assumes that the actual function interpolates smoothly between these values. If in fact the function has a sharp dip in a particular region, then the Wolfram System may miss this dip, and you may get the wrong answer for the minimum.
If you work only with numerical values of functions, there is simply no way to avoid the kinds of problems discussed here. Exact symbolic computation, of course, allows you to get around these problems.
In many calculations, it is therefore worthwhile to go as far as you can symbolically, and then resort to numerical methods only at the very end. This gives you the best chance of avoiding the problems that can arise in purely numerical computations.
Introduction to Numerical Sums, Products, and Integrals
NSum[f,{i,imin,Infinity}]
numerical approximation to
NProduct[f,{i,imin,Infinity}]
numerical approximation to
NIntegrate[f,{x,xmin,xmax}]
numerical approximation to
NIntegrate[f,{x,xmin,xmax},{y,ymin,ymax}]
the multiple integral
Numerical sums, products, and integrals.
Here is a numerical approximation to:
NIntegrate can handle singularities in the integration region:
You can do numerical integrals over infinite regions:
Here is a double integral over a triangular domain. Note the order in which the variables are given:
Here is a double integral over a more complicated domain:
Numerical Integration
N[Integrate[expr,{x,xmin,xmax}]]
try to perform an integral exactly, then find numerical approximations to the parts that remain
NIntegrate[expr,{x,xmin,xmax}]
find a numerical approximation to an integral
NIntegrate[expr,{x,xmin,xmax},{y,ymin,ymax},]
multidimensional numerical integral
NIntegrate[expr,{x,xmin,x1,x2,,xmax}]
do a numerical integral along a line, starting at, going through the points, and ending at
Numerical integration functions.
This finds a numerical approximation to the integral:
Here is the numerical value of the double integral:
An important feature ofNIntegrate is its ability to deal with functions that "blow up" at known points.NIntegrate automatically checks for such problems at the endpoints of the integration region.
The function blows up at, butNIntegrate still succeeds in getting the correct value for the integral:
The Wolfram Language can find the integral of exactly:
NIntegrate detects that the singularity in at is not integrable:
NIntegrate automatically looks for singularities at the endpoints of the integration region and any subregions defined by piecewise functions (such asPiecewise andAbs) in the integrand. If additional singularities are present,NIntegrate may not give you the right answer for the integral. Nevertheless, in following its adaptive procedure,NIntegrate will often detect the presence of potentially singular behavior, and will warn you about it.
NIntegrate warns you of a possible problem due to the singularity in the middle of the integration region. The final result is numerically quite close to the correct answer:
If you know that your integrand has singularities at particular points, you can explicitly tellNIntegrate to deal with them.NIntegrate[expr,{x,xmin,x1,x2,,xmax}] integratesexpr fromxmin toxmax, looking for possible singularities at each of the intermediate pointsxi.
This gives the same integral, but now explicitly deals with the singularity at:
You can also use the list of intermediate pointsxi inNIntegrate to specify an integration contour to follow in the complex plane. The contour is taken to consist of a sequence of line segments, starting atxmin, going through each of thexi, and ending atxmax.
This integrates around a closed contour in the complex plane, going from through the points,, and, then back to:
The integral gives, as expected from Cauchy's theorem:
option name
default value
MinRecursion0
minimum number of recursions for the integration method
MaxRecursionAutomatic
maximum number of recursions for the integration method
MaxPointsAutomatic
maximum total number of times to sample the integrand
Special options forNIntegrate.
WhenNIntegrate tries to evaluate a numerical integral, it samples the integrand at a sequence of points. If it finds that the integrand changes rapidly in a particular region, then it recursively takes more sample points in that region. The parametersMinRecursion andMaxRecursion specify the minimum and maximum number of recursions to use. Increasing the value ofMinRecursion guarantees thatNIntegrate will use a larger number of sample points.MaxPoints andMaxRecursion limit the number of sample points whichNIntegrate will ever try to use. IncreasingMinRecursion orMaxRecursion will makeNIntegrate work more slowly.
With the default settings for all options,NIntegrate misses the peak in near, and gives the wrong answer for the integral:
With the optionMinRecursion->3,NIntegrate samples enough points that it notices the peak around. With the default setting ofMaxRecursion, however,NIntegrate cannot use enough sample points to be able to expect an accurate answer:
With this setting ofMaxRecursion,NIntegrate can get an accurate answer for the integral:
Another way to solve the problem is to makeNIntegrate break the integration region into several pieces, with a small piece that explicitly covers the neighborhood of the peak:
For integrals in many dimensions, it can take a long time forNIntegrate to get a precise answer. However, by setting the optionMaxPoints, you can tellNIntegrate to give you just a rough estimate, sampling the integrand only a limited number of times.
Here is a way to get a rough estimate for an integral that takes a long time to compute:
Numerical Evaluation of Sums and Products
NSum[f,{i,imin,imax}]
find a numerical approximation to the sum
NSum[f,{i,imin,imaxdi}]
use step in the sum
NProduct[f,{i,imin,imax}]
find a numerical approximation to the product
Numerical sums and products.
This gives a numerical approximation to:
There is no exact result for this sum, so the Wolfram Language leaves it in a symbolic form:
You can applyN explicitly to get a numerical result:
The wayNSum works is to include a certain number of terms explicitly, and then to try and estimate the contribution of the remaining ones. There are three approaches to estimating this contribution. The first uses the EulerMaclaurin method, and is based on approximating the sum by an integral. The second method, known as the Wynn epsilon method, samples a number of additional terms in the sum, and then tries to fit them to a polynomial multiplied by a decaying exponential. The third approach, useful for alternating series, uses an alternating signs method; it also samples a number of additional terms and approximates their sum by the ratio of two polynomials (Padé approximation).
option name
default value
MethodAutomatic
Automatic,"EulerMaclaurin","WynnEpsilon", or"AlternatingSigns"
NSumTerms15
number of terms to include explicitly
VerifyConvergenceTrue
whether the convergence of the series should be verified
Special options forNSum.
If you do not explicitly specify the method to use,NSum will try to choose between theEulerMaclaurin orWynnEpsilon methods. In any case, some implicit assumptions about the functions you are summing have to be made. If these assumptions are not correct, you may get inaccurate answers.
The most common place to useNSum is in evaluating sums with infinite limits. You can, however, also use it for sums with finite limits. By making implicit assumptions about the objects you are evaluating,NSum can often avoid doing as many function evaluations as an explicitSum computation would require.
This finds the numerical value of by extrapolation techniques:
You can also get the result, albeit much less efficiently, by constructing the symbolic form of the sum, then evaluating it numerically:
NProduct works in essentially the same way asNSum, with analogous options.
Numerical Equation Solving
NSolve[lhs==rhs,x]
solve a polynomial equation numerically
NSolve[{lhs1==rhs1,lhs2==rhs2,},{x,y,}]
solve a system of polynomial equations numerically
FindRoot[lhs==rhs,{x,x0}]
search for a numerical solution to an equation, starting at
FindRoot[{lhs1==rhs1,lhs2==rhs2,},{{x,x0},{y,y0},}]
search for numerical solutions to simultaneous equations
Numerical root finding.
NSolve gives you numerical approximations to all the roots of a polynomial equation:
You can also useNSolve to solve sets of simultaneous equations numerically:
If your equations involve only linear functions or polynomials, then you can useNSolve to get numerical approximations to all the solutions. However, when your equations involve more complicated functions, there is in general no systematic procedure for finding all solutions, even numerically. In such cases, you can useFindRoot to search for solutions. You have to giveFindRoot a place to start its search.
This searches for a numerical solution, starting at:
The equation has several solutions. If you start at a different,FindRoot may return a different solution:
You can search for solutions to sets of equations. Here the solution involves complex numbers:
Numerical Solution of Polynomial Equations
WhenSolve cannot find solutions in terms of radicals to polynomial equations, it returns a symbolic form of the result in terms ofRoot objects:
You can get numerical solutions by applyingN:
This gives the numerical solutions to 25digit precision:
You can useNSolve to get numerical solutions to polynomial equations directly, without first trying to find exact results:
NSolve[poly==0,x]
get approximate numerical solutions to a polynomial equation
NSolve[poly==0,x,n]
get solutions usingdigit precision arithmetic
NSolve[{eqn1,eqn2,},{var1,var2,}]
get solutions to a polynomial system
Numerical solution of polynomial equations and systems.
NSolve will give you the complete set of numerical solutions to any polynomial equation or system of polynomial equations.
NSolve can find solutions to sets of simultaneous polynomial equations:
Numerical Root Finding
NSolve gives you a general way to find numerical approximations to the solutions of polynomial equations. Finding numerical solutions to more general equations, however, can be much more difficult, as discussed in"Equations in One Variable".FindRoot gives you a way to search for a numerical root of a function or a numerical solution to an arbitrary equation, or set of equations.
FindRoot[f,{x,x0}]
search for a numerical root off, starting withx=x0
FindRoot[lhs==rhs,{x,x0}]
search for a numerical solution to the equationlhs==rhs, starting withx=x0
FindRoot[f1,f2,,{{x,x0},{y,y0},}]
search for a simultaneous numerical root of all thefi
FindRoot[{eqn1,eqn2,},{{x,x0},{y,y0},}]
search for a numerical solution to the simultaneous equationseqni
Numerical root finding.
The curves for and intersect at one point:
This finds a numerical approximation to the value of at which the intersection occurs. The0 tellsFindRoot what value of to try first:
In trying to find a solution to your equation,FindRoot starts at the point you specify, and then progressively tries to get closer and closer to a solution. Even if your equations have several solutions,FindRoot always returns the first solution it finds. Which solution this is will depend on what starting point you chose. If you start sufficiently close to a particular solution,FindRoot will usually return that solution.
The function has an infinite number of roots of the form. If you start sufficiently close to a particular root,FindRoot will give you that root:
If you start with, you get a numerical approximation to the root:
If you wantFindRoot to search for complex solutions, then you have to give a complex starting value:
This finds a zero of the Riemann zeta function:
This finds a solution to a set of simultaneous equations:
The variables used byFindRoot can have values that are lists. This allows you to find roots of functions that take vectors as arguments.
This is a way to solve a linear equation for the variable:
This finds a normalized eigenvector and eigenvalue:
Introduction to Numerical Differential Equations
NDSolve[eqns,y,{x,xmin,xmax}]
solve numerically for the functiony, with the independent variablex in the rangexmin toxmax
NDSolve[eqns,{y1,y2,},{x,xmin,xmax}]
solve a system of equations for theyi
Numerical solution of differential equations.
This generates a numerical solution to the equation with. The result is given in terms of anInterpolatingFunction:
Here is the value of:
With an algebraic equation such as, each solution for is simply a single number. For a differential equation, however, the solution is afunction, rather than a single number. For example, in the equation, you want to get an approximation to the function as the independent variable varies over some range.
The Wolfram Language represents numerical approximations to functions asInterpolatingFunction objects. These objects are functions which, when applied to a particular, return the approximate value of at that point. TheInterpolatingFunction effectively stores a table of values for, then interpolates this table to find an approximation to at the particular you request.
y[x]/.solution
use the list of rules for the functiony to get values fory[x]
InterpolatingFunction[data][x]
evaluate an interpolated function at the pointx
Plot[Evaluate[y[x]/.solution],{x,xmin,xmax}]
plot the solution to a differential equation
Using results fromNDSolve.
This solves a system of two coupled differential equations:
Here is the value ofz[2] found from the solution:
Here is a plot of the solution forz[x] found on line 3.Plot is discussed in"Basic Plotting":
NDSolve[eqn,u,{x,xmin,xmax},{t,tmin,tmax},]
solve a partial differential equation
Numerical solution of partial differential equations.
Numerical Solution of Differential Equations
The functionNDSolve discussed in"Numerical Differential Equations" allows you to find numerical solutions to differential equations.NDSolve handles both single differential equations and sets of simultaneous differential equations. It can handle a wide range ofordinary differential equations as well as somepartial differential equations. In a system of ordinary differential equations there can be any number of unknown functions, but all of these functions must depend on a single "independent variable", which is the same for each function. Partial differential equations involve two or more independent variables.NDSolve can also handledifferentialalgebraic equations that mix differential equations with algebraic ones.
NDSolve[{eqn1,eqn2,},y,{x,xmin,xmax}]
find a numerical solution for the functiony withx in the rangexmin toxmax
NDSolve[{eqn1,eqn2,},{y1,y2,},{x,xmin,xmax}]
find numerical solutions for several functionsyi
Finding numerical solutions to ordinary differential equations.
NDSolve represents solutions for the functionsyi asInterpolatingFunction objects. TheInterpolatingFunction objects provide approximations to theyi over the range of valuesxmin toxmax for the independent variablex.
NDSolve finds solutions iteratively. It starts at a particular value ofx, then takes a sequence of steps, trying eventually to cover the whole rangexmin toxmax.
In order to get started,NDSolve has to be given appropriate initial or boundary conditions for theyi and their derivatives. These conditions specify values foryi[x], and perhaps derivativesyi'[x], at particular pointsx. In general, at least for ordinary differential equations, the conditions you give can be at anyx:NDSolve will automatically cover the rangexmin toxmax.
This finds a solution fory withx in the range0 to2, using an initial condition fory[0]:
This still finds a solution withx in the range0 to2, but now the initial condition is fory[3]:
Here is a simple boundary value problem:
When you useNDSolve, the initial or boundary conditions you give must be sufficient to determine the solutions for theyi completely. When you useDSolve to find symbolic solutions to differential equations, you can get away with specifying fewer initial conditions. The reason is thatDSolve automatically inserts arbitrary constantsC[i] to represent degrees of freedom associated with initial conditions that you have not specified explicitly. SinceNDSolve must give a numerical solution, it cannot represent these kinds of additional degrees of freedom. As a result, you must explicitly give all the initial or boundary conditions that are needed to determine the solution.
In a typical case, if you have differential equations with up toth derivatives, then you need to give initial conditions for up toth derivatives, or give boundary conditions at points.
With a thirdorder equation, you need to give initial conditions for up to second derivatives:
This plots the solution obtained:
With a thirdorder equation, you can also give boundary conditions at three points:
The Wolfram Language allows you to use any appropriate linear combination of function values and derivatives as boundary conditions:
In most cases, all the initial conditions you give must involve the same value ofx, sayx0. As a result, you can avoid giving bothxmin andxmax explicitly. If you specify your range ofx as{x,x1}, then the Wolfram Language will automatically generate a solution over the rangex0 tox1.
This generates a solution over the range 0 to 2:
You can give initial conditions as equations of any kind. In some cases, these equations may have multiple solutions. In such cases,NDSolve will correspondingly generate multiple solutions.
The initial conditions in this case lead to multiple solutions:
Here is a plot of all the solutions:
You can useNDSolve to solve systems of coupled differential equations.
This finds a numerical solution to a pair of coupled equations:
This plots the solution fory from these equations:
This generates a parametric plot using bothx andy:
Unknown functions in differential equations do not necessarily have to be represented by single symbols. If you have a large number of unknown functions, you will often find it more convenient, for example, to give the functions names likey[i].
This constructs a set of five coupled differential equations and initial conditions:
This solves the equations:
Here is a plot of the solutions:
NDSolve can handle functions whose values are lists or arrays. If you give initial conditions likey[0]=={v1,v2,,vn}, thenNDSolve will assume thaty is a function whose values are lists of lengthn.
This solves a system of four coupled differential equations:
Here are the solutions:
option name
default value
MaxStepsAutomatic
maximum number of steps in to take
StartingStepSizeAutomatic
starting size of step in to use
MaxStepSizeAutomatic
maximum size of step in to use
NormFunctionAutomatic
the norm to use for error estimation
Special options forNDSolve.
NDSolve has many methods for solving equations, but essentially all of them at some level work by taking a sequence of steps in the independent variable, and using an adaptive procedure to determine the size of these steps. In general, if the solution appears to be varying rapidly in a particular region, thenNDSolve will reduce the step size or change the method so as to be able to track the solution better.
This solves a differential equation in which the derivative has a discontinuity:
NDSolve reduced the step size around so as to reproduce the kink accurately:
Through its adaptive procedure,NDSolve is able to solve "stiff" differential equations in which there are several components that vary with at very different rates.
In these equations,y varies much more rapidly thanz:
NDSolve nevertheless tracks both components successfully:
NDSolve follows the general procedure of reducing step size until it tracks solutions accurately. There is a problem, however, when the true solution has a singularity or the integration interval is too big. For the first case,NDSolve limits the smallest step size that it will consider as significant for a given integration interval. For the second case, the optionMaxSteps specifies the maximum number of steps thatNDSolve will ever take in attempting to find a solution. For ordinary differential equations the default setting isMaxStepsAutomatic. With theAutomatic setting,NDSolve will estimate how many steps are needed to solve the equation at hand based on the initial step sizes taken.
NDSolve stops when the step size becomes too small:
There is in fact a singularity in the solution at:
The default setting forMaxSteps should be sufficient for most equations with smooth solutions. When solutions have a complicated structure, however, you may occasionally have to choose larger settings forMaxSteps. With the settingMaxSteps->Infinity, there is no upper limit on the number of steps used.
To take the solution to the Lorenz equations this far, you need to remove the default bound onMaxSteps:
Here is a parametric plot of the solution in three dimensions:
WhenNDSolve solves a particular set of differential equations, it always tries to choose a step size appropriate for those equations. In some cases, the very first step thatNDSolve makes may be too large, and it may miss an important feature in the solution. To avoid this problem, you can explicitly set the optionStartingStepSize to specify the size to use for the first step.
The equations you give toNDSolve do not necessarily all have to involve derivatives; they can also just be algebraic. You can useNDSolve to solve many suchdifferentialalgebraic equations.
This solves a system of differentialalgebraic equations:
Here is the solution:
NDSolve[{eqn1,eqn2,},u,{t,tmin,tmax},{x,xmin,xmax},]
solve a system of partial differential equations foru
NDSolve[{eqn1,eqn2,},{u1,u2,},{t,tmin,tmax},{x,xmin,xmax},]
solve a system of partial differential equations for several functionsui
Finding numerical solutions to partial differential equations.
This finds a numerical solution to the wave equation. The result is a twodimensional interpolating function:
This generates a plot of the result:
This finds a numerical solution to a nonlinear wave equation:
Here is a 3D plot of the result:
This is a higherresolution density plot of the solution:
Here is a version of the equation in 2+1 dimensions:
This solves the equation:
This generates a list of plots of the solution:
Numerical Optimization
FindMinimum[f,{x,x0}]
search for a local minimum off, starting atx=x0
FindMinimum[f,x]
search for a local minimum off
FindMinimum[f,{{x,x0},{y,y0},}]
search for a local minimum in several variables
FindMinimum[{f,cons},{{x,x0},{y,y0},}]
search for a local minimum subject to the constraintscons starting atx=x0,y=y0,
FindMinimum[{f,cons},{x,y,}]
search for a local minimum subject to the constraintscons
FindMaximum[f,x]
, etc.
search for a local maximum
Searching for local minima and maxima.
This finds the value of which minimizes, starting at:
The last element of the list gives the value at which the minimum is achieved:
LikeFindRoot,FindMinimum andFindMaximum work by starting from a point, then progressively searching for a minimum or maximum. But since they return a result as soon as they find anything, they may give only a local minimum or maximum of your function, not a global one.
This curve has two local minima:
Starting at, you get the local minimum on the right:
This gives the local minimum on the left, which in this case is also the global minimum:
You can specify variables without initial values:
You can specify a constraint:
NMinimize[f,x]
try to find the global minimum off
NMinimize[f,{x,y,}]
try to find the global minimum over several variables
NMaximize[f,x]
try to find the global maximum off
NMaximize[f,{x,y,}]
try to find the global maximum over several variables
Finding global minima and maxima.
This immediately finds the global minimum:
NMinimize andNMaximize are numerical analogs ofMinimize andMaximize. But unlikeMinimize andMaximize they usually cannot guarantee to find absolute global minima and maxima. Nevertheless, they typically work well when the functionf is fairly smooth, and has a limited number of local minima and maxima.
NMinimize[{f,cons},{x,y,}]
try to find the global minimum off subject to constraintscons
NMaximize[{f,cons},{x,y,}]
try to find the global maximum off subject to constraintscons
Finding global minima and maxima subject to constraints.
With the constraint,NMinimize will give the local minimum on the right:
This finds the minimum of within the unit circle:
In this caseMinimize can give an exact result:
But in this case it cannot:
This gives a numerical approximation, effectively usingNMinimize:
If both the objective functionf and the constraintscons are linear in all variables, then minimization and maximization correspond to alinear programming problem. Sometimes it is convenient to state such problems not in terms of explicit equations, but instead in terms of matrices and vectors.
LinearProgramming[c,m,b]
find the vector which minimizes subject to the constraints and
LinearProgramming[c,m,b,l]
use the constraints and
Linear programming in matrix form.
Here is a linear programming problem in equation form:
Here is the corresponding problem in matrix form:
You can specify a mixture of equality and inequality constraints by making the list be a sequence of pairs. If is, then theth constraint is. If is then it is, and if is then it is.
This makes the first inequality use:
InLinearProgramming[c,m,b,l], you can make be a list of pairs representing lower and upper bounds on the.
In doing large linear programming problems, it is often convenient to give the matrix as aSparseArray object.
Controlling the Precision of Results
In doing numerical operations likeNDSolve andNMinimize, the Wolfram Language by default uses machine numbers. But by setting the optionWorkingPrecision->n you can tell it to use arbitraryprecision numbers withndigit precision.
This does a machineprecision computation of a numerical integral:
This does the computation with 30digit arbitraryprecision numbers:
When you give a setting forWorkingPrecision, this typically defines an upper limit on the precision of the results from a computation. But within this constraint you can tell the Wolfram Language how much precision and accuracy you want it to try to get. You should realize that for many kinds of numerical operations, increasing precision and accuracy goals by only a few digits can greatly increase the computation time required. Nevertheless, there are many cases where it is important to ensure that high precision and accuracy are obtained.
WorkingPrecision
the number of digits to use for computations
PrecisionGoal
the number of digits of precision to try to get
AccuracyGoal
the number of digits of accuracy to try to get
Options for controlling precision and accuracy.
This gives a result to 25digit precision:
50digit precision cannot be achieved with 30digit working precision:
Given a particular setting forWorkingPrecision, each of the functions for numerical operations in the Wolfram Language uses certain default settings forPrecisionGoal andAccuracyGoal. Typical is the case ofNDSolve, in which these default settings are equal to half the settings given forWorkingPrecision.
The precision and accuracy goals normally apply both to the final results returned, and to various norms or error estimates for them. Functions for numerical operations in the Wolfram Language typically try to refine their results until either the specified precision goal or accuracy goal is reached. If the setting for either of these goals isInfinity, then only the other goal is considered.
In doing ordinary numerical evaluation withN[expr,n], the Wolfram Language automatically adjusts its internal computations to achievendigit precision in the result. But in doing numerical operations on functions, it is in practice usually necessary to specifyWorkingPrecision andPrecisionGoal more explicitly.
Monitoring and Selecting Algorithms
Functions in the Wolfram Language are carefully set up so that you normally do not have to know how they work inside. But particularly for numerical functions that use iterative algorithms, it is sometimes useful to be able to monitor the internal progress of these algorithms.
StepMonitor
an expression to evaluate whenever a successful step is taken
EvaluationMonitor
an expression to evaluate whenever functions from the input are evaluated
Options for monitoring progress of numerical functions.
This prints the value ofx every time a step is taken:
Note the importance of usingoption:>expr rather thanoption->expr. You need a delayed rule:> to makeexpr be evaluated each time it is used, rather than just when the rule is given.
Reap andSow provide a convenient way to make a list of the steps taken:
This counts the steps:
To take a successful step toward an answer, iterative numerical algorithms sometimes have to do several evaluations of the functions they have been given. Sometimes this is because each step requires, say, estimating a derivative from differences between function values, and sometimes it is because several attempts are needed to achieve a successful step.
This shows the successful steps taken in reaching the answer:
This shows every time the function was evaluated:
The pattern of evaluations done by algorithms in the Wolfram Language can be quite complicated:
Method->Automatic
pick methods automatically(default)
Method->"name"
specify an explicit method to use
Method->{"name",{"par1"->val1,}}
specify more details of a method
Method options.
There are often several different methods known for doing particular types of numerical computations. Typically the Wolfram Language supports most generally successful ones that have been discussed in the literature, as well as many that have not. For any specific problem, it goes to considerable effort to pick the best method automatically. But if you have sophisticated knowledge of a problem, or are studying numerical methods for their own sake, you may find it useful to tell the Wolfram Language explicitly what method it should use. Function reference pages list some of the methods built into the Wolfram Language; others are discussed in"Numerical and Related Functions" or in advanced documentation.
This solves a differential equation using method, and returns the number of steps and evaluations needed:
With the method selected automatically, this is the number of steps and evaluations that are needed:
This shows what happens with several other possible methods. The Adams method that is selected automatically is the fastest:
This shows what happens with the explicit RungeKutta method when the difference order parameter is changed:
Functions with Sensitive Dependence on Their Input
Functions that are specified by simple algebraic formulas tend to be such that when their input is changed only slightly, their output also changes only slightly. But functions that are instead based on executing procedures quite often show almost arbitrarily sensitive dependence on their input. Typically the reason this happens is that the procedure "excavates" progressively less and less significant digits in the input.
This shows successive steps in a simple iterative procedure with input 0.1111:
Here is the result with input 0.1112. Progressive divergence from the result with input 0.1111 is seen:
The action ofFractionalPart[2x] is particularly simple in terms of the binary digits of the numberx: it just drops the first one, and shifts the remaining ones to the left. After several steps, this means that the results one gets are inevitably sensitive to digits that are far to the right, and have an extremely small effect on the original value ofx.
This shows the shifting process achieved byFractionalPart[2x] in the first 8 binary digits ofx:
If you give input only to a particular precision, you are effectively specifying only a certain number of digits. And once all these digits have been "excavated" you can no longer get accurate results, since to do so would require knowing more digits of your original input. So long as you use arbitraryprecision numbers, the Wolfram Language automatically keeps track of this kind of degradation in precision, indicating a number with no remaining significant digits by0.×10e, as discussed in"Arbitrary-Precision Numbers".
Successive steps yield numbers of progressively lower precision, and eventually no precision at all:
This asks for the precision of each number. Zero precision indicates that there are no correct significant digits:
This shows that the exact result is a periodic sequence:
It is important to realize that if you use approximate numbers of any kind, then in an example like the one above you will always eventually run out of precision. But so long as you use arbitraryprecision numbers, the Wolfram Language will explicitly show you any decrease in precision that is occurring. However, if you use machineprecision numbers, then the Wolfram Language will not keep track of precision, and you cannot tell when your results become meaningless.
If you use machineprecision numbers, the Wolfram Language will no longer keep track of any degradation in precision:
By iterating the operationFractionalPart[2x] you extract successive binary digits in whatever number you start with. And if these digits are apparently randomas in a number likethen the results will be correspondingly random. But if the digits have a simple patternas in any rational numberthen the results you get will be correspondingly simple.
By iterating an operation such asFractionalPart[3/2x] it turns out however to be possible to get seemingly random sequences even from very simple input. This is an example of a very general phenomenon first identified by Stephen Wolfram in the mid1980s, which has nothing directly to do with sensitive dependence on input.
This generates a seemingly random sequence, even starting from simple input:
After the values have been computed, one can safely find numerical approximations to them:
Here are the last 5 results after 1000 iterations, computed using exact numbers:
Using machineprecision numbers gives completely incorrect results:
Many kinds of iterative procedures yield functions that depend sensitively on their input. Such functions also arise when one looks at solutions to differential equations. In effect, varying the independent parameter in the differential equation is a continuous analog of going from one step to the next in an iterative procedure.
This finds a solution to the Duffing equation with initial condition 1:
Here is a plot of the solution:
Here is the same equation with initial condition 1.001:
The solution progressively diverges from the one shown above:

Related Guides

Top

[8]ページ先頭

©2009-2025 Movatter.jp