Re: Re: Beginner question: operating on piecewise defined functions
- To: mathgroup at smc.vnet.net
- Subject: [mg41699] Re: [mg41684] Re: Beginner question: operating on piecewise defined functions
- From: David Withoff <withoff at wolfram.com>
- Date: Sat, 31 May 2003 06:07:48 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>>>>>> "David" == David W Cantrell <DWCantrell at sigmaxi.org> writes: > David> Jan Rychter <jan at rychter.com> wrote: >>> If I define a piecewise function as, say: >>> >>> f[x_] := 1/x^2 /; x >= 1 f[x_] := 1 /; x < 1 >>> >>> then how can I get Mathematica to operate on it, as in: >>> >>> Limit[f[x], {x->Infinity}] >>> >>> Just trying that returns the expression unevaluated > >David> Good question! > >David> I had thought that the answer might lie in rewriting your >David> function in terms of the UnitStep function: 1 + (-1 + >David> x^(-2))*UnitStep[-1 + x] . Alas, that doesn't work; again the >David> limit is returned unevaluated. (BTW, my rewriting above isn't >David> quite equivalent to your function since my form is undefined at >David> x = 0.) > >David> Here's what does work (except again at x = 0): Rewrite your >David> function as (1 + x^2 - (1 + x)*Abs[-1 + x])/(2*x^2) . >David> Thankfully, Mathematica can find the limit of that as x -> >David> Infinity. > >David> But there must be a better way. I'll be interested in seeing >David> other replies. > >I'd like to thank everyone that responded. But I'm still surprised that >Mathematica doesn't do this out of the box. The Mathematica Book >encourages one to define functions using /; -- and yet one can't do much >with such a definition later on. > >This is even more surprising because in most cases it shouldn't pose a >problem for Mathematica -- obviously when I'm asking for a limit at >Infinity, that could be matched to the restrictions and the appropriate >function form could be chosen? > >I wonder if that an area where Mathematica will be improved in the future. The answer to this and several related questions can be seen by recognizing that f[x_] := 1/x^2 /; x >= 1 f[x_] := 1 /; x < 1 is a program that defines f[x], and noting that there is a huge conceptual distinction between computing limits (or doing almost anything else) with programs and doing the same tasks with ordinary closed-form expressions. For example, a basic calculus book will tell you how to solve a problem like "given f(x) equal to cos(x), come up with a new function that gives the derivative of f(x)", but it is quite a different thing to solve a problem like "given a program that computes f(x), write a new program that computes the derivative of f(x)." There are two ways for the computer to work out this limit: either it must run the program (that is, evaluate f[x] for selected values of x) or it must examine the program and work out how that program will behave when it is run. The first approach means computing the limit numerically. You can get this using NLimit. Although the symbolic Limit function could in principle invoke NLimit automatically, it is not at all clear if this would be desirable, for a whole collection of reasons, not the least of which is that this would inject all of the problems and complexities of numerical error into the calculation. The second approach (examining the program) is conspicuously impractical in general, since the program that defines f[x] could be arbitrarily large and complicated. A procedure could in principle be written to handle the particular special case presented here -- a program consisting of a set of rules with simple inequality conditions -- but even that case is not easy, and this would handle only one special case out of an infinite number of possible programs. Quite a lot of work has been done on automatically differentiating programs, integrating programs, and so forth, but the practical answer to this question is no, it is not likely that there will be general computer programs that can do this sort of thing in the foreseeable future.