플롯 연산 (TIKZ, 루프)으로이 함수 그래프를 그리는 방법

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}