#!/usr/bin/env python # coding: utf-8 # ### $ y=2x+ \cos x$ 와 역함수의 그래프 # In[1]: get_ipython().run_line_magic('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') # 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) # In[7]: xlist=np.linspace(1,10,11) # In[8]: xlist # 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) # In[13]: app(100) # In[15]: app(1000) # In[ ]: