다음 그림들은 전자책 파이썬과 함께하는 미분적분의 5.3장에 수록된 그래프들과 코드들입니다.
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.3.1 x=symbols('x') f=(x**2-1)/(x**2+x-6) x1, x2, x3=np.linspace(-6, -3.01, 30), np.linspace(-2.99, 1.99, 30), np.linspace(2.01, 6, 30) y1, y2, y3=[f.subs(x, i) for i in x1], [f.subs(x, i) for i in x2], [f.subs(x, i) for i in x3] plt.figure(figsize=(4, 3)) plt.plot(x1, y1, color="g", label="f(x)") plt.plot(x2, y2, color="g") plt.plot(x3, y3, color="g") plt.vlines(-3, -10, 10, ls="--", color="brown" , label="x=-3") plt.vlines(2, -10, 10, ls="--", color="brown" , label="x=2") plt.hlines(1, -6, 6, ls="--", color="r" , label="y=1") plt.xlabel("x",loc="right", fontsize="11") plt.ylabel("y", rotation="horizontal", loc="top", fontsize="11") plt.ylim((-5, 5)) plt.legend(loc=(0.8, 0.6), labelcolor=['g','brown',"brown",'r'], frameon=False) plt.show()
#그림 5.3.2 a=symbols('a', real=True) f=(a**2-2*a)/(a**3-a) x1, x2, x3=np.linspace(-3, -1.01, 30), np.linspace(-0.99, 0.99, 30), np.linspace(1.01, 3, 30) y1, y2, y3=[f.subs(a, i) for i in x1], [f.subs(a, i) for i in x2], [f.subs(a, i) for i in x3] plt.figure(figsize=(4, 3)) plt.plot(x1, y1, color="g", label=r"$f(x)=\frac{x^{2} - 2 x}{x^{3} - x}$") plt.plot(x2, y2, color="g") plt.plot(x3, y3, color="g") plt.vlines(-1, -10, 10, ls="--", color="brown" , label="x=-1") plt.vlines(1, -10, 10, ls="--", color="brown" , label="x=1") plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-5, 5)) plt.legend(loc=(0.8, 0.6), labelcolor=['g','brown',"brown"], frameon=False) plt.show()
#그림 5.3.3 a=symbols('a', real=True) f=(3*a**2+2*a-4)/(2*a**2-a+1) x=np.linspace(-6, 6, 100) fy=[f.subs(a, i) for i in x] plt.figure(figsize=(4, 3)) plt.plot(x, fy, color="g", label=r"$f(x)=\frac{3 x^{2} + 2 x - 4}{2 x^{2} - x + 1}$") plt.hlines(3/2, -6, 6, ls="--", color="brown" , label=r"$y=\frac{3}{2}$") plt.xlabel("x",fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-5, 5)) plt.legend(loc=(0.8, 0.7), labelcolor=['g','brown',"brown"], frameon=False) plt.show()
#그림 5.3.4 a=symbols('a') f=(a**2-4)/(a-1) x1, x2=np.linspace(-3, 0.99, 50), np.linspace(1.01, 3, 50) fy1, fy2=[f.subs(a, i) for i in x1], [f.subs(a, i) for i in x2] plt.figure(figsize=(4, 3)) plt.plot(x1, fy1, color="g", label=r"$f(x)=\frac{x^2-4}{x-1}$") plt.plot(x2, fy2, color="g" ) plt.vlines(1, 10, -10, ls="--", color="b", label="x=1") x0=np.linspace(-3, 3, 100) plt.plot(x0, x0+1, ls="--", color="r", label="y=x+1") plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-10,10)) plt.legend(loc=(0.8, 0.7), labelcolor=['g','b','r'], frameon=False) plt.show()
#그림 5.3.5 a=symbols('a') f=8/(a**2-4) x1, x2, x3=np.linspace(-5, -2.01, 50), np.linspace(-1.99, 1.99, 30), np.linspace(2.01, 5, 30) y1, y2, y3=[f.subs(a, i) for i in x1], [f.subs(a, i) for i in x2], [f.subs(a, i) for i in x3] plt.figure(figsize=(4, 3)) plt.plot(x1, y1, color="g", label=r"$f(x)=\frac{8}{x^{2} - 4}$") plt.plot(x2, y2, color="g") plt.plot(x3, y3, color="g") plt.vlines(-2, -10, 10, ls="--", color="r" , label="x=-2") plt.vlines(2, -10, 10, ls="--", color="r" , label="x=2") plt.hlines(0, -5, 5, ls="--", lw=3, color="b", label="y=0") plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal", fontsize="11") plt.ylim((-5, 5)) plt.legend(loc=(0.8, 0.6), labelcolor=['g','r',"r","b"], frameon=False) plt.show()
#그림 5.3.6 x=np.linspace(-1, 5, 100) y=1-np.exp(-2*x) plt.figure(figsize=(4, 3)) plt.plot(x, y, color="g", label="f(x)=1-exp(2x)") plt.hlines(1, -1, 5, ls="--", lw=3, color="r", label="y=1") plt.xlabel("x", fontsize="11") plt.ylabel("y", rotation="horizontal",fontsize="11") plt.ylim((-1, 1.3)) plt.legend(loc=(0.6, 0.6), labelcolor=['g','r',"r","b"], frameon=False) plt.show()
댓글
댓글 쓰기