```python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import sympy

style_name = 'bmh' #bmh
mpl.style.use(style_name)
np.set_printoptions(precision=4, linewidth =150)

style = plt.style.library[style_name]
style_colors = [ c['color'] for c in style['axes.prop_cycle'] ]

sympy.init_printing()
```


```python
from matplotlib.patches import ConnectionPatch
from matplotlib import animation, rc
from IPython.display import HTML

plt.rcParams["figure.figsize"] = (5,7)

def f(x) :
    return  x**3 - 5*x

def fdot(x):
    return 3*x**2 - 5

fig = plt.figure()
fig.set_tight_layout(True)

ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)

step = 60
x = np.linspace(-3, 3, step)
slope_points = np.linspace(-2.5, 2.5, step)

fdotx = fdot(x)
m_idx = np.where(fdotx<0)[0]
p_idx = np.where(fdotx>=0)[0]

ax1.plot(x, f(x), color=style_colors[0], lw=3)
ax2.plot(x, fdotx, color=style_colors[1], lw=3)
ax2.plot(x[m_idx], fdotx[m_idx], color=style_colors[7], lw=3)

#numerical deriv.
method = 'center'
eps = 0.01
itv=0.6
x = slope_points[0]
diff = (f(x+eps/2) - f(x-eps/2))/eps

   
img1_cp,    = ax1.plot(x, f(x), 'o', markersize=10, color=style_colors[0], zorder=3)
img1_slope, = ax1.plot([x-itv, x+itv], [f(x)-diff*itv, f(x)+diff*itv], lw=2, color=style_colors[1])
img1_text  = ax1.text(x + 0.1 ,f(x)+0.2, "Slope {:.2f}".format(diff), fontsize=14)
ax1.set_title(r"$f(x)=x^3 - 5x$", fontsize=16)

img2_cp,    = ax2.plot(x, fdot(x), 'o', markersize=10, color=style_colors[1], zorder=3)
con = ConnectionPatch(xyA=(x, fdot(x)), xyB=(x, f(x)), ls='--', coordsA="data", coordsB="data",
              axesA=ax2, axesB=ax1, color=style_colors[3], lw=2, alpha=0.6,
              arrowstyle="<|-", mutation_scale=20, fc="w", zorder=2)
ax2.add_artist(con)
img2_deriv = ax2.text(x + 0.1 ,fdot(x)+0.2, "Value {:.2f}".format(fdot(x)), fontsize=14)
ax2.set_title(r"Derivative")

def update(i):
    global con
    
    x = slope_points[i]
    diff = (f(x+eps/2) - f(x-eps/2))/eps
    
    if diff > 0 :
        color = style_colors[1]
    else : 
        color = style_colors[7]

    img1_cp.set_color(color)
    img1_cp.set_data(x, f(x))
    img1_slope.set_color(color)
    img1_slope.set_data([x-itv, x+itv], [f(x)-diff*itv, f(x)+diff*itv])
    img1_text.set_position((x+0.1, f(x)+0.2))
    img1_text.set_text("Slope {:.2f}".format(diff))
    
    img2_cp.set_color(color)
    img2_cp.set_data(x, fdot(x))
    con.remove()
    con = ConnectionPatch(xyA=(x, fdot(x)+0.5), xyB=(x, f(x)-0.5), ls='--', coordsA="data", coordsB="data",
              axesA=ax2, axesB=ax1, color=color, lw=2, alpha=0.6,
              arrowstyle="<|-", mutation_scale=20, fc="w", zorder=2)
    ax2.add_artist(con)
    img2_deriv.set_position((x+0.1, fdot(x)+0.2))
    img2_deriv.set_text("Value {:.2f}".format(fdot(x)))
    
    return img1_cp, img1_slope, img1_text, img2_cp, img2_deriv,

ani = animation.FuncAnimation(fig, update, frames=np.arange(step), interval=30)
#ani.save('deriv-ani.gif', dpi=80, writer='imagemagick')

rc('animation', html='html5')
HTML(ani.to_html5_video())
```


```python
from matplotlib.patches import ConnectionPatch
from matplotlib import animation, rc
from IPython.display import HTML

plt.rcParams["figure.figsize"] = (8,6)

step = 60

x = np.linspace(-4, 4, step)
def f(x):
    return x**3 - 15*x + 30

fig = plt.figure()
fig.set_tight_layout(True)
ax1 = fig.add_subplot(221)

p1x = x[0]; p1y = f(p1x);
p3x = p1y;  p3y = np.log(p3x)

# 원래 그래프, x, y를 바꿔서 그린다.
ax1.plot(f(x), x, lw=3, label="f(x)", color=style_colors[0])
P1, = ax1.plot(p1y, p1x, 'o')
anno = ax1.annotate("", xy=np.array([6, p1x]), xytext=(p1y, p1x),
                fontsize=20,color=style_colors[1],
                arrowprops=dict(arrowstyle="<|-", color=style_colors[0], lw=2, 
                                fc="w", ls="--", alpha=0.6, connectionstyle="arc3,rad=0.0",),
                )
ax1.set_title(r"$y=x^3 - 15x + 30$", fontsize=18)
ax1.set_xlabel("$y$", fontsize=18)
ax1.set_ylabel(r"$x$", fontsize=18)

#같이 그린 그래프
ax2 = fig.add_subplot(222)
ax2.plot(x, f(x), lw=3, label="f(x)", color=style_colors[0])
ax2_1 = ax2.twinx()
ax2_1.plot(x, np.log(f(x)), lw=3, label="log f(x)", color=style_colors[1])
ax2.set_title(r'$f(x)$ and $\logf(x)$', fontsize=18)

# 로그 그래프
ax3 = fig.add_subplot(223)
logx = np.linspace(*ax1.get_xlim(), step)
ax3.set_xlim(ax1.get_xlim())
ax3.plot(logx, np.log(logx), lw=3, label="log(x)", color=style_colors[1])
P3, = ax3.plot(p3x, p3y, 'o', color=style_colors[1])
ax3.set_title(r"$\log(y)$", fontsize=18)
ax3.set_xlabel("$y$", fontsize=18)
ax3.set_ylabel(r"$\log(y)$", fontsize=18)

# 로그 씌운 그래프
ax4 = fig.add_subplot(224, sharey=ax3)
ax4.plot(x, np.log(f(x)), lw=3, label="log f(x)", color=style_colors[1])
P4, = ax4.plot(p1x, p3y, 'o', color=style_colors[1])
ax4.set_xlabel("$x$", fontsize=18)
ax4.set_ylabel(r"$\logf(x)$", fontsize=18)

con1 = ConnectionPatch(xyA=(p3x, p3y), xyB=(p1y, p1x), ls='--', coordsA="data", coordsB="data",
              axesA=ax3, axesB=ax1, color=style_colors[0], lw=2, alpha=0.6,
              arrowstyle="<|-", mutation_scale=20, fc="w", zorder=2)
ax3.add_artist(con1)

con2 = ConnectionPatch(xyA=(p1x, p3y), xyB=(p3x, p3y), ls='--', coordsA="data", coordsB="data",
              axesA=ax4, axesB=ax3, color=style_colors[1], lw=2, alpha=0.6,
              arrowstyle="<|-", mutation_scale=20, fc="w", zorder=2)
ax4.add_artist(con2)

plt.subplots_adjust(hspace=0.5)

def update(i):
    global con1, con2, step
    x = np.linspace(-4, 4, step)
    
    p1x = x[i]; p1y = f(p1x);
    p3x = p1y;  p3y = np.log(p3x)
    
    P1.set_data(p1y, p1x)
    anno.xy     = [6, p1x]
    anno.set_position([p1y, p1x])

    P3.set_data(p3x, p3y)
    P4.set_data(p1x, p3y)
    
    con1.remove()
    con1 = ConnectionPatch(xyA=(p3x, p3y), xyB=(p1y, p1x), ls='--', coordsA="data", coordsB="data",
              axesA=ax3, axesB=ax1, color=style_colors[0], lw=2, alpha=0.6,
              arrowstyle="<|-", mutation_scale=20, fc="w", zorder=2)
    ax3.add_artist(con1)
    
    con2.remove()
    con2 = ConnectionPatch(xyA=(p1x, p3y), xyB=(p3x, p3y), ls='--', coordsA="data", coordsB="data",
              axesA=ax4, axesB=ax3, color=style_colors[1], lw=2, alpha=0.6,
              arrowstyle="<|-", mutation_scale=20, fc="w", zorder=2)
    ax4.add_artist(con2)
    
    return P1, P3, P4

ani = animation.FuncAnimation(fig, update, frames=np.arange(len(x)), interval=30)
#ani.save('log-extrema.gif', dpi=80, writer='imagemagick')

rc('animation', html='html5')
HTML(ani.to_html5_video())

#plt.show()
```


```python
###########################################
# 미분 = 기울기를 나타내는 애니메이션
###########################################

from matplotlib import animation, rc
from IPython.display import HTML

plt.rcParams["figure.figsize"] = (6,6)

x = np.linspace(0,5)

def f(x):
    return 2*x**3 + 5*x**2

a , b = 3, 5 
d  = 0.01

fig = plt.figure()
fig.set_tight_layout(True)

plt.plot(x, f(x), lw=3)
avg, = plt.plot( (a,b), (f(a), f(b)), lw=2) #평균변화율
mp,  = plt.plot(b, f(b), 'ro')
dx,  = plt.plot( (a,b), (f(a), f(a)), lw=2) #dx
dy,  = plt.plot( (b,b), (f(a), f(b)), lw=2) #dy
dx2, = plt.plot( (a,a), (0, f(a)), '--', lw=2 )
dy2, = plt.plot( (b,b), (0, f(a)), '--', lw=2 )
img1_text  = plt.text(1, 300, "Slope {:.2f}".format((f(b)-f(a))/(b-a)), fontsize=14)

plt.xticks( [a, b], ['a', 'b'] );
plt.xticks(fontsize=20)
plt.yticks(fontsize=15)

def update(i):
    global b
    
    if b - a < d/100 :
        return
    else :
        b -= d
        
    #a, f(a)를 지나고 기울기 (f(b)-f(a))/(b-a)인 직선을 x=1,x=5를 양끝으로 하는 직선
    l = ((f(b)-f(a))/(b-a))*(1-a) + f(a)
    r = ((f(b)-f(a))/(b-a))*(5-a) + f(a)
    avg.set_data([1,5], [l,r])
    mp.set_data(b, f(b))
    img1_text.set_text("Slope {:.2f}".format((r-l)/(5-1)))
    
    dx.set_data((a,b), (f(a), f(a)))
    dx2.set_data((a,a), (0, f(a)))
    
    dy.set_data((b,b), (f(a), f(b)))
    dy2.set_data((b,b), (0, f(a)))
    plt.xticks( [a, b], ['a', 'b'] );
    
    return avg

ani = animation.FuncAnimation(fig, update, frames=np.arange(250), interval=20)    
#rc('animation', html='html5')
#HTML(ani.to_html5_video())

ani.save('imgs/diff-ani.gif', dpi=80, writer='imagemagick', fps=60)
#plt.show()
```


```python
###########################################
# 미분 가능을 나타내는 애니메이션
###########################################


from matplotlib import animation, rc
from IPython.display import HTML

plt.rcParams["figure.figsize"] = (6,6)

x = np.linspace(0,5)

def f(x):
    return 2*x**3 + 5*x**2

c, a , b = 1, 3, 5 
d  = 0.01

fig = plt.figure()
fig.set_tight_layout(True)

plt.plot(x, f(x), lw=3)
avg_r, = plt.plot( (a,b), (f(a), f(b)), lw=2) #평균변화율
avg_l, = plt.plot( (c,a), (f(c), f(a)), lw=2)

mp_r, = plt.plot(b, f(b), 'ro')
mp_l, = plt.plot(c, f(c), 'bo')

img1_text  = plt.text(1, 300, "Right slope {:.2f}".format((f(b)-f(a))/(b-a)), fontsize=14)
img2_text  = plt.text(1, 280, "Left  slope {:.2f}".format((f(a)-f(c))/(a-c)), fontsize=14)

plt.xticks( [c, a, b], ['c', 'a', 'b'] );
plt.xticks(fontsize=20)
plt.yticks(fontsize=15)

def update(i):
    global b
    global c
    
    if b - a < (d / 100) :
        return
    else :
        b -= d
        c += d
        
    #a, f(a)를 지나고 기울기 (f(b)-f(a))/(b-a)인 직선을 x=1,x=5를 양끝으로 하는 직선
    l = ((f(b)-f(a))/(b-a))*(1-a) + f(a)
    r = ((f(b)-f(a))/(b-a))*(5-a) + f(a)
    avg_r.set_data([1,5], [l,r])
    img1_text.set_text("Right slope {:.2f}".format((r-l)/(5-1)))
    
    l = ((f(c)-f(a))/(c-a))*(1-a) + f(a)
    r = ((f(c)-f(a))/(c-a))*(5-a) + f(a)
    avg_l.set_data([1,5], [l,r])
    img2_text.set_text("Left  slope {:.2f}".format((r-l)/(5-1)))
    
    mp_r.set_data(b, f(b))
    mp_l.set_data(c, f(c))
    
    #dx.set_data((a,b), (f(a), f(a)))
    #dx2.set_data((a,a), (0, f(a)))
    
    #dy.set_data((b,b), (f(a), f(b)))
    #dy2.set_data((b,b), (0, f(a)))
    
    plt.xticks( [c, a, b], ['c', 'a', 'b'] );
    
    #return avg

ani = animation.FuncAnimation(fig, update, frames=np.arange(250), interval=20)    
#rc('animation', html='html5')
#HTML(ani.to_html5_video())

ani.save('imgs/diff-ani-psb.gif', dpi=80, writer='imagemagick', fps=60)
#plt.show()
```


```python
###########################################
# 미분 불가능을 나타내는 애니메이션
###########################################


from matplotlib import animation, rc
from IPython.display import HTML

plt.rcParams["figure.figsize"] = (6,6)

x = np.linspace(0,5)

def f(x):
    return (2*x**3 + 5*x**2) + 20*np.abs(x-3)

c, a , b = 1, 3, 5 
d  = 0.01

fig = plt.figure()
fig.set_tight_layout(True)

plt.plot(x, f(x), lw=3)
avg_r, = plt.plot( (a,b), (f(a), f(b)), lw=2) #평균변화율
avg_l, = plt.plot( (c,a), (f(c), f(a)), lw=2)

mp_r, = plt.plot(b, f(b), 'ro')
mp_l, = plt.plot(c, f(c), 'bo')

img1_text  = plt.text(1, 300, "Right slope {:.2f}".format((f(b)-f(a))/(b-a)), fontsize=14)
img2_text  = plt.text(1, 280, "Left  slope {:.2f}".format((f(a)-f(c))/(a-c)), fontsize=14)

plt.xticks( [c, a, b], ['c', 'a', 'b'] );
plt.xticks(fontsize=20)
plt.yticks(fontsize=15)

def update(i):
    global b
    global c
    
    if b - a < (d / 100) :
        return
    else :
        b -= d
        c += d
        
    #a, f(a)를 지나고 기울기 (f(b)-f(a))/(b-a)인 직선을 x=1,x=5를 양끝으로 하는 직선
    l = ((f(b)-f(a))/(b-a))*(1-a) + f(a)
    r = ((f(b)-f(a))/(b-a))*(5-a) + f(a)
    avg_r.set_data([1,5], [l,r])
    img1_text.set_text("Right slope {:.2f}".format((r-l)/(5-1)))
    
    l = ((f(c)-f(a))/(c-a))*(1-a) + f(a)
    r = ((f(c)-f(a))/(c-a))*(5-a) + f(a)
    avg_l.set_data([1,5], [l,r])
    img2_text.set_text("Left  slope {:.2f}".format((r-l)/(5-1)))
    
    mp_r.set_data(b, f(b))
    mp_l.set_data(c, f(c))
    
    #dx.set_data((a,b), (f(a), f(a)))
    #dx2.set_data((a,a), (0, f(a)))
    
    #dy.set_data((b,b), (f(a), f(b)))
    #dy2.set_data((b,b), (0, f(a)))
    
    plt.xticks( [c, a, b], ['c', 'a', 'b'] );
    
    #return avg

ani = animation.FuncAnimation(fig, update, frames=np.arange(250), interval=20)    
#rc('animation', html='html5')
#HTML(ani.to_html5_video())

ani.save('imgs/diff-ani-impsb.gif', dpi=80, writer='imagemagick', fps=60)
#plt.show()
```


```python
%%html
<link href='https://fonts.googleapis.com/earlyaccess/notosanskr.css' rel='stylesheet' type='text/css'>
<!--https://github.com/kattergil/NotoSerifKR-Web/stargazers-->
<link href='https://cdn.rawgit.com/kattergil/NotoSerifKR-Web/5e08423b/stylesheet/NotoSerif-Web.css' rel='stylesheet' type='text/css'>
<!--https://github.com/Joungkyun/font-d2coding-->
<link href="http://cdn.jsdelivr.net/gh/joungkyun/font-d2coding/d2coding.css" rel="stylesheet" type="text/css">
<style>
    h1     { font-family: 'Noto Sans KR' !important; color:#348ABD !important;   }
    h2     { font-family: 'Noto Sans KR' !important; color:#467821 !important;   }
    h3, h4 { font-family: 'Noto Sans KR' !important; color:#A60628 !important;   }
    p:not(.navbar-text) { font-family: 'Noto Serif KR', 'Nanum Myeongjo'; font-size: 12pt; line-height: 200%;  text-indent: 10px; }
    li:not(.dropdown):not(.p-TabBar-tab):not(.p-MenuBar-item):not(.jp-DirListing-item):not(.p-CommandPalette-header):not(.p-CommandPalette-item):not(.jp-RunningSessions-item)
            { font-family: 'Noto Serif KR', 'Nanum Myeongjo'; font-size: 12pt; line-height: 200%; }
    table  { font-family: 'Noto Sans KR' !important;  font-size: 11pt !important; }           
    li > p  { text-indent: 0px; }
    li > ul { margin-top: 0px !important; }       
    sup { font-family: 'Noto Sans KR'; font-size: 9pt; } 
    code, pre  { font-family: D2Coding, 'D2 coding' !important; font-size: 12pt !important; line-height: 130% !important;}
    .code-body { font-family: D2Coding, 'D2 coding' !important; font-size: 12pt !important;}
    .ns        { font-family: 'Noto Sans KR'; font-size: 15pt;}
    .summary   {
                   font-family: 'Georgia'; font-size: 12pt; line-height: 200%; 
                   border-left:3px solid #FF0000; 
                   padding-left:20px; 
                   margin-top:10px;
                   margin-left:15px;
               }
    .green { color:#467821 !important; }
    .comment { font-family: 'Noto Sans KR'; font-size: 10pt; }
</style>
```


<link href='https://fonts.googleapis.com/earlyaccess/notosanskr.css' rel='stylesheet' type='text/css'>
<!--https://github.com/kattergil/NotoSerifKR-Web/stargazers-->
<link href='https://cdn.rawgit.com/kattergil/NotoSerifKR-Web/5e08423b/stylesheet/NotoSerif-Web.css' rel='stylesheet' type='text/css'>
<!--https://github.com/Joungkyun/font-d2coding-->
<link href="http://cdn.jsdelivr.net/gh/joungkyun/font-d2coding/d2coding.css" rel="stylesheet" type="text/css">
<style>
    h1     { font-family: 'Noto Sans KR' !important; color:#348ABD !important;   }
    h2     { font-family: 'Noto Sans KR' !important; color:#467821 !important;   }
    h3, h4 { font-family: 'Noto Sans KR' !important; color:#A60628 !important;   }
    p:not(.navbar-text) { font-family: 'Noto Serif KR', 'Nanum Myeongjo'; font-size: 12pt; line-height: 200%;  text-indent: 10px; }
    li:not(.dropdown):not(.p-TabBar-tab):not(.p-MenuBar-item):not(.jp-DirListing-item):not(.p-CommandPalette-header):not(.p-CommandPalette-item):not(.jp-RunningSessions-item)
            { font-family: 'Noto Serif KR', 'Nanum Myeongjo'; font-size: 12pt; line-height: 200%; }
    table  { font-family: 'Noto Sans KR' !important;  font-size: 11pt !important; }           
    li > p  { text-indent: 0px; }
    li > ul { margin-top: 0px !important; }       
    sup { font-family: 'Noto Sans KR'; font-size: 9pt; } 
    code, pre  { font-family: D2Coding, 'D2 coding' !important; font-size: 12pt !important; line-height: 130% !important;}
    .code-body { font-family: D2Coding, 'D2 coding' !important; font-size: 12pt !important;}
    .ns        { font-family: 'Noto Sans KR'; font-size: 15pt;}
    .summary   {
                   font-family: 'Georgia'; font-size: 12pt; line-height: 200%; 
                   border-left:3px solid #FF0000; 
                   padding-left:20px; 
                   margin-top:10px;
                   margin-left:15px;
               }
    .green { color:#467821 !important; }
    .comment { font-family: 'Noto Sans KR'; font-size: 10pt; }
</style>


cf0e3ff293c37438ed3587b773baa7407a6c45fa
ipynb
Jupyter Notebook
study/mathematics-for-ml/datablocklab/class-02/ani.ipynb
data-block/dev
e7a71eda6f1c4f18b262aa07899577529b7e827c
MIT
2019-10-29T12:19:45.000Z
2020-01-17T02:36:57.000Z
study/mathematics-for-ml/datablocklab/class-02/ani.ipynb
data-block/dev
e7a71eda6f1c4f18b262aa07899577529b7e827c
MIT
study/mathematics-for-ml/datablocklab/class-02/ani.ipynb
data-block/dev
e7a71eda6f1c4f18b262aa07899577529b7e827c
MIT
Qwen/Qwen-72B
1. YES
2. YES