pgfplots를 사용하여 푸리에 전개 그리기

pgfplots를 사용하여 푸리에 전개 그리기

M = 16인 경우를 플롯해야 합니다. 여기에 이미지 설명을 입력하세요

불행히도 내가하는 방식은 완전히 잘못된 것 같습니다.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{figure}[h] \label{fig:csm:graph}
    \centering
    \begin{tikzpicture}
        \begin{axis}[axis lines = middle, xlabel = $d - z$, ylabel = $I$]
            \def\sum{0}
            \pgfplotsinvokeforeach{1, 2,..., 16}{
                \def\ck{pi * (2 * #1 - 1)}
                \xdef\sum{\sum + (1 / \ck) * sin(\ck * x)}
            }
            \xdef\sum{2 * \sum + 0.5}
            \addplot[domain = -0.5:1, red] (x, {\sum});
        \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

어떤 도움이라도 주시면 감사하겠습니다.

편집: MWE를 추가했습니다.

답변1

더 복잡한 수학의 경우 LaTeX는 적절한 도구가 아닙니다. Sage라는 컴퓨터 대수학 시스템을 사용하여 (무료)세이지매스 클라우드계정을 사용하면 플롯을 빠르게 얻을 수 있습니다.

\documentclass{article}
\usepackage{sagetex}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\begin{document}
\begin{sagesilent}
t = var('t')
x = var('x')
f1 = lambda t: 1
f2 = lambda t: 0
f = Piecewise([[(-1,0),f1],[(0,1),f2]])
Fourier=f.plot_fourier_series_partial_sum(32,1,-.5,.5)
############################
LowerY = -.2
UpperY = 1.2
LowerX = -.5
UpperX = .5
step = .005
g =.5
for i in range(1,17):
    g += -2*(1/(pi*(2*i-1)))*sin((pi*(2*i-1))*x)
x_coords = [t for t in srange(LowerX,UpperX,step)]
y_coords = [g(t).n(digits=6) for t in srange(LowerX,UpperX,step)]

output = r""
output += r"\begin{tikzpicture}[scale=.7]"
output += r"\begin{axis}[xmin=%f,xmax=%f,ymin= %f,ymax=%f]"% (LowerX,UpperX,LowerY, UpperY)
output += r"\addplot[thin, blue, unbounded coords=jump] coordinates {"
for i in range(0,len(x_coords)-1):
    if (y_coords[i])<LowerY or (y_coords[i])>UpperY:
        output += r"(%f , inf) "%(x_coords[i])
    else:
        output += r"(%f , %f) "%(x_coords[i],y_coords[i])
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\begin{center}
\sagestr{output}
\end{center}
\begin{center}
\sageplot[width=6cm]{plot(Fourier, (x, -.5, .5),ymin=-.2,   ymax=1.2,detect_poles=True)}
\end{center}
\end{document}

결과는 다음과 같습니다. 여기에 이미지 설명을 입력하세요 Sage를 사용하여 근사하는 함수(귀하의 경우 1과 0의 조각별 함수)를 알려 주어야 하며 Sage가 나머지를 처리합니다. 이것이 플롯된 두 번째 그림입니다( # 위의 코드). pgfplots를 사용하면 함수를 작성해야 합니다(# 아래의 대부분의 코드가 수행하는 작업입니다). Python은 마지막 숫자를 실행하지 않으므로 루프는 실제로 17이 아닌 16으로 이동합니다.

관련 정보