Shared06 - Linear and Quadratic Approximation Assignment / Linear and Quadratic Approximation Notes.sagewsOpen in CoCalc
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is

### Prerequisites:

• Intro to Sage
• Tangent Lines
• Differentiation

It is often convenient to approximate a complicated function using a simpler function. The "simpler" function is often a polynomial. The simplest polynomial is a straight line (degree 1). Approximating a function with a linear function is called linearization (or linear approximation). Approximating a function with a degree 2 polynomial (a parabola) is called quadratic approximation. We will discuss these two approximation methods today, and we'll save approximation with higher degree polynomials for Calculus 2.

Of course, it's kind of silly to do linear or quadratic approximation with the computer, since the computer can do much more accurate approximations of these functions. The point of this lab is to see just how accurate these simple approximations can be.

## Linearization

As we have seen several times this semester, differentiable functions are "close to" their tangent lines near the point of tangency. This means that we can approximate any differentiable function with a tangent line (at least over a small interval).

## Example 1

Suppose we want to approximate without a computer or calculator. The cube root function is complicated (at least by hand), so we'll approximate the cube root with a simple linear function, a tangent line.

What point of tangency do we choose? There are two requirements:

1. The x-coordinate needs to be close to 28, since our approximation only works close to the point of tangency.

2. It needs to be simple to compute the function value at this x-coordinate (this is the y-coordinate of the point of tangency, and we don't want to approximate this).

For this example, we will choose the tangent line at . This value is fairly close to 28, and it is easy to compute (27 is a perfect cube).

Now let's compute the tangent line and use this line to approximate .

First, define the function we're trying to approximate:

f(x)=x^(1/3)  #cube root


Find the derivative:

df(x)=derivative(f,x);show(df(x))


Now we need to find to get the slope of the tangent line. If is simple to compute, then is probably simple to compute as well.

In this case, , so .

Find an equation for the tangent line:

TL(x)=3+1/27*(x-27); show(TL(x))


Just to check our work, let's graph and near . [Of course, if we were actually doing approximation by hand, we wouldn't do this step.]

f(x)=x^(1/3);TL(x)=3+1/27*(x-27)
plot(f,xmin=20,xmax=34)+plot(TL,xmin=20,xmax=34,color='red')+point((27,3),size=25,color='black')


As we expected, the tangent line is close to the graph of near .

Now let's use the tangent line to approximate .

The tangent line has equation . Since (near ), we have .

TL(28); N(_)

82/27 3.03703703703704

Compare this to Sage's approximation of :

N(28^(1/3))

3.03658897187566

Our approximation is off by about , for a percent error of around .

Note: percent error

N(abs((TL(28)-f(28))/f(28))*100)

0.0147555420086370

Suppose we would like to find as well?

Since 29 is still fairly close to 27, we can use the same tangent line to get a linear approximation:

.

TL(29); N(_)

83/27 3.07407407407407

Sage gives us .

N(29^(1/3))

3.07231682568585

This gives a percent error of about

N(abs((TL(29)-f(29))/f(29))*100)

0.0571961971348601

This linear approximation is only good when is close to 27.

Here is a graph of the percent error versus . You can see that the error increases as you move away from 27.

f(x)=x^(1/3);TL(x)=3+1/27*(x-27)
plot(abs((TL(x)-f(x))/f(x))*100,xmin=20,xmax=34,axes_labels=['$x$','Percent Error'],fontsize=8)


Even 7 units away from 27 (in either direction), the error is less than 1%. Not bad for an approximation that you could easily do on a napkin over lunch!

## Example 2 (copy and paste for linear approximation)

Now let's use linear approximation to estimate . This is not one of the exact values we learned in precalc, but is not too far from , and we recall from precalc that . So we'll use as our point of tangency.

For this example, we'll let Sage do all the work, but it could be done by hand without too much trouble.

f(x)=sin(x)     #define the function we want to approximate
x_tangent=pi/6  #define the x-coordinate of the point of tangency
x_approx=pi/5   #define the x-value where we are trying to approximate the function

df(x)=derivative(f,x)
TL(x)=f(x_tangent)+df(x_tangent)*(x-x_tangent)
print 'Our approximation is:',N(TL(x_approx))
print ''
print 'Sage gives:',N(f(x_approx))
print ''
print 'The percent error is:',N(abs((TL(x_approx)-f(x_approx))/f(x_approx))*100),'%'

Our approximation is: 0.590689968211711 Sage gives: 0.587785252292473 The percent error is: 0.494179788946536 %

Our approximation is off by about .

To check our work, here's a graph of and near .

f(x)=sin(x);a=pi/6;df(x)=derivative(f,x);TL(x)=f(a)+df(a)*(x-a)
plot(f,xmin=0,xmax=pi/3)+plot(TL,xmin=0,xmax=pi/3,color='red')+point((pi/6,f(pi/6)),color='black',size=25)


Here's a graph of the percent error. You can see that it's much worse than our cube root example above.

f(x)=sin(x);a=pi/6;df(x)=derivative(f,x);TL(x)=f(a)+df(a)*(x-a)
plot(abs((TL(x)-f(x))/f(x))*100,xmin=0.1,xmax=1,axes_labels=['$x$','Percent Error'],fontsize=8)


Just out of curiosity, would would happen if we chose the tangent line at ? This is close to and we know the exact value of as well.

I'll just cut and paste the input from above. The only thing I need to change is x_tangent.

f(x)=sin(x)     #define the function we want to approximate
x_tangent=pi/4  #define the x-coordinate of the point of tangency
x_approx=pi/5   #define the x-value where we are trying to approximate the function

df(x)=derivative(f,x)
TL(x)=f(x_tangent)+df(x_tangent)*(x-x_tangent)
print 'Our approximation is:',N(TL(x_approx))
print ''
print 'Sage gives:',N(f(x_approx))
print ''
print 'The percent error is:',N(abs((TL(x_approx)-f(x_approx))/f(x_approx))*100),'%'

Our approximation is: 0.596034707732588 Sage gives: 0.587785252292473 The percent error is: 1.40348118771963 %

Now I'm off by about . This is not surprising, since is further away from than .

Suppose we have a function and its tangent line at the point .

Recall, an equation of the tangent line is . The derivative of is the constant (the derivative of a linear function is a constant, equal to its slope). In other words, at the point , the functions and have the same derivative (or slope). [The point of the tangent line in the first place was to find the slope of at the point of tangency.] A linear function is completely determined by two pieces of information, in this case (1) the point of tangency, and (2) the slope.

To do quadratic approximation, we want to find a "tangent parabola" for our function at the point . A quadratic function is determined by three pieces of information. Once again we will use the point of tangency and the slope, but we need one more. The key idea is to use the second derivative. Just like and have the same first derivative at , we will choose our tangent parabola so that it has the same first and second derivatives as at .

Let's call our quadratic function , for some constants , , and . Our goal is to find , , and so that is "close to" near the point .

[Using instead of just will make our calculations easier. Note the presence of in the tangent line formula as well.]

Note the following facts:

1. , so

2. , so .

So, if and are to have the same (1) function value, (2) first derivative, and (3) second derivative at , we need , , and .

Therefore, an equation for the "tangent parabola" is

[Notice the equation for the tangent line within this formula. The tangent parabola extends the tangent line with a degree-2 term.]

## Example 3

Use quadratic approximation to estimate .

Solution: The function we are trying to approximate is the cube root function, so . Just like in Example 1 above, we will use as our point of tangency (because is easy to compute).

We want to find the "tangent parabola" .

We need , , and .

Thus, .

Now we can approximate

Compare this to the tangent line approximation from Example 1:

and the approximation from Sage:

This quadratic approximation gives us a percent error of about (compared with from linear approximation).

#Finding the coefficients:
f(x)=x^(1/3)
df(x)=derivative(f,x)
d2f(x)=derivative(f,x,2)
f(27)     #C
df(27)    #B
d2f(27)/2 #A

3 1/27 -1/2187
#Quadratic approximation
P(x)=-1/2187*(x-27)^2+1/27*(x-27)+3
P(28)
n(_)

6641/2187 3.03657978966621
#Percent error
N(abs((P(28)-f(28))/f(28))*100)

0.000302385655022364

Let's look at a graph. The cube root function is in blue, the tangent parabola is in red, and the tangent line is in green.

plot(x^(1/3),xmin=12,xmax=42)+plot(-1/2187*(x-27)^2+1/27*(x-27)+3,xmin=12,xmax=42,color='red')+point((27,3),color='black',size=25)+plot(3+1/27*(x-27),xmin=12,xmax=42,color='green',linestyle='dashed')


Notice how much farther we can get from 27 before the approximation breaks down (compared with the tangent line).

Below is the graph of the percent error for quadratic approximation. Remember that the tangent line had percent errors less than for the same range of x-values - now we have percent errors less than .

f(x)=x^(1/3);P(x)=-1/2187*(x-27)^2+1/27*(x-27)+3
plot(abs((P(x)-f(x))/f(x))*100,xmin=20,xmax=34,axes_labels=['$x$','Percent Error'],fontsize=8)


## Example 4 (copy and paste for quadratic approximation)

Use quadratic approximation to estimate . Use as the point of tangency, as in Example 2 above.

f(x)=sin(x)     #define the function we want to approximate
x_tangent=pi/6  #define the x-coordinate of the point of tangency
x_approx=pi/5   #define the x-value where we are trying to approximate the function

df(x)=derivative(f,x)
d2f(x)=derivative(f,x,2)
P(x)=d2f(x_tangent)/2*(x-x_tangent)^2+df(x_tangent)*(x-x_tangent)+f(x_tangent)
print 'Our approximation is:',N(P(x_approx))
print ''
print 'Sage gives:',N(f(x_approx))
print ''
print 'The percent error is:',N(abs((P(x_approx)-f(x_approx))/f(x_approx))*100),'%'

Our approximation is: 0.587948411433631 Sage gives: 0.587785252292473 The percent error is: 0.0277582910630994 %

Compare this percent error with the we got from linear approximation.

Here's a graph.

f(x)=sin(x);a=pi/6;df(x)=derivative(f,x);TL(x)=f(a)+df(a)*(x-a)
plot(f,xmin=-pi,xmax=pi)+plot(P,xmin=-pi,xmax=pi,ymin=-1.5,color='red')+point((pi/6,1/2),color='black',size=25)+plot(TL,xmin=-pi,xmax=pi,color='green',linestyle='dashed',ymax=1.25,ymin=-1.25)


Here's the percent error graph. Recall, we had errors over 30% with the tangent line.

plot(abs((P(x)-f(x))/f(x))*100,xmin=0.1,xmax=1,axes_labels=['$x$','Percent Error'],fontsize=8)


The next step would be cubic approximation. To get a cubic, we need one more piece of information. We'll make the third derivative of our "tangent cubic" equal to the third derivative of at . You can continue this process as many times as you like to get a polynomial of any degree. We'll come back to this in Calculus 2.