%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import seaborn
x=np.linspace(0,4,100)
y=2*x+np.cos(x)
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')
<matplotlib.lines.Line2D at 0xd2c4ad0>
# 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')
import math
math.log(10)
2.302585092994046
xlist=np.linspace(1,10,11)
xlist
array([ 1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1, 10. ])
def stf(x):
return 1/x
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()
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
app(10)
2.7702144304424889
app(100)
2.343752798389934
app(1000)
2.3066417754393771