我已经“编写”了以下代码来获取附图,但无法在偶数音符上方绘制水平线,即x-2j
和x-2n
以及和x-0
。x-2
对于复合梯形规则来说,这非常简单,但现在我对这种方法感到困惑。
有人能告诉我该怎么做才能得到正确的图像吗?
以下是代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
\draw[very thick, ->] (-0.3,0) -- (12,0) node[right] {$x$} coordinate (x axis);
\draw[very thick, ->] (0,-0.3) -- (0,3) node[above] {$y$};
\draw [name path=curve] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);
\path [name path=line 1] (1.0,0) -- (1.0,3);
\path [name path=line 2] (1.5,0) -- (1.5,3);
\path [name path=line 3] (2.0,0) -- (2.0,3);
\path [name path=line 4] (2.5,0) -- (2.5,3);
\path [name path=line 5] (3.0,0) -- (3.0,3);
\path [name path=line 6] (5.6,0) -- (5.6,3);
\path [name path=line 7] (6.5,0) -- (6.5,3);
\path [name path=line 8] (7.4,0) -- (7.4,3);
\path [name path=line 9] (9.3,0) -- (9.3,3);
\path [name path=line 10] (10.1,0) -- (10.1,3);
\path [name path=line 11] (11,0) -- (11,3);
\path [name intersections={of=curve and line 1, by={a}}];
\path [name intersections={of=curve and line 2, by={b}}];
\path [name intersections={of=curve and line 3, by={c}}];
\path [name intersections={of=curve and line 4, by={d}}];
\path [name intersections={of=curve and line 5, by={e}}];
\path [name intersections={of=curve and line 6, by={f}}];
\path [name intersections={of=curve and line 7, by={g}}];
\path [name intersections={of=curve and line 8, by={h}}];
\path [name intersections={of=curve and line 9, by={i}}];
\path [name intersections={of=curve and line 10, by={j}}];
\path [name intersections={of=curve and line 11, by={k}}];
\draw[dashed] (1,0) node[below] {$x_{-1}$} -- (a);
\draw[dashed] (1.5,0) node[below] {$x_0$} -- (b);
\draw[dashed] (2,0) node[below] {$x_1$} -- (c);
\draw[dashed] (2.5,0) node[below] {$x_2$} -- (d);
\draw[dashed] (3,0) node[below] {$x_3$} -- (e);
\draw[dashed] (5.6,0) node[below] {$x_{2j - 1}$} -- (f);
\draw[dashed] (6.5,0) node[below] {$x_{2j}$} -- (g);
\draw[dashed] (7.4,0) node[below] {$x_{2j+1}$} -- (h);
\draw[dashed] (9.3,0) node[below] {$x_{2n-1}$} -- (i);
\draw[dashed] (10.1,0) node[below] {$x_{2n}$} -- (j);
\draw[dashed] (11,0) node[below] {$x_{2n+1}$} -- (k);
}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}
\end{document}
答案1
假设要连接节点(f)
(2j-1)、(g)
(2j) 和(h)
(2j+1),其中g
中间节点是(如上图橙色线所示):
\draw[dashed, red] (5.6,0) node[below] {$x_{2j - 1}$} -- (f);
\draw[dashed] (6.5,0) node[below] {$x_{2j}$} -- (g);
\draw[dashed, blue] (7.4,0) node[below] {$x_{2j+1}$} -- (h);
我们需要一条从 (g) 延伸到其他两个节点的水平线。因此,首先“画(g)
“从到 y 轴的水平线交点:
\path[name path=Horizontal Line at g] (g) -| (0,0);
计算该线与线的交点line 6
:
\path [name intersections={of=Horizontal Line at g and line 6, by={f'}}];
并将该交点标记为。然后我们只需要一条从 到 的(f')
水平线并将其延伸到:(h)
(g)
(f')
\draw[ultra thick, orange] (h) |- (g) -| (f');
笔记:
- 代码可能需要一些清理,但我知道这是你要找的东西后,我会这样做。从你的描述中看不出你想要显示什么。我给这两条线添加了颜色,以显示我试图连接的是哪一条。
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
\draw[very thick, ->] (-0.3,0) -- (12,0) node[right] {$x$} coordinate (x axis);
\draw[very thick, ->] (0,-0.3) -- (0,3) node[above] {$y$};
\draw [name path=curve] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);
\path [name path=line 1] (1.0,0) -- (1.0,3);
\path [name path=line 2] (1.5,0) -- (1.5,3);
\path [name path=line 3] (2.0,0) -- (2.0,3);
\path [name path=line 4] (2.5,0) -- (2.5,3);
\path [name path=line 5] (3.0,0) -- (3.0,3);
\path [name path=line 6] (5.6,0) -- (5.6,3);
\path [name path=line 7] (6.5,0) -- (6.5,3);
\path [name path=line 8] (7.4,0) -- (7.4,3);
\path [name path=line 9] (9.3,0) -- (9.3,3);
\path [name path=line 10] (10.1,0) -- (10.1,3);
\path [name path=line 11] (11,0) -- (11,3);
\path [name intersections={of=curve and line 1, by={a}}];
\path [name intersections={of=curve and line 2, by={b}}];
\path [name intersections={of=curve and line 3, by={c}}];
\path [name intersections={of=curve and line 4, by={d}}];
\path [name intersections={of=curve and line 5, by={e}}];
\path [name intersections={of=curve and line 6, by={f}}];
\path [name intersections={of=curve and line 7, by={g}}];
\path [name intersections={of=curve and line 8, by={h}}];
\path [name intersections={of=curve and line 9, by={i}}];
\path [name intersections={of=curve and line 10, by={j}}];
\path [name intersections={of=curve and line 11, by={k}}];
\draw[dashed] (1,0) node[below] {$x_{-1}$} -- (a);
\draw[dashed] (1.5,0) node[below] {$x_0$} -- (b);
\draw[dashed] (2,0) node[below] {$x_1$} -- (c);
\draw[dashed] (2.5,0) node[below] {$x_2$} -- (d);
\draw[dashed] (3,0) node[below] {$x_3$} -- (e);
\draw[dashed, red] (5.6,0) node[below] {$x_{2j - 1}$} -- (f);
\draw[dashed] (6.5,0) node[below] {$x_{2j}$} -- (g);
\draw[dashed, blue] (7.4,0) node[below] {$x_{2j+1}$} -- (h);
\draw[dashed] (9.3,0) node[below] {$x_{2n-1}$} -- (i);
\draw[dashed] (10.1,0) node[below] {$x_{2n}$} -- (j);
\draw[dashed] (11,0) node[below] {$x_{2n+1}$} -- (k);
\path[name path=Horizontal Line at g] (g) -| (0,0);
\path [name intersections={of=Horizontal Line at g and line 6, by={f'}}];
\draw[ultra thick, orange] (h) |- (g) -| (f');
}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}
\end{document}
代码简化1:
您可以使用\foreach
循环来简化代码。\foreach
这里的第二个来自 Gonzalo Medina 的评论——不知道我要花多长时间才能弄清楚这一点。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
\draw[very thick, ->] (-0.3,0) -- (12,0) node[right] {$x$} coordinate (x axis);
\draw[very thick, ->] (0,-0.3) -- (0,3) node[above] {$y$};
\draw [name path=curve] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);
\path [name path=line 1] (1.0,0) -- (1.0,3);
\path [name path=line 2] (1.5,0) -- (1.5,3);
\path [name path=line 3] (2.0,0) -- (2.0,3);
\path [name path=line 4] (2.5,0) -- (2.5,3);
\path [name path=line 5] (3.0,0) -- (3.0,3);
\path [name path=line 6] (5.6,0) -- (5.6,3);
\path [name path=line 7] (6.5,0) -- (6.5,3);
\path [name path=line 8] (7.4,0) -- (7.4,3);
\path [name path=line 9] (9.3,0) -- (9.3,3);
\path [name path=line 10] (10.1,0) -- (10.1,3);
\path [name path=line 11] (11,0) -- (11,3);
% Can replace all these with a \foreach
% \path [name intersections={of=curve and line 1, by={a}}];
% ...
% \path [name intersections={of=curve and line 11, by={k}}];
%\foreach \Line in {1,...,11} {% For this to work needs some expandafter magic
% \path [name intersections={of=curve and line \Line, by={\alph{\Line}}}];
%}
\foreach \Line/\Name in {1/a, 2/b, 3/c, 4/d, 5/e, 6/f, 7/g, 8/h, 9/i, 10/j, 11/k} {%
\path [name intersections={of=curve and line \Line, by={\Name}}];
}
% Can replace all these with a \foreach
% \draw[dashed] (1,0) node[below] {$x_{-1}$} -- (a);
% ...
% \draw[dashed] (11,0) node[below] {$x_{2n+1}$} -- (k);
\foreach \point/\label in
{a/-1, b/0, c/1, d/2, e/3, f/2j-1, g/2j, h/2j+1, i/2n-1, j/2n, k/2n+1}
\draw[dashed] (\point|-0,0) node[below] {$x_{\label}$} -- (\point);
% Draw the desired lines
\path[name path=Horizontal Line at g] (g) -| (0,0);
\path[name intersections={of=Horizontal Line at g and line 6, by={f'}}];
\draw[ultra thick, orange] (h) |- (g) -| (f');
}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}
\end{document}
代码简化2:
使用另一种方法,\foreach
您可以进一步减少代码,并进行一些小的调整(我个人对箭头和线条样式的偏好):
笔记:
- 这是作为单独的 MWE 完成的,以便更容易地跟踪从给定代码到此的演变。
代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
\draw[very thick, -stealth] (-0.3,0) -- (12,0) node[right] {$x$};% x-axis
\draw[very thick, -stealth] (0,-0.3) -- (0,3) node[above] {$y$};% y-axis
\draw [name path=curve, thick, blue] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);% curve
\foreach \x/\Point [count=\xi] in {1.0/a, 1.5/b, 2.0/c, 2.5/d, 3.0/e, 5.6/f, 6.5/g, 7.4/h, 9.3/i, 10.1/j, 11/k } {%
\path [name path global=line \xi, red] (\x,0) -- (\x,3);
\path [name intersections={of=curve and line \xi, by={\Point}}];
}%
\foreach \Line/\Name in {1/a, 2/b, 3/c, 4/d, 5/e, 6/f, 7/g, 8/h, 9/i, 10/j, 11/k} {%
\path [name intersections={of=curve and line \Line, by={\Name}}];
}
\foreach \point/\label in
{a/-1, b/0, c/1, d/2, e/3, f/2j-1, g/2j, h/2j+1, i/2n-1, j/2n, k/2n+1}
\draw[densely dotted, thick] (\point|-0,0) node[below] {$x_{\label}$} -- (\point);
% Draw the desired lines
\path[name path=Horizontal Line at g] (g) -| (0,0);
\path[name intersections={of=Horizontal Line at g and line 6, by={f'}}];
\draw[ultra thick, orange] (h) |- (g) -| (f');
}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}
\end{document}
答案2
另一种可能性是,使用垂直坐标系而不计算更多交点:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
\draw[very thick, ->] (-0.3,0) -- (12,0) node[right] {$x$} coordinate (x axis);
\draw[very thick, ->] (0,-0.3) -- (0,3) node[above] {$y$};
\draw [name path=curve] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);
\path [name path=line 1] (1,0,0) -- (1.0,3);
\path [name path=line 2] (1.5,0) -- (1.5,3);
\path [name path=line 3] (2.0,0) -- (2.0,3);
\path [name path=line 4] (2.5,0) -- (2.5,3);
\path [name path=line 5] (3.0,0) -- (3.0,3);
\path [name path=line 6] (5.6,0) -- (5.6,3);
\path [name path=line 7] (6.5,0) -- (6.5,3);
\path [name path=line 8] (7.4,0) -- (7.4,3);
\path [name path=line 9] (9.3,0) -- (9.3,3);
\path [name path=line 10] (10.1,0) -- (10.1,3);
\path [name path=line 11] (11,0) -- (11,3);
% naming the intersections
\foreach \name [count=\i from 1] in {a,b,...,k}
\path [name intersections={of=curve and line \i, by={\name}}];
% vertical lines
\foreach \point/\label in {a/-1,b/0,c/1,d/2,e/3,f/2j-1,g/2j,h/2j+1,i/2n-1,j/2n,k/2n+1}
\draw[dashed] (\point|-0,0) node[below] {$x_{\label}$} -- (\point);
% horizontal lines
\draw[ultra thick,red!60!black] (a) |- (a|-b) -- (b) -- (c|-b);
\draw[ultra thick,red!60!black] (c|-d) -- (d) -- (e|-d) -| (e);
\draw[ultra thick,red!60!black] (f|-g) -- (g) -- (h|-g) -| (h);
\draw[ultra thick,red!60!black] (i|-j) -- (j) -- (k|-j);
}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}
\end{document}