501
edits
Line 452: | Line 452: | ||
=== PT1 / Low-pass filter step response === | === PT1 / Low-pass filter step response === | ||
<!-- {{SyntaxHighlight| --> | <!-- {{SyntaxHighlight| --> | ||
{{Code|Creating a transfer function and plotting its response|<syntaxhighlight lang="octave" style="font-size:14px"> | {{Code|Creating a transfer function and plotting its response|<syntaxhighlight lang="octave" style="font-size:14px"> | ||
T1 = 0.4; # time constant | T1 = 0.4; # time constant | ||
P = tf([1], [T1 1]) | P = tf([1], [T1 1]); # create transfer function model | ||
step(P, 2) # plot step response | step(P, 2) # plot step response | ||
Line 468: | Line 469: | ||
[[File:Pt1.png|600px]] | [[File:Pt1.png|600px]] | ||
==== Bode Diagram for TikZ/PGFplots ==== | |||
We use the same system as before but we draw now with the <code style="font-size:14px; border:solid lightgray 1px; padding: 2px">bode(P)</code> a bode diagram. | |||
The output is then written to a .csv file. | |||
<!-- {{SyntaxHighlight| --> | |||
{{Code|Creating a transfer function and plotting its response|<syntaxhighlight lang="octave" style="font-size:14px"> | |||
T1 = 0.4; # time constant | |||
P = tf([1], [T1 1]); | |||
[mag, pha, w] = bode(P); | |||
csvwrite("dat/bode_p.csv", [w', 20*log10(mag), pha]); | |||
</syntaxhighlight>}} | |||
We can then invoke LaTeX with the following .tex file | |||
<!-- {{SyntaxHighlight| --> | |||
{{Code|Creating a transfer function and plotting its response|<syntaxhighlight lang="latex" style="font-size:14px"> | |||
\documentclass[tikz]{standalone} | |||
\usepackage{pgfplotstable} | |||
\usepackage{siunitx} | |||
\usetikzlibrary{pgfplots.groupplots} | |||
\begin{document} | |||
\pgfplotstableread[col sep=comma]{\detokenize{/path/to/csv/file/dat/bode_p.csv}}\datatable | |||
\begin{tikzpicture} | |||
\begin{groupplot}[ | |||
group style={rows=2}, | |||
width=0.8\textwidth, | |||
height=0.4\textwidth, | |||
xmajorgrids, | |||
ymajorgrids, | |||
enlarge x limits=false, | |||
] | |||
\nextgroupplot[ | |||
title={Bode Diagram of $P$}, | |||
xmode=log, | |||
ylabel={Magnitude / \si{\decibel}}, | |||
] | |||
\addplot[blue,line width=2pt] table[x index=0,y index=1]{\datatable}; | |||
\nextgroupplot[ | |||
xmode=log, | |||
xlabel={Frequency / \si{\radian\per\second}}, | |||
ylabel={Phase / \si{\degree}}, | |||
] | |||
\addplot[red,line width=2pt] table[x index=0,y index=2]{\datatable}; | |||
\end{groupplot} | |||
\end{tikzpicture} | |||
\end{document} | |||
</syntaxhighlight>}} | |||
to generate a beautiful bode diagram | |||
[[File:bode_p.png]] | |||
Try also (a first order low-pass filter has -3db magnitude at f=1/T1). | |||
=== Inverted Pendulum === | === Inverted Pendulum === |
edits