## sympy example – finding tangent lines to a function

Chapter 2: Derivatives
Exercises 2.2, p130
43. Find the tangents to Newton’s Serpentine (graphed here) at the origin and the point (1, 2).
$y = \frac{4 x}{x^{2} + 1}$

y = (4*x) / (x**2 + 1)

# find the derivative of y
dy = cancel(diff(y, x))

# find y coordinate of x at 0
p0 = y.subs(x, 0)

# find slopes of the tangents
m0 = dy.subs(x, 0)
m1 = dy.subs(x, 1)

y0 = 4*x
y1 = 2
plot(y, y0, y1, (x, -5, 5), ylim=(-4, 4))


$\begin{pmatrix}\frac{4 x}{x^{2} + 1}, & - \frac{4 x^{2} - 4}{x^{4} + 2 x^{2} + 1}\end{pmatrix}$

47. a) Find an equation for the line that is tangent to the curve $x^{3} - x$ at point (-1, 0)
c) Confirm your estimates of the coordinates of the second intersection point by solving the equations for the curve
and tangent simultaneously

y = x**3 - x

# find the derivative of y
dy = diff(y, x)

# find slope of the tangent line and equation
m = dy.subs(x, -1)
y1 = m*(x + 1)

# draw the curve and line
plot(y, y1, (x, -5, 5), ylim=(-10, 10))

# above line equation = the curve function, 2x + 2 = x**3 - x
rt = solve(x**3 - 3*x - 2, x)
print rt
# the roots are -1 and 2
[-1, 2]

# checking the roots better use boolean operation but visual inspection is simple
y1.subs(x, -1), y.subs(x, -1), y1.subs(x, 2), y.subs(x, 2)
# Output[105]: (0, 0, 6, 6)


## About janpenguin

Email: janpenguin [at] riseup [dot] net Every content on the blog is made by Free and Open Source Software in GNU/Linux.
This entry was posted in Python, Science and technologies and tagged , , , , . Bookmark the permalink.