rlkamalapurkar / bodeplot

LaTeX package to plot Bode diagrams
LaTeX Project Public License v1.3c
6 stars 1 forks source link

Bug with negative Gain #13

Closed VictuarVi closed 1 month ago

VictuarVi commented 2 months ago

Wrong starting phase

By plotting the following function:

\begin{BodePhPlot}[%
    axes/{height=4cm,
    width=10cm},
    ytick distance=90,
    ylabel={Fase (deg)}, xlabel={Frequenza (rad/s)}
    ]
    {0.01}
    {100}
    \addBodeZPKPlots[%
    true/{green, thick},
    linear/{draw=none},
    asymptotic/{orange,thick}%
    ]
    {phase}
    {%
    z/{0.9},
    p/{-0.2, -8},
    k/-0.5625%
    }
\end{BodePhPlot}

The starting phase should be -180° ($\Re (k) < 0$), while the plot starts at phase 0°: image

also, as you can tell, the zeroes and poles have a "slight off" line. It should be perfectly straight, but it isn't. The transfer function is: $$G(s) = \frac{(-0.9+s)}{(s+0.2)(s+8)}$$ which, from by calculations, has:

For some reason, the magnitude plot too is incorrect:

\begin{BodeMagPlot}[%
    axes/{height=4cm,
    width=10cm,ytick distance=5, ylabel={Guadagno (dB)}, xlabel={Frequenza (rad/s)}},
    ]
    {0.01}
    {100}
    \addBodeZPKPlots[%
    true/{draw=none},
    linear/{draw=none},
    asymptotic/{orange,thick}%
    ]
    {magnitude}
    {%
    z/{0.9},
    p/{-0.2, -8},
    k/-0.5625%
    }
    \end{BodeMagPlot}

image because $20 \log_{10} (|-0.5625|) \approx -5 \text{ dB}$, not $-10$ as seen in the plot.

However, if I change the gain to $k = 1$, the plot becomes correct:

\begin{BodeMagPlot}[%
    axes/{height=4cm,
    width=10cm,ytick distance=5, ylabel={Guadagno (dB)}, xlabel={Frequenza (rad/s)}},
    ]
    {0.01}
    {100}
    \addBodeZPKPlots[%
    true/{draw=none},
    linear/{draw=none},
    asymptotic/{orange,thick}%
    ]
    {magnitude}
    {%
    z/{0.9},
    p/{-0.2, -8},
    k/1%
    }
    \end{BodeMagPlot}

image

rlkamalapurkar commented 2 months ago
z/{0.9},
p/{-0.2, -8},
k/-0.5625%

Gives you the Bode plot of $$G(s) = \frac{-0.5625(-0.9+s)}{(s+0.2)(s+8)}$$, not $$G(s) = \frac{(-0.9+s)}{(s+0.2)(s+8)}$$.

If you want Bode plot of $$G(s) = \frac{(-0.9+s)}{(s+0.2)(s+8)}$$, then k should be 1.

The ZPK form I used is similar to MATLAB, where k is not the DC gain of the system.

rlkamalapurkar commented 2 months ago

The lines not being exactly straight is a consequence of sampling. If you increase the number of samples by changing the command to something like asymptotic/{orange,thick,samples=200}, the asymptotic plot will look better.

VictuarVi commented 2 months ago

Oh wow, thanks! it works (both of the solutions you posted).

I just have one last question. According to the documentation, if I write:

\begin{BodeMagPlot}[%
    axes/{
        ytick distance=10,
        height=4cm,width=10cm,
        xlabel={Frequenza (rad/s)},
        ylabel={Guadagno (dB)},
        },
    commands/{\node at (axis cs: 0.9,-20) [circle,fill,inner sep=0.05cm label=below left:{$p_i$}]{};}
    ]
[...]

in the options of BodeMagPlot, a note should appear. That's nearly the same syntax as the one used in the example of BodeTF (except without the mag/ part).

Despite that, LaTeX returns the error: Package pgfkeys: I do not know the key '/tikz/commands' and I am going to ignore it. Perhaps you misspelled it..

rlkamalapurkar commented 1 month ago

I did not add the commands option to any environments since you can simply add the tikz code inside the environment itself.

\begin{BodeMagPlot}[%
    axes/{
        ytick distance=10,
        height=4cm,width=10cm,
        xlabel={Frequenza (rad/s)},
        ylabel={Guadagno (dB)},
        },
    ]
    \node at (axis cs: 0.9,-20) [circle,fill,inner sep=0.05cm label=below left:{$p_i$}]{};
[...]
\end{BodeMagPlot}
VictuarVi commented 1 month ago

Thanks. It works.