다음 그래프들은 전자책 파이썬과 함께하는 미분적분의 5.2 장에 소개된 그래프와 코드들입니다.
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.2.2 x=np.linspace(-.5, 6, 100) f=(x-2)**4+1 g=(x-2)**3+1 plt.figure(figsize=(4, 3)) plt.plot(x, f, color="g", label="f(x)") plt.plot(x, g, color="b", label="g(x)") plt.hlines(1, -0.5, 6, color="k", ls="--") plt.scatter(2, 1, s=50, c="r", label="crtical point") plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-11, 11)) plt.legend(loc="best", labelcolor=['g','b','r'], frameon=False) plt.show()
#그림 5.2.3 a=symbols("a", real=True) f=3*a**5-20*a**2-1 df=f.diff(a) c=[float(i) for i in solve(df)] ddf=df.diff(a) x=np.linspace(-.5, 4, 100) fy=[f.subs(a, i) for i in x] dfy=[df.subs(a, i) for i in x] ddfy=[ddf.subs(a, i) for i in x] plt.figure(figsize=(4, 3)) plt.plot(x, fy, color="g", label="f(x)") plt.plot(x, dfy, color="b", label=r"$\frac{df(x)}{dx}$") plt.plot(x, ddfy, color="r", label=r"$\frac{d}{dx}\,\frac{df(x)}{dx}$") plt.scatter(0, -1, s=50, c="brown", label="f''(A)<0") plt.scatter(1.39, -24.8, s=50, c="k", label="f''(B)>0") plt.scatter(0.87, 0.06, s=50, c="r", label="f''(C)=0") plt.scatter(0.87, -14.75, s=50, c="orange", label="D") plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-60,60)) plt.legend(loc='best', labelcolor=['g','b','r', "brown","k","r","orange"], frameon=False) plt.show()
#그림 5.2.4 a=symbols('a', real=True) f=a**5-20*a**3 df=f.diff(a) ddf=f.diff(a, 2) ip=solve(ddf) x=np.linspace(-5, 5, 100) fy=[f.subs(a, i) for i in x] dfy=[df.subs(a, i).evalf() for i in x] ddfy=[arg(ddf.subs(a, i)).evalf() for i in x] plt.figure(figsize=(4, 3)) plt.plot(x, fy, color="g", label=r"$f(x)$") plt.plot(x, dfy, color="b", ls="--", alpha=0.6, label=r"$\frac{df(x)}{dx}$") plt.plot(x, ddfy, color="r",ls="--", alpha=0.6, label=r"$\frac{d}{dx}\frac{df(x)}{dx}$") p=((2.45, -205.76), (-2.45, 205.76),(0,0)) nme=("A", "B","C") for i in range(3): plt.scatter(p[i][0], p[i][1], s=50, c="brown") plt.text(p[i][0], p[i][1]+40, nme[i], fontweight="bold", color="brown") plt.vlines(p[i][0], -500, 500, color="brown", alpha=0.5, ls="-.") plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-500, 500)) plt.legend(bbox_to_anchor=(1,1), labelcolor=['g','b',"r"], frameon=False) plt.show()
#그림 5.2.5 a=symbols('a', real=True) f=a**3-3*a**2+2 df=f.diff(a) ddf=f.diff(a, 2) critical=solve(df) inflec=solve(ddf) x=np.linspace(-3, 4, 100) fy=[f.subs(a, i) for i in x] dfy=[df.subs(a, i) for i in x] ddfy=[ddf.subs(a, i) for i in x] plt.figure(figsize=(4, 3)) plt.plot(x, fy, color="g", label=r"$f(x)$") plt.plot(x, dfy, color="b", ls="--", alpha=0.6, label=r"$\frac{df(x)}{dx}$") plt.plot(x, ddfy, color="r",ls="--", alpha=0.6, label=r"$\frac{d}{dx}\frac{df(x)}{dx}$") p=((1,0), (0,0), (2,0)) nme=("inflection","critical", "critical") col=("k","brown","orange") for i in range(len(p)): plt.scatter(p[i][0], p[i][1], s=50, c=col[i],label=nme[i]) plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-10, 10)) plt.legend(bbox_to_anchor=(1,1), labelcolor=['g','b',"r"], frameon=False) plt.show()
댓글
댓글 쓰기