3

I'm using Python Control Module to plot Bode and Nyquist diagrams of a transfer function. The code is as simple as follows:


# Simple Nyquist plotting

import control
import matplotlib.pyplot as plt

num = 5
den = [1,6,11,6]

#Creating a transfer function G = num/den
G = control.tf(num,den) 

control.nyquist(G)
plt.grid(True)
plt.title('Nyquist Diagram of G(s) = 5/(s+1)(s+2)(s+3)')
plt.xlabel('Re(s)')
plt.ylabel('Im(s)')
plt.show()

The Nyquist diagram is plotted: enter image description here

I wonder if it is possible to increase the graph of the number of points, improving its resolution.

rnt_42
  • 106
  • 3
  • 10

2 Answers2

2

Note that in the plot, all the data points are present. You just have to enlarge the window and you'll see all the points.

You can do that manually (just enlarging the plot window), or you can set the plot window in Matplotlib before showing the result:

If you've already got the figure created you can quickly do this:

fig = matplotlib.pyplot.gcf()
fig.set_size_inches(18.5, 10.5)
fig.savefig('test2png.png', dpi=100)

To propagate the size change to an existing gui window add forward=True

fig.set_size_inches(18.5, 10.5, forward=True)
Community
  • 1
  • 1
jcoppens
  • 5,306
  • 6
  • 27
  • 47
0

python-control library follows a matlab-like syntax so it is best to check first if it is possible to do it as it is in matlab. This time it is indeed. You can actually look at the function signature for hints.

For example in an IPython terminal if we type

cnt.nyquist? 

we get

Signature: cnt.nyquist(syslist, omega=None, Plot=True, color='b', labelFreq=0, *args, **kwargs)
Docstring:
Nyquist plot for a system

Plots a Nyquist plot for the system over a (optional) frequency range.

Parameters
----------
syslist : list of Lti
    List of linear input/output systems (single system is OK)
omega : freq_range
    Range of frequencies (list or bounds) in rad/sec
Plot : boolean
    If True, plot magnitude
labelFreq : int
    Label every nth frequency on the plot
*args, **kwargs:
    Additional options to matplotlib (color, linestyle, etc)

Returns
-------
real : array
    real part of the frequency response array
imag : array
    imaginary part of the frequency response array
freq : array
    frequencies

Examples
--------
>>> sys = ss("1. -2; 3. -4", "5.; 7", "6. 8", "9.")
>>> real, imag, freq = nyquist_plot(sys)
File:      c:\python34\lib\site-packages\control\freqplot.py
Type:      function

So for your case it is simple to fix

num = 5
den = [1,6,11,6]

#Creating a transfer function G = num/den
G = control.tf(num,den) 
w = numpy.logspace(-3,3,5000)
control.nyquist(G,w);
percusse
  • 3,006
  • 1
  • 14
  • 28