다음 그림들은 전자책 파이썬과 함께하는 미분적분의 6.3장과 6.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")
def axisTran(ax): ax.spines['left'].set_position(("data", 0)) ax.spines['bottom'].set_position(("data", 0)) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False)
#그림 6.3.1 a=symbols("a") f=(a-1)*(a-2)*(a-3)+3 x=np.linspace(0, 5, 100) y=[f.subs(a, i) for i in x] plt.figure(figsize=(4,3)) plt.plot(x, y ,color="g", label="f(x)") x0, x1=np.linspace(1, 2.5, 20), np.linspace(2.5, 4, 20) fy0, fy1=[float(f.subs(a, i)) for i in x0], [float(f.subs(a, i)) for i in x1] plt.fill_between(x0, fy0, color="b", alpha=0.5, label="F(x)") plt.fill_between(x1, fy1, color="r", alpha=0.5, label="F(x+h)-F(X)") plt.xticks([1, 2.5, 4], ["a=0", "x", "b=x+h"]) plt.yticks([]) plt.ylabel("f(x)", rotation="horizontal", labelpad=10, fontsize=11) plt.ylim((-3, 10)) plt.legend(loc='best', labelcolor="linecolor", frameon=False) plt.show()
#그림 6.3.2 a=symbols("a") f=a**2-1 x=np.linspace(-1, 2.5, 100) y=[float(f.subs(a, i)) for i in x] plt.figure(figsize=(4,3)) plt.plot(x, y ,color="g", label=r"$f(x)=x^2-1$") x1=np.linspace(0, 2, 100) y1=[float(f.subs(a, i)) for i in x1] plt.fill_between(x1, y1,color="b", alpha=0.3) plt.xlabel("x", fontsize=11) plt.ylabel("f(x)", rotation="horizontal", labelpad=10, fontsize=11) plt.ylim((-2, 3)) plt.legend(loc="best", labelcolor=["g", 'b','r'], frameon=False) plt.show()
#그림 6.3.3 t=symbols('t') a=12*t**2-6 v=a.integrate(t)+5 s=v.integrate(t)+5 x=np.linspace(-4, 4, 100) ay=[a.subs(t, i) for i in x] vy=[v.subs(t, i) for i in x] sy=[s.subs(t, i) for i in x] plt.figure(figsize=(4,3)) plt.plot(x, ay, color="r", label="a(t)") plt.plot(x, vy, color="b", label="v(t)") plt.plot(x, sy, color="g", label="s(t)") plt.ylim(-10, 40) plt.xlabel("x", fontsize=11) plt.ylabel("y", rotation="horizontal", fontsize=11) plt.legend(loc="best", labelcolor='linecolor', frameon=False) plt.show()
#그림 6.3.5 x=np.arange(0, 360) r=np.cos(np.radians(x))+1 plt.figure(figsize=(4,3)) plt.plot(x, r ,color="g", label=r"$r(\theta)=\cos(\theta)+1$") x0=np.linspace(0, 90, 50) r0=np.cos(np.radians(x0))+1 plt.fill_between(x0, r0, color="r", alpha=0.3) plt.xticks([0, 90, 180, 270, 360], [0, r"$\frac{\pi}{2}$", r"$\pi$", r"$\frac{3\pi}{2}$", r"$2\pi$" ]) plt.xlabel(r"$\theta$", fontsize=11) plt.ylabel(r"$r(\theta)$", rotation="horizontal", labelpad=10, fontsize=11) plt.legend(loc="best", labelcolor="g", frameon=False) plt.show()
#그림 6.3.6 x=np.linspace(-1, 2.5, 100) f=2*x+1 g=x**2+1 plt.figure(figsize=(4,3)) plt.plot(x, f ,color="b", label=r"$f(x)=2x+1$") plt.plot(x, g ,color="g", label=r"$g(x)=x^2+1$") x1=np.linspace(0, 2, 100) f1=2*x1+1 g1=x1**2+1 plt.fill_between(x1, f1, g1, color="r", alpha=0.3) plt.xlabel("x", fontsize=11) plt.ylabel("y", rotation="horizontal", labelpad=10, fontsize=11) plt.legend(loc="best", labelcolor="linecolor", frameon=False) plt.show()
#그림 6.3.7 x=np.linspace(-3, 10, 100) f=x**4-3*x**3-4*x**2+10 g=40-x**2 plt.figure(figsize=(4,3)) plt.plot(x, f ,color="b", label=r"$f(x)=2x+1$") plt.plot(x, g ,color="g", label=r"$g(x)=x^2+1$") x1=np.linspace(1,3, 100) f1=x1**4-3*x1**3-4*x1**2+10 g1=40-x1**2 plt.fill_between(x1, g1, f1, color="r", alpha=0.3) plt.xlabel("x", fontsize=11) plt.ylabel("y", rotation="horizontal", labelpad=10, fontsize=11) plt.ylim((-40, 60)) plt.legend(loc="best", labelcolor="linecolor") plt.show()
#그림 6.3.8 x=np.linspace(-1, 3, 100) f=x**2 plt.figure(figsize=(4,3)) plt.plot(x, f ,color="g", label=r"$f(x)=x^2$") x1=np.linspace(0,2, 100) f1=x1**2 plt.fill_between(x1, f1, color="r", alpha=0.3) plt.hlines(4/3, (4/3)**0.5, 2, ls="--", color="b", label=r"$y_{avg}=\frac{4}{3}$") plt.xlabel("x", fontsize=11) plt.ylabel("y", rotation="horizontal", fontsize=11) plt.ylim(-0, 5) plt.legend(loc="best", labelcolor="linecolor", frameon=False) plt.show()
#그림 6.4.1 x0=np.linspace(-90, 360) y0=np.sin(np.radians(x0)) fig, ax=plt.subplots(figsize=(4,3)) ax.plot(x0, y0 ,color="g", label="f(x)=sin(x)") x=np.linspace(-90, 0, 30), np.linspace(0, 180, 30), np.linspace(180, 360, 30) y=np.sin(np.radians(x)) col=["brown", "b","r"] nme=["Aear1: -1.42", "Aear2: 1.99", "Aear3: 1.99"] for i in range(len(x)): ax.fill_between(x[i], y[i], color=col[i], alpha=0.3, label=nme[i]) axisTran(ax) ax.set_xlabel("x", loc="right",fontsize=11) ax.set_ylabel("y", rotation="horizontal", loc="top", fontsize=11) ax.set_xticks([-90,0, 90, 180, 270, 360], [r"$-\frac{\pi}{2}$", 0, r"$\frac{\pi}{2}$", r"$\pi$",r"$\frac{3\pi}{2}$", r"$2\pi$"]) ax.set_yticks([-1, -0.5, 0.5, 1]) ax.set_ylim((-1.1, 1.1)) ax.legend(loc="best", labelcolor=["g","brown", "b","r"]) plt.show()
댓글
댓글 쓰기