我正在尝试将我在 Inkscape 中制作的这幅插图重现为 tikzpictures,以便在 LaTex 中使用。我查看了很多这,但我被困住了,而且已经很晚了。你们这些好心的专家能帮我弄清楚如何制作这个插图吗?
我也看了这和这,如果我找到可行的解决方案,我会更新。与此同时,请在这里提出任何建议或想法。非常感谢!
这是对可编译 MWE 的第一次尝试,
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
integral segments/.code={\pgfmathsetmacro\integralsegments{#1}},
integral segments=3,
integral/.style args={#1:#2}{
ybar interval,
domain=#1+((#2-#1)/\integralsegments)/2:#2+((#2-#1)/\integralsegments)/2,
samples=\integralsegments-1,
x filter/.code=\pgfmathparse{\pgfmathresult-((#2-#1)/\integralsegments)/2}
}
}
\begin{tikzpicture}[/pgf/declare function={f=-15*(x-5)+(x-5)^3+50;}]
\begin{axis}[
ticks=none,
domain=0:10,
samples=100,
axis lines=middle
]
\addplot [thick] {f} node[pos=1] (f) {};
\addplot [
red,
integral=2:6
] {f};
\end{axis}
\end{tikzpicture}
\end{document}
@salim-bou和@zarko,我很惊讶,也非常感激您花时间,而且只花了几分钟。问我如何在图表上叠加一些文本(显然不是 LaTeX 代码,但我只有一些类似绘画的软件来显示我所在的位置)是不是太过分了。
答案1
好吧,@salim bou 咬了我 6 分钟,但无论如何(略有不同):
我使用以下纯 TikZ 代码重现了您的图像:
\documentclass[tikz,
border=3mm]{standalone}
\usetikzlibrary{intersections,quotes}
\begin{document}
\begin{tikzpicture}
% axes
\draw[->] (-0.2,0) -- (8,0) node [below left] {$x$};
\draw[->] (0,-0.2) -- (0,6) node [below left] {$y(x)$};
% grid
\draw[gray!30,thin] (-1,-1) grid +(9,7);
% curve
\draw[red,very thick, name path=A]
(1,2) .. controls +(1,0) and + (-1.5,-0.1) .. (4,3)
.. controls +(1.5,0.1) and + (-1,0) .. (7,4);
% start, end of curve
\draw[dashed] (1,-0.1) node[below] {$a$} -- (1,2);
\draw[dashed] (7,-0.1) node[below] {$b$} -- (7,4);
% for determining intersections a and b
\path[name path=B] (3,-0.1) node[below] {$x_0$} -- + (0,6);
\path[name path=C] (5,-0.1) node[below] {$x_0 + \Delta x$} -- + (0,6);
% rectangles
\draw [name intersections={of=A and B, by=a},very thick,dashed, blue]
(5,0) -| (a) -| (5,0);
\draw [name intersections={of=A and C, by=b},very thick,dotted,green]
(3,0) -| (b) -| (3,0);
% measure of \Delta x
\draw[<->] (3,0.5) to ["$\Delta x$"] (5,0.5);
% comments:
\draw[<-] (3.4,3.0) -- + (120:1) node[above] {$f(x_0+\Delta)\cdot\Delta x$};
\draw[<-] (4.4,2.9) -- + (0:0.8) node[right] {$f(x_0+\Delta)\cdot\Delta x$};
\draw[<-] (4.4,1.5) -- + (0:0.8) node[right] {$f(x_0)\cdot\Delta x$};
\end{tikzpicture}
\end{document}
这使:
编辑:考虑到最近对问题的编辑——向 MWE 添加了:
% comments:
\draw[<-] (3.4,3.0) -- + (120:1) node[above] {$f(x_0+\Delta)\cdot\Delta x$};
\draw[<-] (4.4,2.9) -- + (0:0.8) node[right] {$f(x_0+\Delta)\cdot\Delta x$};
\draw[<-] (4.4,1.5) -- + (0:0.8) node[right] {$f(x_0)\cdot\Delta x$};
答案2
代码
\documentclass[margin=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\def\a{0.5}
\def\b{7}
\def\xA{2}
\def\xB{4}
\begin{document}
\begin{tikzpicture}[>=stealth,thick]
\draw [->](-0.5,0)--(8,0);
\draw [->](0,-0.5)--(0,6);
\draw [name path=graph](\a,1.5)coordinate(a)..controls (\xA,4) and (\b,2).. (\b,5)coordinate(b);
\path [name path=lineA](\xA,0)--+(0,6);
\path [name path=lineB](\xB,0)--+(0,6);
\draw [name intersections={of=lineA and graph},dashed,green]
(\xB,0)|-(intersection-1)--(intersection-1 |- 0,0)node [below,black]{\strut $x_0$};
\draw [name intersections={of=lineB and graph},dashed,blue]
(\xA,0)|-(intersection-1)--(intersection-1 |- 0,0)node [below,black]{\strut $x_0+\Delta x$};
\draw [<->] ([yshift=3mm]\xA,0)--node[fill=white]{$\Delta x$}([yshift=3mm]\xB,0);
\draw [dashed,thin] (a)--(a|- 0,0)node [below]{\strut $a$};
\draw [dashed,thin] (b)--(b|- 0,0)node [below]{\strut $b$};
\end{tikzpicture}
\end{document}
结果