다음 그림들은 전자책 파이썬과 함께하는 미분적분의 5.4장에 수록된 그래프들과 코드들입니다.
import numpy as np import pandas as pd from sympy import * import matplotlib.pyplot as plt import seaborn as sns sns.set_style("darkgrid")
#그림 5.4.1 a=symbols('a') f=a**3-3*a+2 x=np.linspace(-3, 3, 100) y=[f.subs(a, i) for i in x] df=f.diff(a) dy=[df.subs(a, i) for i in x] plt.figure(figsize=(4, 3)) plt.plot(x, y, color="g", label="f(x)") plt.plot(x, dy, ls="dashed", color="b", alpha=0.3, label=r"$\frac{df(x)}{dx}$") col=[("b", "f(-2): Lower Int"), ("r", "f(-1): critical)"),('r', "f(1): critical"), ('b', "f(1.5): Upper Int")] for i, j in enumerate([-2, -1, 1, 1.5]): plt.scatter(j, f.subs(a, j), s=50, c=col[i][0]) if j<0: plt.text(j+0.2, f.subs(a, j)+0.2, col[i][1], c=col[i][0]) else: plt.text(j+0.2, f.subs(a, j)-0.3, col[i][1], c=col[i][0]) plt.xlabel("x",fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-4, 5)) plt.legend(loc="best", labelcolor=['g',"b"], frameon=False) plt.show()
#그림 5.4.2 a=symbols('a') f=4*a-a**4 df=f.diff(a) x=np.linspace(-2, 3, 100) y=[f.subs(a, i) for i in x] dy=[df.subs(a, i) for i in x] plt.figure(figsize=(4, 3)) plt.plot(x, y, color="g", label="f(x)") plt.plot(x, dy, color="b", ls="dashed", alpha=0.3, label=r"$\frac{df(x)}{dx}$") plt.scatter(1, f.subs(a, 1), s=50, c="r") plt.text(1+0.2, f.subs(a, 1)+0.2, 'f(1)', c="r") plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-20, 20)) plt.legend(loc="best", labelcolor=['g', "b"], frameon=False) plt.show()
#그림 5.4.3 x=symbols('x', real=True) f=5*x+35/x df=f.diff(x) a1, a2=np.linspace(-5, -0.01, 50), np.linspace(0.01, 5, 50) y1, y2=[f.subs(x, i) for i in a1], [f.subs(x, i) for i in a2] dy1, dy2=[df.subs(x, i) for i in a1], [df.subs(x, i) for i in a2] plt.figure(figsize=(4,3)) plt.plot(a1, y1, color="g", label="f(x)") plt.plot(a2, y2, color="g") plt.plot(a1, dy1, ls="dashed", color="b", alpha=0.3, label=r"$\frac{df(x)}{dx}$") plt.plot(a2, dy2, ls="dashed", color="b", alpha=0.3) plt.scatter(np.sqrt(7), f.subs(x, np.sqrt(7)), s=30, c="brown", label="critical point") plt.ylim(-400, 400) plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.legend(loc="best", labelcolor=['g','b','brown']) plt.show()
댓글
댓글 쓰기