Up to Main Lab Page | Next Lesson - Taylor Series | Previous Lesson - Text in Maple |
For example, consider the function sqrt(x). If x is a perfect square the value can be found exactly, however if x is not a perfect square we know that the result will be irrational. Thus any evaluation of sqrt(x) will be an approximation of the true value.
So if you are given the job of implementing the sqrt function in an application you must figure out some way of approximating the value, quickly and efficiently. Of course most compilers have built in sqrt functions, but somebody had to program these functions in the first place. In addition these arguments hold for a wide range of functions which may or may not be standard compiler functions.
Most approximations of functions happen 'about a point a', the further away from a we go the worse the approximation will be.
The simplest approximation is to assume that a function is in fact equal to its value at a nearby point, this is the constant approximation. If we know the value of a function, f, at some point, a, we may assume that it is close to the value of f (a) near a.
Of course the constant approximation is not a very good approximation, especially if the slope of the function is steep. A better approximation is to assume that the function is a straight line. The obvious line to take is the tangent line at a, since this will allow for the slope of f near a.
The equation of the tangent line to a function f at a point a is:
y = f '(a) (x - a) + f (a)We assume that f is close to the tangent line, that is:
f (x) ~ f '(a) (x - a) + f (a)
So our approximation will be
sqrt(x) | ~ | ¼ (x - 4) + 2. |
= | ¼ x + 1. |
Lets try out our approximation, comparing it with the true value.
> f := x -> sqrt(x);
> ap := x -> x/4 + 1;
> plot([f(x), ap(x)], x = 1..6, color=[red,blue]);
> [evalf(4 + i/10), evalf(f(4 + i/10)), evalf(ap(4 + i/10)),
evalf(f(4 + i/10) - ap(4 + i/10))] $ i = -5..5;
From the output we can see that this is not a bad approximation, giving roughly
2 digits accuracy on a range 4 ± 0.5.
This shows us that the linear approximation will be good when
f ''(x) is small.
If we know a bound for f ''(x) on some interval then we can
bound our error.
That is, if | f ''(x) | < M on I,
then | En(x) | < M | x - a |2
on I.
For example, in the case of f (x) = sqrt(x),
D(f )(x) = 1/(2 sqrt(x)),
D(D(f ))(x) = -1/(4 sqrt(x3) < 1/32, if x > 4.
Thus En(x) < 1/32 | x - a |2
for x > 4.
The reason that the linear approximation works well is that the approximation has the
same value at a, and the same slope.
It seems reasonable that we would get an even better approximation if in
addition the curvature at a was the same as that of the function.
The curvature is measured by the second derivative of f.
Another way of thinking of this is that a linear approximation involves a linear term (x - a), a qudratric approximation will involve the qudratic term (x - a)2. Thus we wish to find constants A, B and C such that
Consider P (a) = A.
If we want P (a) = f (a)
we must have that A = f (a).
Consider P '(x) = B + 2C(x - a),
so P '(a) = B, so B = f '(a).
Consider P ''(x) = 2C,
so P '(a) = 2C,
so C = ½ f ''(a).
Thus the quadratic approximation is given by
The approximation will be better the less the second derivative changes. Thus this is good for functions like sqrt and ln which have relatively slowly changing second derivatives. A function like sin on the other hand has a very variable second derivative and so the approximation will fail more quickly.
Thus f (x) ~ 2 + ¼ (x - 4) - 1/64 (x - 4)2
Using Maple simplify we get that this is 3/4+3/8*x-1/64*x^2.
Lets try some values:
> f := x -> sqrt(x);
> ap := 2 + (x - 4)/4 - (x - 4)^2/64;
> plot([f(x), ap(x)], x = 1..6, color=[red,blue]);
> [evalf(4 + i/10), evalf(f(4 + i/10)), evalf(ap(4 + i/10)),
evalf(f(4 + i/10) - ap(4 + i/10))] $ i = -5..5;
We can see that we did indeed get a better approximation, generally 3 digits instead of 2. Near the point the approximation is even better still.
For another example, consider the function ln(x), near 1.
> f := x -> ln(x);
> A := f(1);
> B := D(f)(1);
> C := D(D(f))(1)/2;
> ap := x -> A + B*(x - 1) + C*(x - 1)^2;
> plot([f(x), ap(x)], x = .5..2, color=[red,blue]);
> [evalf(1 + i/10), evalf(f(1 + i/10)), evalf(ap(1 + i/10)),
evalf(f(1 + i/10) - ap(1 + i/10))] $ i = -5..5;
This shows us that the linear approximation will be good when
f '''(x) is small,
i.e. the second derivative is changing slowly.
If we know a bound for f '''(x) on some interval then we can
bound our error.
That is, if | f '''(x) | < M on I,
then | En(x) | <
M | x - a |3 on I.
In the case of f(x) = sqrt(x),
D(f )(x) = 1/(2 sqrt(x)),
D(D(f ))(x) = -1/(4 sqrt(x3)
(D@@3)(f)(x) = 3/(8 sqrt(x5)
So (D@@3)(f)(5) = 1/256.
Thus En(x) < 1/256 (x - a)3,
for x > 4.
Up to Main Lab Page | Next Lesson - Taylor Series | Previous Lesson - Text in Maple | Top of this Lesson |