วิธีการวาดกราฟฟังก์ชันนี้ด้วยการลงจุด (TIKZ, Loop)
Aug 20 2020
ฉันต้องการวาดกราฟฟังก์ชันสูตรการวิเคราะห์ของฟังก์ชันคือ:

ฉันสามารถใช้รหัสต่อไปนี้เพื่อวาดภาพของฟังก์ชันเฉพาะได้
แต่ฉันต้องการกระชับวิธีกำหนดตัวอักษร n
วาดรูปฟังก์ชันที่สอดคล้องกันโดยเปลี่ยน n
ฉันคิดว่ามันควรจะรู้ด้วยลูป แต่ฉันไม่รู้ว่าจะทำอย่างไร
สิ่งที่ต้องทำ?
\documentclass{book}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->,>=stealth] (-4,0) -- (4,0) node[below] {$ x $};
\draw[->,>=stealth] (0,-0.5) -- (0,4) node[right] {$ y $};
\fill (0,0) circle ( 0 ) node[ below left ] { $ O $ } ;
\draw[ domain= -3 : 3 ,samples= 300 ] plot ( \x, {
abs(\x)
});
\draw[ domain= -3 : 3 ,samples= 300 ] plot ( \x, {
abs(\x) + abs((\x) + 1)
});
\draw[ domain= -3 : 3 ,samples= 300 ] plot ( \x, {
abs(\x) + abs((\x) + 1) + abs((\x) + 2)
});
\end{tikzpicture}
\end{document}
คำตอบ
5 ZhiyuanLck Aug 21 2020 at 10:34
ใช้math
ไลบรารี
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}
\pgfset{fpu=true, fpu/output format=fixed}
\tikzmath{
function f(\x, \n) {
real \s;
\s = 0;
for \i in {0,...,\n}{
\s = \s + abs(\x + \i);
};
return \s;
};
}
\pgfset{fpu=false}
\draw[->,>=stealth] (-4,0) -- (4,0) node[below] {$x$};
\draw[->,>=stealth] (0,-0.5) -- (0,15) node[right] {$y$};
\fill (0,0) circle (2pt) node[below left] {$o$};
\foreach \i in {1,...,4} {
\draw [domain=-3:1, samples=300] plot (\x, {f(\x, \i)});
}
\end{tikzpicture}
\end{document}
