$ y=2x+ \cos x$ 와 역함수의 그래프

In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import seaborn
In [2]:
x=np.linspace(0,4,100)
y=2*x+np.cos(x)
In [3]:
fig, ax =plt.subplots()
ax.plot(x,2*x+np.cos(x),color="red")
ax.plot(y,x,color="blue")
ax.set_aspect(1)
ax.grid(True,which='both')
ax.axhline(y=0,color='k')
ax.axvline(x=0,color='k')
Out[3]:
<matplotlib.lines.Line2D at 0xd2c4ad0>
In [4]:
# fig, ax =plt.subplots()
# ax.plot(x,2*x+np.cos(x),color="red")
# ax.plot(y,x,color="blue")
# ax.set_aspect(0.5)
# ax.grid(True,which='both')
# ax.axhline(y=0,color='k')
# ax.axvline(x=0,color='k')
# ax.hist(x,y, histtype='stepfilled', facecolor='g')

$\ln 10$ 값의 리만합 근사

In [5]:
import math
In [6]:
math.log(10)
Out[6]:
2.302585092994046
In [7]:
xlist=np.linspace(1,10,11)
In [8]:
xlist
Out[8]:
array([  1. ,   1.9,   2.8,   3.7,   4.6,   5.5,   6.4,   7.3,   8.2,
         9.1,  10. ])
In [9]:
def stf(x):
    return 1/x
In [10]:
import matplotlib
def reimann(a, b, n, l, f):
    numRange = np.arange(a, b, .02)
    y = f(numRange)
    w = float(b - a)/n
    x = 0.0
    d = {"l": 0, "r": 1, "m": 0.5}
    offset = d[l[0]]
    for i in range(n):
        x += f(a + (i+offset)*w)
        plt.gca().add_patch(matplotlib.patches.Rectangle((i*w + a,0),w,f(a + (i+offset)*w)))
    plt.plot(numRange, y, color=(1.0, 0.00, 0.00), zorder=5)
    s = w * x
    #print('\n', s, '\n')

# def main():
#     functionString = input("\nEnter a function: ")
#     a = int(input("Enter the start point: "))
#     b = int(input("Enter the end point: "))
#     n = int(input("Enter the number of rectangles: "))
#     l = input("Type right, left, or middle: ")
#     reimann(a, b, n, l, lambda x: eval(functionString))
#     plt.show()
In [11]:
def app(n):
    risum=0
    ylist=[]
    xlist=np.linspace(1,10,n+1)
    dx= 9/n
    for i in range(n):
        risum=risum+stf(xlist[i])*dx
        ylist.append(stf(xlist[i]))
#     print(ylist)
    fig, ax =plt.subplots()
    ax.scatter(xlist[:n],ylist, color='g')
    ax.set_aspect(4)
    ax.grid(True,which='both')
    ax.axhline(y=0,color='k')
    ax.axvline(x=0,color='k')
    reimann(xlist[0], xlist[-1], n, 'middle',stf)
    return risum
        
    
    
In [12]:
app(10)
Out[12]:
2.7702144304424889
In [13]:
app(100)
Out[13]:
2.343752798389934
In [15]:
app(1000)
Out[15]:
2.3066417754393771