CoCalc Public Files06 - Linear and Quadratic Approximation Assignment / Linear and Quadratic Approximation Notes.sagewsOpen with one click!
Author: Aaron Tresham
Views : 453
License: Other -- explicitly state in your code
Compute Environment: Ubuntu 20.04 (Default)
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


  • Intro to Sage
  • Tangent Lines
  • Differentiation

Linear and Quadratic Approximation

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.


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).

Let's start with a relatively simple example.

Example 1

Suppose we want to approximate 283\sqrt[3]{28} 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 x=27x=27. This value is fairly close to 28, and it is easy to compute 273=3\sqrt[3]{27}=3 (27 is a perfect cube).

Now let's compute the tangent line and use this line to approximate 283\sqrt[3]{28}.

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

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

Find the derivative:

13x23\displaystyle \frac{1}{3 \, x^{\frac{2}{3}}}

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

In this case, f(x)=13x2/3\displaystyle f'(x)=\frac{1}{3x^{2/3}}, so f(27)=13272/3=1332=127\displaystyle f'(27)=\frac{1}{3\cdot27^{2/3}}=\frac{1}{3\cdot3^2}=\frac{1}{27}.

Find an equation for the tangent line: TL(x)=f(x0)+f(x0)(xx0)TL(x)=f(x_0)+f'(x_0)(x-x_0)

TL(x)=3+1/27*(x-27); show(TL(x))
127x+2\displaystyle \frac{1}{27} \, x + 2

Just to check our work, let's graph ff and TLTL near x=27x=27. [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 ff near x=27x=27.

Now let's use the tangent line to approximate 283\sqrt[3]{28}.

The tangent line has equation TL(x)=3+127(x27)TL(x)=3+\frac{1}{27}(x-27). Since x3TL(x)\sqrt[3]{x}\approx TL(x) (near x=27x=27), we have 283TL(28)=3+127(2827)=3+127=82273.0370\sqrt[3]{28}\approx TL(28)=3+\frac{1}{27}(28-27)=3+\frac{1}{27}=\frac{82}{27}\approx3.0370.

TL(28); N(_)
82/27 3.03703703703704

Compare this to Sage's approximation of 283\sqrt[3]{28}:


Our approximation is off by about 3.037037037037043.03658897187566=0.0004480651613798513.03703703703704-3.03658897187566=0.000448065161379851, for a percent error of around 0.0148%0.0148\%.

Note: percent error =approximationactualactual100=\displaystyle\left|\frac{approximation-actual}{actual}\right|\cdot100

Suppose we would like to find 293\displaystyle\sqrt[3]{29} as well?

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

293TL(29)=3+127(2927)=3+227=83273.0741\sqrt[3]{29}\approx TL(29)=3+\frac{1}{27}(29-27)=3+\frac{2}{27}=\frac{83}{27}\approx3.0741.

TL(29); N(_)
83/27 3.07407407407407

Sage gives us 2933.0723\sqrt[3]{29}\approx 3.0723.


This gives a percent error of about 0.0572%0.0572\%


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

Here is a graph of the percent error versus xx. 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 sin(π5)\sin\left(\frac{\pi}{5}\right). This is not one of the exact values we learned in precalc, but π5\frac{\pi}{5} is not too far from π6\frac{\pi}{6}, and we recall from precalc that sin(π6)=12\sin\left(\frac{\pi}{6}\right)=\frac{1}{2}. So we'll use (π6,12)\left(\frac{\pi}{6},\frac{1}{2}\right) 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.


The lecture video is based on an earlier version of CoCalc. Notice the uses of the print command below. In the video the print commands do not use parentheses, but in the current version of CoCalc (August 2020) parentheses are required. This problem may appear in later lessons as well. My apologies.

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 0.5%0.5\%.

To check our work, here's a graph of ff and TLTL near x=π6x=\frac{\pi}{6}.

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 x=π4x=\frac{\pi}{4}? This is close to π5\frac{\pi}{5} and we know the exact value of sin(π4)\sin\left(\frac{\pi}{4}\right) 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 1.4%1.4\%. This is not surprising, since π40.785\frac{\pi}{4}\approx0.785 is further away from π50.628\frac{\pi}{5}\approx0.628 than π60.524\frac{\pi}{6}\approx0.524.

Quadratic Approximation

Suppose we have a function f(x)f(x) and its tangent line TL(x)TL(x) at the point (x0,f(x0))(x_0,f(x_0)).

Recall, an equation of the tangent line is TL(x)=f(x0)+f(x0)(xx0)TL(x)=f(x_0)+f'(x_0)(x-x_0). The derivative of TLTL is the constant f(x0)f'(x_0) (the derivative of a linear function is a constant, equal to its slope). In other words, at the point (x0,f(x0))(x_0,f(x_0)), the functions ff and TLTL have the same derivative (or slope). [The point of the tangent line in the first place was to find the slope of ff 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 ff at the point (x0,f(x0))(x_0,f(x_0)). 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 ff and TLTL have the same first derivative at x=x0x=x_0, we will choose our tangent parabola so that it has the same first and second derivatives as ff at x=x0x=x_0.

Let's call our quadratic function P(x)=A(xx0)2+B(xx0)+CP(x)=A\cdot(x-x_0)^2+B\cdot(x-x_0)+C, for some constants AA, BB, and CC. Our goal is to find AA, BB, and CC so that PP is "close to" ff near the point (x0,f(x0))(x_0,f(x_0)).

[Using (xx0)(x-x_0) instead of just xx will make our calculations easier. Note the presence of (xx0)(x-x_0) in the tangent line formula as well.]

Note the following facts:

  1. P(x0)=A(x0x0)2+B(x0x0)+C=CP(x_0)=A\cdot(x_0-x_0)^2+B\cdot(x_0-x_0)+C=C

  2. P(x)=2A(xx0)+BP'(x)=2A\cdot(x-x_0)+B, so P(x0)=2A(x0x0)+B=BP'(x_0)=2A\cdot(x_0-x_0)+B=B

  3. P(x)=2AP''(x)=2A, so P(x0)=2AP''(x_0)=2A.

So, if PP and ff are to have the same (1) function value, (2) first derivative, and (3) second derivative at x=x0x=x_0, we need C=f(x0)C=f(x_0), B=f(x0)B=f'(x_0), and A=f(x0)2A=\frac{f''(x_0)}{2}.

Therefore, an equation for the "tangent parabola" is P(x)=f(x0)2(xx0)2+f(x0)(xx0)+f(x0)P(x)=\frac{f''(x_0)}{2}\cdot(x-x_0)^2+f'(x_0)\cdot(x-x_0)+f(x_0)

[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 283\sqrt[3]{28}.

Solution: The function ff we are trying to approximate is the cube root function, so f(x)=x3f(x)=\sqrt[3]{x}. Just like in Example 1 above, we will use (27,3)(27,3) as our point of tangency (because 273\sqrt[3]{27} is easy to compute).

We want to find the "tangent parabola" P(x)=A(xx0)2+B(xx0)+CP(x)=A\cdot(x-x_0)^2+B\cdot(x-x_0)+C.

We need C=f(27)=3C=f(27)=3, B=f(27)=127B=f'(27)=\frac{1}{27}, and A=f(27)2=12187A=\frac{f''(27)}{2}=-\frac{1}{2187}.

Thus, P(x)=12187(x27)2+127(x27)+3P(x)=-\frac{1}{2187}\cdot(x-27)^2+\frac{1}{27}\cdot(x-27)+3.

Now we can approximate 283=f(28)P(28)=12187(2827)2+127(2827)+3=12187+127+3=66412187\sqrt[3]{28}=f(28)\approx P(28)=-\frac{1}{2187}\cdot(28-27)^2+\frac{1}{27}\cdot(28-27)+3=-\frac{1}{2187}+\frac{1}{27}+3=\frac{6641}{2187}

3.03657978966621\approx 3.03657978966621

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 0.0003%0.0003\% (compared with 0.0148%0.0148\% 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)

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.


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 1%1\% for the same range of x-values - now we have percent errors less than 0.15%0.15\%.

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 sin(π5)\sin\left(\frac{\pi}{5}\right). Use (π6,12)\left(\frac{\pi}{6},\frac{1}{2}\right) 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.587948411433630 Sage gives: 0.587785252292473 The percent error is: 0.0277582910630994 %

Compare this percent error with the 0.494179788946525%0.494179788946525\% 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 ff at x=x0x=x_0. 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.