TeX - LaTeX Asked on January 31, 2021
Here is my tikz
picture code and my goal is to have all of the necessary reqirements for a Riemann sum picture to be commands that will automatically make the picture for me. So far I have this but I’m having difficulty making the subintervals automatic since I keep on getting errors when I tried making the pattern as ra+0.5
or as ra+rstep
(where rstep
is how much of an width each rectangle has). I also would rather not need to define my function each time I use it.
In the end, I want to define the endpoints a
and b, the subintervals n
, and the function all at once at the start. This way, I could make multiple pictures of different #
of subintervals, different a
‘s and b
‘s, and different functions.
documentclass[11pt]{article}
usepackage{tikz}
newcommandra{-1} % ra = Riemann, a
newcommandratwo{-0.5} % ra = Riemann, a
newcommandrb{4} % rb = Riemann, b
newcommandrbtwo{3.5} % rb = Riemann, b
begin{document}
textcolor{blue}{Left-hand Riemann Sum} and textcolor{red}{Right-hand Riemann Sum}
begin{tikzpicture}
draw[<->] (ra-0.5,0) -- (rb+0.5,0);
draw[<->] (0,ra-0.5) -- (0,rb+0.5);
draw[dashed] (ra,0) -- (ra,1) node[above] {$a$};
draw[dashed] (rb,0) -- (rb,-1) node[below] {$b$};
%Right-Hand
foreach x in {ratwo,0,...,rb} % <--- my issues
draw[thick, fill=red!25] (x-.5,0) -- (x-.5,{sin(deg(x))}) -- (x,{sin(deg(x))}) -- (x,0) -- cycle;
%Left-Hand
foreach x in {ra,-0.5,...,rbtwo} % <--- my issues
draw[thick, fill=blue!25] (x,0) -- (x,{sin(deg(x))}) -- (x+.5,{sin(deg(x))}) -- (x+.5,0) -- cycle;
draw[ultra thick, <->, domain=ra:rb, smooth, samples=100, variable=x] plot ({x},{sin(deg(x))});
end{tikzpicture}
end{document}
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.
Answered by DJP on January 31, 2021
A rough version. Draw in pic
and manage style by pgfkeys
documentclass[tikz, border=1cm]{standalone}
pgfdeclarelayer{left}
pgfdeclarelayer{right}
pgfdeclarelayer{plot}
pgfsetlayers{main, right, left, plot}
tikzset{
pics/riemann sum/.style args={#1:#2:#3}{
code={
pgfmathsetmacro{leftpoint}{#1}
pgfmathsetmacro{rightpoint}{#2}
pgfmathsetmacro{movecount}{#3}
pgfmathsetmacro{step}{(rightpoint-leftpoint)/movecount}
begin{scope}[local bounding box=riemann]
foreach i [count=c from 0] in {1,...,movecount} {
pgfonlayer{left}
path[riemann sum/left sum] (leftpoint+c*step, {temp(leftpoint+c*step)}) rectangle (leftpoint+i*step, 0);
endpgfonlayer
pgfonlayer{right}
path[riemann sum/right sum] (rightpoint-c*step, {temp(rightpoint-c*step)}) rectangle (rightpoint-i*step, 0);
endpgfonlayer
}
pgfonlayer{plot}
draw [domain=#1:#2, riemann sum/riemann line] plot (x, {temp(x)});
endpgfonlayer
end{scope}
draw[->, riemann sum/riemann axis] ([xshift=-5mm]riemann.west) -- ([xshift=5mm]riemann.east);
draw[->, riemann sum/riemann axis] ([yshift=-5mm]riemann.south) -- ([yshift=5mm]riemann.north);
}
},
riemann sum/.search also=/tikz,
riemann sum/.cd,
function/.style 2 args={declare function={temp(#1)=#2;}},
left sum/.style={draw},
right sum/.style={draw},
riemann line/.style={},
riemann axis/.style={},
left/.style={left sum/.append style={#1}},
right/.style={right sum/.append style={#1}},
line/.style={riemann line/.append style={#1}},
axis/.style={riemann axis/.append style={#1}}
}
newcommand{riemannsum}[2][-1:1:2]{pic[riemann sum/.cd,#2] {riemann sum=#1};}
begin{document}
begin{tikzpicture}
riemannsum[-2:2:8]{
function={x}{sin(x r)},
left={thick, fill=blue!50},
right={thick, fill=yellow!50},
line={thick},
axis={thick}
}
riemannsum[-2:2:12]{
function={x}{sin(x r) + cos(x^2 r)},
left={thick, fill=blue!50},
right={thick, fill=yellow!50},
line={thick, red, samples=300, domain=-3:3},
axis={thick},
yshift=5cm,
}
end{tikzpicture}
end{document}
Answered by ZhiyuanLck on January 31, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP