[PYTHON] The tangent equation problem (high school level) is troublesome so I can solve it

Introduction

Differentiation is a frequently used operation in the field of artificial intelligence. This time, I will touch on the tangent problem in ordinary differential equations. Since it is a high school student level, it is assumed that it can be solved on paper.

environment

・ Jupyter notebook ・ Julia 1.4.0 ・ Python 3.

Tangent equation problem

Function f(x) = 3x^2+4x-5 x=Find the tangent equation at 1.

Thinking on paper

The formula for the tangent equation is

x=The tangent to a is
f(x)−f(a)=f'(a)(x−a)

This formula has a slope f'(a) and is translated. You can find each item of this. In this problem, a = 1.

\begin{align}
&f (x) = y\\
&f'(x)=6x+4\\
&f (a) = f(1) =3.1^2+4.1-5=2\\
&f'(a)=f'(1)=6+4=10
\end{align}

For the tangent equations from these,

y = 10*x - 8

Will be.

Differentiate and tangent equation

python

import sympy as sym
from sympy.plotting import plot
sym.init_printing(use_unicode=True)

#Original function
def originfunc(x):
    return 3*x**2+4*x-5


#differential
def diffunc(x):
    dify = sym.diff(originfunc(x))
    return dify

if __name__ == "__main__":
    x = sym.symbols('x')
    y_1 = originfunc(1)
    print(y_1)
    #=>2
    dify_x = diffunc(x)
    print(dify_x)
    #=>6*x + 4
    dify_1 = dify_x.subs(x, 1)
    print(dify_1)
    #=>10
    #Tangent equation
    y = dify_1*(x - 1) + y_1
    print('y =',y)
    #=>y = 10*x - 8

result

On the contrary, it was annoying for sympy beginners.

Recommended Posts

The tangent equation problem (high school level) is troublesome so I can solve it
Explaining the classic machine learning "Support Vector Machine (SVM)" so that even high school students can understand it
I tried to make OneHotEncoder, which is often used for data analysis, so that it can reach the itch.
I thought "What is Linux?", So I looked it up.
I tried to solve the problem with Python Vol.1