这是一个 MATLAB 代码
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);
是否可以用 tikz/pgfplots 绘制这个?
答案1
我使用与键gnuplot
sum
相结合的函数,允许用户给出将要执行的原始命令。tikz
raw gnuplot
tikz/pgfplots
gnuplot
\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}
答案2
该Asymptote
解决方案使用一个函数来评估总和,可以根据您的需要进行调整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}
为了处理它latexmk
,请创建文件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");
然后运行latexmk -pdf f.tex
。
答案3
我会考虑使用优秀的matlab2tikz,将 MATLAB 绘图转换为 TikZ 图形。这可能是杰克在他的评论中建议。
我matlab2tikz()
在上面添加了你的 MATLAB 代码(复制后这三个文件从存储库复制到同一目录中,阅读说明),这会产生一些代码,我手动清理了一下。结果如下。
这当然只是数据的导出,你也可以直接用PGFPlots
\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}