![如何在 tikz 上绘制自定义函数?](https://linux22.com/image/460099/%E5%A6%82%E4%BD%95%E5%9C%A8%20tikz%20%E4%B8%8A%E7%BB%98%E5%88%B6%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0%EF%BC%9F.png)
我使用 powerpoint 笔/记号笔工具任意绘制了一个函数,如下例所示。但不知道该如何将其转换为 tikz?我是否必须使用一些已知的函数近似值?任何帮助都非常感谢。
预期:(我用钢笔/记号笔工具在 MS PowerPoint 上画了这幅图)
我的结果:
代码:
\documentclass[border=3pt,tikz]{standalone}
\usepackage{tikz}
\usepackage{etoolbox} % for \ifnumcomp
\usepackage{listofitems} % for \readlist to create arrays
\tikzset{>=latex} % for LaTeX arrow head
\def\nstyle{int(\lay<\Nnodlen?min(2,\lay):3)} % map layer number onto 1, 2, or 3
\usetikzlibrary{positioning, calc, arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{scope}[scale=0.75,xshift=18cm,yshift=-2cm] % <---- HERE
\draw[blue!50!black] plot[domain={-1.5:5}] (\x,{sin(\x^2 r)});
\draw[blue!50!black, densely dotted] (-2,0) -- (5,0);
\draw[black, densely dashed] (0,0)
-- ++(0,-2) node[below] {$x_1$} coordinate[pos=0] (A);
\draw[black, densely dashed] (0.5,0)
-- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
\draw[black, densely dashed] (1.0,0)
-- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
\draw[black, densely dashed] (1.5,0)
-- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
\draw[black, densely dashed] (2.0,0)
-- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
\draw[thick, <->] (-2,4)
-- node[left, midway, anchor=south, rotate=90, font=\bfseries] (Y) {$f(x)$} (-2,-2)
-- node[below, pos=1, font=\bfseries] {Quad. points} (5,-2);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
\documentclass[tikz, border=1cm]{standalone}
\tikzset{>=latex}
\begin{document}
\begin{tikzpicture}[
declare function={f(\x)=sin((\x-2)^2 r)+2;},
]
\draw[blue!50!black] plot[domain={0.5:7}, samples=50, smooth] (\x,{f(\x)});
\draw[blue!50!black, densely dotted] (0,2) -- (7,2);
\draw[densely dashed] (2.0,0) node[below]{$x_1$} -- +(0,{f(2.0)}) node[above=4pt]{$y_1$};
\draw[densely dashed] (2.5,0) node[below]{$x_2$} -- +(0,{f(2.5)}) node[above=4pt]{$y_2$};
\draw[densely dashed] (3.0,0) node[below]{$x_3$} -- +(0,{f(3.0)}) node[above=4pt]{$y_3$};
\draw[densely dashed] (3.5,0) node[below]{$x_4$} -- +(0,{f(3.5)}) node[above=4pt]{$y_4$};
\draw[densely dashed] (5.0,0) node[below]{$x_n$} -- +(0,{f(5.0)}) node[above right]{$y_n$};
\draw[thick, <->] (0,6) --node[above, rotate=90, font=\bfseries] (Y) {$f(x)$} (0,0) -- (7,0) node[below, font=\bfseries] {Quad. points};
\end{tikzpicture}
\end{document}
编辑:用一些随机点代替函数
\documentclass[tikz, border=1cm]{standalone}
\tikzset{>=latex}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\draw[blue!50!black] plot[smooth] coordinates {(1.0,1.2) (1.5,2.3) (2.0,2.0) (2.5,3.0) (3.0,2.6) (3.5,1.8) (4.0,2.4) (5.0,1.5) (7.0,3.0)};
\draw[blue!50!black, densely dotted] (0,2) -- (7,2);
\draw[densely dashed] (2.0,0) node[below]{$x_1$} -- (2.0,2.0) node[above=4pt]{$y_1$};
\draw[densely dashed] (2.5,0) node[below]{$x_2$} -- (2.5,3.0) node[above=4pt]{$y_2$};
\draw[densely dashed] (3.0,0) node[below]{$x_3$} -- (3.0,2.6) node[above=4pt]{$y_3$};
\draw[densely dashed] (3.5,0) node[below]{$x_4$} -- (3.5,1.8) node[above=4pt]{$y_4$};
\draw[densely dashed] (5.0,0) node[below]{$x_n$} -- (5.0,1.5) node[above=4pt]{$y_n$};
\draw[thick, <->] (0,6) --node[above, rotate=90, font=\bfseries] (Y) {$f(x)$} (0,0) -- (7,0) node[below, font=\bfseries] {Quad. points};
\end{scope}
\end{tikzpicture}
\end{document}