这是我的尝试,
\begin{document}
\begin{tikzpicture}
\draw[very thin, gray!15!black, dashed] (1,0) -- (1,5.5) (5,0) --(5,5.5);
\draw[thick, black!50!gray] (1,0) rectangle (2,3) (2,0) rectangle (3,4) (3,0) rectangle (4,4.6) (4,0) rectangle (5,5);
\path (1,0) node[below]{$a$} (5,0) node[below]{$b$} (3,0) node[below]{ $a+k(\frac{b-a}{n})$}(3,5) node[font=\normalsize]{$f(x)$};
\draw[thick,-latex] (-0.5,0) -- (6,0);
\draw[thick,-latex] (0,-0.5) -- (0,5.7);
\end{tikzpicture}
现在的问题是曲线。
使用 TikZ 我该如何绘制这个?
答案1
您可以通过为曲线选择一个函数来实现这一点。然后,所有内容都根据用键声明的这个函数进行定义declare function
,并可以根据用户的需求进行更改。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[
declare function = {localplot(\x) = sqrt(2*\x + 1);}
]
\draw[-Latex] (-1, 0) -- (5, 0);
\draw[
domain = -0.25:4.25,
samples = 200
] plot ({\x}, {localplot(\x)});
\foreach \x in {0,0.5,...,3.5} {
\draw (\x, 0) rectangle ({\x + 0.5}, {localplot(\x)});
}
\draw (0, -0.2) -- (0, {localplot(4.25) + 0.1});
\draw (4, -0.2) -- (4, {localplot(4.25) + 0.1});
\fill (0, 0) circle (0.05)
node[below right] {$a$};
\fill (4, 0) circle (0.05)
node[below left] {$b$};
\fill (2, 0) circle (0.05)
node[below] {$a + k\left(\frac{b - a}{n}\right)$};
\end{tikzpicture}
\end{document}
此代码产生: