So I am trying to make a function, plot, that will take f as a function ( I used cos in my shell), a and b as ranges with b greater than a.(B = 1 and A = 0 default), with n intervals, and m as the method of Riemann sum(center, left, or right.)
But the problem is that when I run the code and try to tye in right as a method, it does the following.
import numpy as np
import matplotlib.pyplot as plt
def plot(f, a = 0, b = 1, m = str('center'), n = 10):
v = 0
par = (b - a)/1000
x = np.arange(a, b, par)
plt.plot(x, f(x), 'black')
w = (b - a)/n
##This is juts plotting the graph, does not need to be grouped
if b < a:
raise ValueError('Value b must be greater than a', b)
##Check if b is greater than a
if m != str('center') or m != str('left') or m != str('right'):
raise ValueError('Value m must be one of the following: center, left, or right', b)
##Check if m is valid
if n < 0:
raise ValueError('N must be a positive integer', n)
##Check if n is positive and an integer
if m == 'center':
d = w /2
x2 = np.arange(a + d , b + d , w)
plt.bar(x2,f(x2), align = 'center', color = 'blue',\
edgecolor = 'black', width = w, alpha = 0.5)
print("The estimation using", m ,"is", v)
plt.show()
##Does Mid Point
if m == 'left':
x2 = np.arange(a , b , w)
plt.bar(x2,f(x2), align = 'edge', color = 'red',\
edgecolor = 'black', width = w, alpha = 0.5)
print("The estimation using", m ,"is", v)
plt.show()
##Does Left Point
if m == 'right':
x2 = np.arange(a + w, b + w , w)
plt.bar(x2,f(x2), align = 'edge', color = 'orange',\
edgecolor = 'black', width = -w, alpha = 0.5)
print("The estimation using", m ,"is", v)
plt.show()
##Does Right Point
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
p2.plot(f)
File "C:\Users\Vincent\AppData\Local\Programs\Python\Python37-32\Project2.py", line 21, in plot
raise ValueError('Value m must be one of the following: center, left, or right', b)
ValueError: ('Value m must be one of the following: center, left, or right', 1)
This is the error message that I get. I didn't type in 1 for m, I typed in 1 for b but it is reading it somehow.
m=str('center')to create a string... Just using quotes will create a string:m='center'