If your work involves mathematics and/or programming you should investigate the sagetex package, located here on CTAN. This gives you access to a computer algebra system, called SAGE, as well as the Python programming language. SAGE is not part of a LaTeX installation. You can get up and running in 5 minutes by creating a free online Cocalc account. Another way is to install SAGE to your computer. This can be more problematic depending on your background with computers. With that caveat, here is a sagetex solution:
\documentclass{article}
\usepackage{sagetex}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{sagesilent}
def RiemannRec(a,b,n,f):
t = var('t')
delta = (b-a)/n
LowerY = find_local_minimum(f,a,b)[0]-.5
UpperY = find_local_maximum(f,a,b)[0]+.5
step = .01
x_coords = [t for t in srange(a,b,step)]
y_coords = [f(t).n(digits=4) for t in srange(a,b,step)]
####################### Picture
output = r"\begin{tikzpicture}[scale=0.75]"
output += r"\begin{axis}["
output += r"xtick=\empty, ytick=\empty,"
output += r"grid = none,"
output += r"thick,black,"
output += r"scale=1,"
output += r"axis lines=center,"
output += r"line join=bevel,"
output += r"xmin=%f,xmax=%f,ymin= %f,ymax=%f]"%(a-.5,b+.5,LowerY, UpperY)
#### Left hand rectangles
for i in range(0,n):
output += r"\draw[color=Red,pattern=north west lines, pattern color=Red!90,opacity=.4] (%s,0) rectangle (%s,%s);"%(a+i*delta,a+(i+1)*delta,f(a+i*delta))
#### Right hand rectangles
for i in range(0,n):
output += r"\draw[color=NavyBlue,pattern=north east lines, pattern color=NavyBlue!90!white,opacity=.4] (%s,0) rectangle (%s,%s);"%(a+i*delta,a+(i+1)*delta,f(a+(i+1)*delta))
####### the function
output += r"\addplot[smooth] coordinates {"
for i in range(0,len(x_coords)-1):
output += r"(%s,%s)"%(x_coords[i],y_coords[i])
output += r"};"
#### a and b
output += r"\draw[dashed] (%s,0)--(%s,1) node[above] {$a$};"%(a,a)
output += r"\draw[dashed] (%s,0)--(%s,-1) node[below] {$b$};"%(b,b)
output += r""
output += r"\end{axis}"
output += r"\end{tikzpicture}"
return output
f(x) = (sin(x)).function(x)
fig1 = RiemannRec(-1.0,4.0,10,f) #a,b,n,function
\end{sagesilent}
This is the first diagram:
\begin{center}
\sagestr{fig1}
\end{center}
\begin{sagesilent}
f(x) = (sin(x)+cos(x^2)).function(x)
fig2 = RiemannRec(-1.0,4.0,6,f)
\end{sagesilent}
This is the second diagram:
\begin{center}
\sagestr{fig2}
\end{center}
\end{document}
Create a LaTeX document, copy/paste the code into it and you'll get output like this:

There are observations to make about the code. The code assumes you have a continuous function on a closed, bounded interval from a to b. This guarantees that your function achieves its maximum and minimum value over the interval. Now SAGE can find these values and set the minimum and maximum values for the plotting screen so the entire graph shows with: LowerY = find_local_minimum(f,a,b)[0]-.5 UpperY = find_local_maximum(f,a,b)[0]+.5
That means you don't need to spend time determining the plotting parameters for the screen. The plotting parameters of the screen are set here: output += r"xmin=%f,xmax=%f,ymin= %f,ymax=%f]"%(a-.5,b+.5,LowerY, UpperY). A function, RiemannRec(a,b,n,f), is created to do the work using Python code. You need to tell it a and b, the left and right endpoints, as well as n, the number of rectangles and f, the function. I wasn't happy with your output because the output from left Riemann integration and right Riemann integration sometimes overwrote the other so I've changed the opacity so that you can see both output simultaneously. Notice also that the you still will have some tweaking to do so that your indications of where a and b are don't interfere with the graph. Finally, notice the somewhat clunky call of the function f(x) = (sin(x)).function(x) fig1 = RiemannRec(-1.0,4.0,10,f). Coding this way generates no errors. However, you could get your output with just one line. Suppose I try fig1 = RiemannRec(-1.0,4.0,10,cos(2*x)) and run it. You'll see in the picture below I get a deprecation warning that has been going on since 2009(!).

So you can code things more naturally but it will give a warning that hasn't meant anything for over a decade. Eventually it might be fixed, so you have a solution ready if/when that day arrives.