9

This is a MATLAB code

T=0.3;
A=0.3;
t=0:0.01:4*T;
n1=length(t);
N=100;
s=0;

signal=0;
for i=1:n1
s=0;
for n=1:N
s=s+A*4/(pi*(2*n-1))*sin(2*pi*(2*n-1)/T*t(i));
end
signal(i)=s;
end
plot(t,signal);

Is it possible to draw this with tikz/pgfplots?

enter image description here

  • 6
    Yes. (Would this count as an answer;)?) (Disclaimer: I definitely do not want to be rude or something, I just have a strange sense of humor. I'm pretty sure some code will appear here soon by some tikz geeks, though a bit more specification in the question would be welcome.) – mbork Jun 2 '13 at 20:38
  • 1
    @mbork +1p for a precise answer ;-) – Przemysław Scherwentke Jun 2 '13 at 20:41
  • 2
    Do you actually want to evaluate the function in PGFPlots, or would you be happy to just export the data from Matlab and only do the plotting using PGFPlots? – Jake Jun 2 '13 at 20:48
  • 1
    Welcome to TeX.SX! – mafp Jun 2 '13 at 20:58
  • 3
    I use raw gnuplot code to benefit the sum function. \addplot gnuplot[raw gnuplot] {set samples 1000;fourier(k, x) = 4/((2*k+1)*pi) * cos(2*(2*k+1)*pi*x - pi/2);plot[-.75:.75] sum [k=0:2] fourier(k,x)};. – cjorssen Jun 2 '13 at 21:07
14

I use gnuplot sum function combined with the tikz key raw gnuplot that allows the user to give from tikz/pgfplots the raw commands gnuplot will execute.

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
             xlabel = $t$,
             xtick = {-.5,.5},
             xticklabels = {$-\frac{T}{2}$, $\frac{T}{2}$},
             ytick = {-1,-.5,.5,1},
             yticklabels = {$-A$, $-\frac{A}{2}$, $\frac{A}{2}$, $A$},
             domain = -.75:.75,
             samples = 200,
             legend style = {%
               at = {(0.5,1.02)},
               anchor = south},
             ]
  \addplot[mark = none] gnuplot {(x - floor(x +.5)) < 0 ? -1 : 1};
  \addlegendentry{Signal}
  \foreach \i in {2,4,6,8}{%
    \addplot+[mark = none] gnuplot[raw gnuplot] {%
      set samples 200;
      fourier(k, x) = 4/((2*k+1)*pi) * cos(2*(2*k+1)*pi*x - pi/2);
      plot[-.75:.75] sum [k=0:\i] fourier(k,x)
    };
    \addlegendentryexpanded{\number\numexpr 2*\i+1\relax{} first terms}
}
\end{axis}
\end{tikzpicture}
\end{document}
5

enter image description here

This Asymptote solution uses a function to evaluate sum, that can be adapted to your needs, f.tex:

\documentclass[a4paper]{article}
\usepackage[inline]{asymptote}

\begin{document}
\begin{figure}
\begin{asy}
size(300);
import graph;
real T,A;
T=0.3;
A=0.3;

real tmin=0;
real tmax=1.4;
real ymin=-0.4;
real ymax=0.4;

real signal(real t){
  int NN=400;
  real s=0;
  for(int n=1;n<=NN;++n){
    s=s+A*4/(pi*(2*n-1))*sin(2*pi*(2*n-1)/T*t);
  }
  return s;
}

pen linePen=red+1.2pt;
pen axPen=darkblue+0.5pt;

draw(graph(signal,tmin,tmax,n=200),linePen);

string noLabels(real x){return "";};  // format tick labels (empty)
defaultpen(fontsize(10pt));
xaxis(YEquals(ymin),tmin,tmax,axPen,LeftTicks(Step=0.2));
xaxis(YEquals(ymax),tmin,tmax,axPen,RightTicks(noLabels,Step=0.2));
yaxis(ymin,ymax,axPen,RightTicks(Step=0.1));
yaxis(XEquals(tmax),ymin,ymax,axPen,LeftTicks(noLabels,Step=0.1));

\end{asy}
\caption{Fourier Series Expansion with \texttt{Asymptote}}
\end{figure}
\end{document}

To process it with latexmk, create file latexmkrc:

sub asy {return system("asy '$_[0]'");}
add_cus_dep("asy","eps",0,"asy");
add_cus_dep("asy","pdf",0,"asy");
add_cus_dep("asy","tex",0,"asy");

and run latexmk -pdf f.tex.

3

I would consider to use the excellent matlab2tikz, which converts MATLAB plots to TikZ figures. This is probably what Jake suggested in his comment.

I added matlab2tikz() to your MATLAB code above (after copying these three files from the repository into the same directory, read the instructions), which produced some code which I manually cleaned up a little bit. The result is below.

This is of course "just" an export of the data, you probably could also draw it directly with PGFPlots

result

\documentclass{article}
\usepackage{tikz,pgfplots}
\usepackage\[active,tightpage\]{preview}
    \PreviewEnvironment{tikzpicture}
\begin{document}

\begin{tikzpicture}
    \begin{axis}
        \addplot \[color=blue,solid\]
            table\[row sep=crcr\]{
            0 0\\
            0.01 0.302387144957655\\
            0.02 0.301150753370238\\
            0.03 0.298375570984069\\
            0.04 0.300647458957776\\
            0.05 0.300548549766943\\
            0.06 0.298995957934019\\
            0.07 0.300480519514177\\
            0.08 0.300480519514176\\
            0.09 0.29899595793402\\
            0.1 0.300548549766943\\
            0.11 0.300647458957775\\
            0.12 0.298375570984069\\
            0.13 0.301150753370238\\
            0.14 0.302387144957655\\
            0.15 3.77542115127404e-15\\
            0.16 -0.302387144957655\\
            0.17 -0.301150753370238\\
            0.18 -0.298375570984069\\
            0.19 -0.300647458957775\\
            0.2 -0.300548549766942\\
            0.21 -0.298995957934019\\
            0.22 -0.300480519514176\\
            0.23 -0.300480519514176\\
            0.24 -0.29899595793402\\
            0.25 -0.300548549766943\\
            0.26 -0.300647458957776\\
            0.27 -0.298375570984068\\
            0.28 -0.301150753370237\\
            0.29 -0.302387144957655\\
            0.3 -7.55084230254808e-15\\
            0.31 0.302387144957654\\
            0.32 0.301150753370237\\
            0.33 0.29837557098407\\
            0.34 0.300647458957777\\
            0.35 0.300548549766944\\
            0.36 0.298995957934018\\
            0.37 0.300480519514177\\
            0.38 0.300480519514174\\
            0.39 0.298995957934022\\
            0.4 0.300548549766944\\
            0.41 0.300647458957777\\
            0.42 0.298375570984067\\
            0.43 0.301150753370237\\
            0.44 0.302387144957654\\
            0.45 -8.18728593532897e-15\\
            0.46 -0.302387144957655\\
            0.47 -0.301150753370238\\
            0.48 -0.298375570984068\\
            0.49 -0.300647458957778\\
            0.5 -0.300548549766943\\
            0.51 -0.298995957934016\\
            0.52 -0.300480519514178\\
            0.53 -0.300480519514179\\
            0.54 -0.29899595793402\\
            0.55 -0.300548549766945\\
            0.56 -0.300647458957773\\
            0.57 -0.298375570984064\\
            0.58 -0.301150753370239\\
            0.59 -0.302387144957652\\
            0.6 -1.51016846050962e-14\\
            0.61 0.302387144957655\\
            0.62 0.301150753370239\\
            0.63 0.298375570984065\\
            0.64 0.300647458957779\\
            0.65 0.300548549766947\\
            0.66 0.298995957934013\\
            0.67 0.300480519514179\\
            0.68 0.300480519514178\\
            0.69 0.298995957934022\\
            0.7 0.300548549766941\\
            0.71 0.300647458957774\\
            0.72 0.298375570984066\\
            0.73 0.301150753370235\\
            0.74 0.302387144957653\\
            0.75 -3.68337151806705e-15\\
            0.76 -0.302387144957655\\
            0.77 -0.301150753370235\\
            0.78 -0.298375570984065\\
            0.79 -0.300647458957776\\
            0.8 -0.30054854976694\\
            0.81 -0.298995957934016\\
            0.82 -0.300480519514178\\
            0.83 -0.300480519514175\\
            0.84 -0.298995957934021\\
            0.85 -0.300548549766944\\
            0.86 -0.300647458957778\\
            0.87 -0.298375570984064\\
            0.88 -0.301150753370239\\
            0.89 -0.302387144957646\\
            0.9 -7.08452764472111e-14\\
            0.91 0.302387144957655\\
            0.92 0.301150753370237\\
            0.93 0.298375570984071\\
            0.94 0.300647458957776\\
            0.95 0.300548549766945\\
            0.96 0.298995957934014\\
            0.97 0.300480519514176\\
            0.98 0.300480519514172\\
            0.99 0.298995957934027\\
            1 0.300548549766942\\
            1.01 0.300647458957771\\
            1.02 0.298375570984071\\
            1.03 0.301150753370237\\
            1.04 0.302387144957656\\
            1.05 -3.7046597480602e-14\\
            1.06 -0.302387144957659\\
            1.07 -0.301150753370238\\
            1.08 -0.29837557098407\\
            1.09 -0.30064745895778\\
            1.1 -0.300548549766942\\
            1.11 -0.298995957934014\\
            1.12 -0.300480519514181\\
            1.13 -0.300480519514172\\
            1.14 -0.298995957934024\\
            1.15 -0.300548549766942\\
            1.16 -0.300647458957776\\
            1.17 -0.298375570984067\\
            1.18 -0.301150753370239\\
            1.19 -0.302387144957663\\
            1.2 -3.02033692101923e-14\\
            };
    \end{axis}
\end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.