Wednesday, September 14, 2016

Calculus Finding Slope of An Equation and Finding Tangent Equation using Secant Line


TODO:
Fix formatting and check for errors

Some Ideas:

Here I want to find equation tangent of the equation $ y = \frac{x^2}{4} - 1 $. The tangent line just touches the curve at that point, but does not cross it. We know the the equation of a straight line is $ y = mx + c $, where, $ m $ is the slope. The slope can be calculated for two points $ (x_1, y_1) $ and $ (x_2, y_2) $ using, $ m = \frac{\Delta{y}}{\Delta{x}} = \frac{y_2 - y_1}{x_2 - x_1} $. Example: calculate $ y = f(x) = 4x + 1 $

Finding Slope, Tangent, Secant Equation of Straight Line:



The graph above is for the equation, $ y = 4x + 1 $. Here, I want to find the slope between two points $ (5,f(5)) = (5, 21) $ and $ (7,f(7)) = (7, 29) $.
Using the rise over run formula,
$ m = \frac{29 - 21}{7 - 5} $
$ \ \ = \frac{8}{2} $
$ \ \ = 4 $
So the equation is now, $ y = mx + c = 4x + c $.

Now, setting any of the points $ x $ and $ y $ value will give the equation of the line, which should be same since the slope isn't changing. Using $ (x = 5, y = 21) $ gives,
$ y = 4x + c $
$ 21 = 4 * 5 + c $
$ c = 1 $
Finally the equation is $ y = f(x) = 4x + c = 4x + 1 $.

This was basically calculating the equation of line from two given points. Finding tangent here was very easy, but the problem comes when the slope is always changing. It is hard to find tangent since the condition is to just touch the given point.

Finding the equation of the Secant Line:

The secant line goes through two points on the curve. For, $ y = f(x) = \frac{x^2}{4} - 1 $. Let us find tangent at $ x = 8 $, at $ (x,f(x)) = (8,f(8)) $ and secant through $ (x_1, y_1)=(3, f(3)) $ and $ (x_2, y_2) = (8,f(8)) $.

Finding the secant is easy. Just use $ m = \frac{\Delta{y}}{\Delta{x}} = \frac{y_2 - y_1}{x_2 - x_1} $. So,
$ m = \frac{f(x_2) - f(x_1)}{x_2 - x_1} = \frac{f(8) - f(3)}{8-3} = \frac{\frac{8^2}{4} - 1 - (\frac{3^2}{4} - 1)}{5} = \frac{\frac{64}{4} - 1 - \frac{9}{4} + 1}{5} = \frac{16-2.25}{5} = 2.75 $

Below I will post animated gif of the graph to show how tangent and secant is changing with various points changing.
I made a desmos demo for this here,
https://www.desmos.com/calculator/9u1bgsocdl



Since, slope $ m = 2.75 $, now we have $ y = 2.75x + c $.
Next we need $ c $. By setting $ x $ and $ y $ values we can calculate the value of $ c $. Any point will work, but $ x = 8, y = f(8) $ is easier since $ f(8) = \frac{8^2}{4} - 1 = 15 $ is an integer.

$ y = f(x) = mx + c $
$ c = f(x) - mx $
$ c = f(x) - \frac{f(x_2) - f(x_1)}{x_2 - x_1} * x $
$ c = f(x) - 2.75 * x $
$ c = f(8) - 2.75 * 8 $
$ c = 15 - 22 $
$ c = -7 $

So the equation of the secant line that goes through $ (3,f(3)) = (3,1.25) $ and $ (8,f(8)) = (8,15) $ is $ y = f(x) = 2.75x - 7 $. Plotting will show that the equation passes through the two points.

Another way to verify is using x = 3 and x = 8 to see if the values of f(x) for each values of x matches.

If x = 3,
$ f(x) = 2.75x - 7 $
$ f(3) = 8.25 - 7 $
$ f(3) = 1.25 $

So this one matches. For x = 8,
$ f(x) = 2.75x - 7 $
$ f(8) = 22 - 7 $
$ f(8) = 15 $
This one matches too.

Summarizing the Equations:

$ y = mx + c $
$ m = \frac{f(x_2) - f(x_1)}{x_2 - x_1} $
$ y = f(x) = \frac{f(x_2) - f(x_1)}{x_2 - x_1} * x + c $
$ c = f(x_2) - m * x_2 $
$ y = f(x) = \frac{f(x_2) - f(x_1)}{x_2 - x_1} * x + f(x_2) - \frac{f(x_2) - f(x_1)}{x_2 - x_1} * x_2 $
$ y = f(x) = m * x + f(x_2) - m * x_2 $
In the equation above to calculate $ c $, I have used $ x_2 $, but it can be replaced with any $ x $.



Without the vertical and horizontal lines,

Animated Gif for Secant

Notice the equation of the secant changes as the two points change. Here I kept one point stationary, but changing both points will still result in an equation that goes through both the points.


Now the task is to find the tangent Line Equation:

In the graph below I have shown points $ (8,f(8)) $ and $ (8.01,f(8.01)) $, but they are so close they can't be separately identified at this level.


Zooming in:

Zooming in further shows the two points separately.


Tangent Line Slope Approximation Table:



$ x = 8 + h $ is very small where $ h \neq 0 $. $ x = 8 $ and $ 8 + h $ is really really close. So the slope of the secant line,
$ \frac{\frac{(8+h)^2}{4} - 1 - (\frac{8^2}{4} - 1)}{8 + h - 8} = \frac{16 + \frac{16h}{4} + \frac{h^2}{4} - 16}{h} = 4 + \frac{h}{4} $

As $ h $ becomes close to $ 0 $, the slope gets closer to 4. So,
$ lim_{h \to 0} (4 + \frac{h}{4}) = 4 $ at, $(x,f(x)) = (8,f(8)) = (8,15)$. So the tangent line slope is 4.

The equation is $ y = 4x + c $. Now c is needed. So,
$ \frac{y_2 - y_1}{x_2 - x_1} = \frac{f(x + h) - f(x)}{x + h - x} = \frac{f(x + h) - f(x)}{h} $

This equation $ \frac{f(x + h) - f(x)}{h} $ is the slope of secant line through $ (x,f(x)) $ and $ (x+h,f(x+h)) $.
$ f'(x) $ defines slope of tangent at $ (x,f(x)) $, so,
$ f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} $.

The slope of both tangent and secant then,
$ \frac{f(x + h) - f(x)}{h} = \frac{m(x+h)+c - (mx+c)}{h} = \frac{mh}{h} = m $

The equation of the tangent at $ (8,15) $ can be calculated by setting x and y to the calculated equation, $ y = 4x + c $.
$ 15 = 4 * 8 + c $
$ c = -17 $

So the equation of the tangent is,
$ y = 4x - 17 $

It can be calculated directly. Previously the equation was, $ y = mx + c $, but now $ f'(x) $ defines the slope of the equation. So replace m with $ f'(x) $.
We have, $ y = \frac{x^2}{4} - 1 $
So, $ y' = f'(x) = \frac{x}{2} $
$ f'(8) = \frac{8}{2} = 4 $

c can be calculated using,
$ y = mx + c $
$ y = f'(x_n) * x + c $
$ c = f(x) - f'(x) * x $
So,
$ y = f'(x_n) * x + ( f(x_n) - f'(x_n) * x_n ) $.
For, $ x_n = 8, y_n = f(x_n) = f(8) = 15 $. So, equation of tangent line at (8,15).
$ y = f'(8) * x + ( f(8) - f'(8) * 8 ) $
$ y = 4 * x + ( 15 - 4 * 8 ) $
$ y = 4x - 17 $

Animated Gif of Tangent and Secant Changing:



In order to optimize ( maximizing or minimizing value of some function ), here minimization can be done when $ \frac{d}{dx} f(x) = f'(x) = 0 $ So,
$ f'(x) = \frac{d}{dx} ( \frac{x^2}{4} - 1 ) = 0 $
$ \frac{x}{2} = 0 $
$ x = 0 $
Setting $ x = 0 $, $ y = \frac{0^2}{4} - 1 = -1 $.

So, for $ y = \frac{x^2}{4} - 1 $ is minimized at $ (0,-1) $ where slope of tangent line is 0. This technique can be used to fit a line to given points on scatter plot and minimizing the error between points and the line.

No comments: