Skip to content

Updated examples to be PEP compliant #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 9, 2019
6 changes: 3 additions & 3 deletions examples/bdalg-matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
sys1ss = ss(A1, B1, C1, 0)
sys1tf = ss2tf(sys1ss)

sys2tf = tf([1, 0.5], [1, 5]);
sys2ss = tf2ss(sys2tf);
sys2tf = tf([1, 0.5], [1, 5])
sys2ss = tf2ss(sys2tf)

# Series composition
series1 = sys1ss + sys2ss;
series1 = sys1ss + sys2ss
123 changes: 78 additions & 45 deletions examples/bode-and-nyquist-plots.ipynb

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions examples/check-controllability-and-observability.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@

from __future__ import print_function

from scipy import * # Load the scipy functions
import numpy as np # Load the scipy functions
from control.matlab import * # Load the controls systems library

# Parameters defining the system

m = 250.0 # system mass
k = 40.0 # spring constant
b = 60.0 # damping constant
k = 40.0 # spring constant
b = 60.0 # damping constant

# System matrices
A = matrix([[1, -1, 1.],
[1, -k / m, -b / m],
[1, 1, 1]])
A = np.array([[1, -1, 1.],
[1, -k/m, -b/m],
[1, 1, 1]])

B = matrix([[0],
[1 / m],
[1]])
B = np.array([[0],
[1/m],
[1]])

C = matrix([[1., 0, 1.]])
C = np.array([[1., 0, 1.]])

sys = ss(A, B, C, 0)

Expand Down
89 changes: 45 additions & 44 deletions examples/genswitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,76 @@
import os

import numpy as np
import matplotlib.pyplot as mpl
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from control import phase_plot, box_grid

# Simple model of a genetic switch
#

# This function implements the basic model of the genetic switch
# Parameters taken from Gardner, Cantor and Collins, Nature, 2000
def genswitch(y, t, mu=4, n=2):
return (mu / (1 + y[1]**n) - y[0], mu / (1 + y[0]**n) - y[1])
return mu/(1 + y[1]**n) - y[0], mu/(1 + y[0]**n) - y[1]

# Run a simulation from an initial condition
tim1 = np.linspace(0, 10, 100)
sol1 = odeint(genswitch, [1, 5], tim1)

# Extract the equlibirum points
mu = 4; n = 2; # switch parameters
eqpt = np.empty(3);
eqpt[0] = sol1[0,-1]
eqpt[1] = sol1[1,-1]
eqpt[2] = 0; # fzero(@(x) mu/(1+x^2) - x, 2);
# Extract the equilibrium points
mu = 4; n = 2 # switch parameters
eqpt = np.empty(3)
eqpt[0] = sol1[0, -1]
eqpt[1] = sol1[1, -1]
eqpt[2] = 0 # fzero(@(x) mu/(1+x^2) - x, 2)

# Run another simulation showing switching behavior
tim2 = np.linspace(11, 25, 100);
sol2 = odeint(genswitch, sol1[-1,:] + [2, -2], tim2)
tim2 = np.linspace(11, 25, 100)
sol2 = odeint(genswitch, sol1[-1, :] + [2, -2], tim2)

# First plot out the curves that define the equilibria
u = np.linspace(0, 4.5, 46)
f = np.divide(mu, (1 + u**n)) # mu / (1 + u^n), elementwise
f = np.divide(mu, (1 + u**n)) # mu/(1 + u^n), element-wise

mpl.figure(1); mpl.clf();
mpl.axis([0, 5, 0, 5]); # box on;
mpl.plot(u, f, '-', f, u, '--') # 'LineWidth', AM_data_linewidth);
mpl.legend(('z1, f(z1)', 'z2, f(z2)')) # legend(lgh, 'boxoff');
mpl.plot([0, 3], [0, 3], 'k-') # 'LineWidth', AM_ref_linewidth);
mpl.plot(eqpt[0], eqpt[1], 'k.', eqpt[1], eqpt[0], 'k.',
eqpt[2], eqpt[2], 'k.') # 'MarkerSize', AM_data_markersize*3);
mpl.xlabel('z1, f(z2)');
mpl.ylabel('z2, f(z1)');
plt.figure(1); plt.clf()
plt.axis([0, 5, 0, 5]) # box on;
plt.plot(u, f, '-', f, u, '--') # 'LineWidth', AM_data_linewidth)
plt.legend(('z1, f(z1)', 'z2, f(z2)')) # legend(lgh, 'boxoff')
plt.plot([0, 3], [0, 3], 'k-') # 'LineWidth', AM_ref_linewidth)
plt.plot(eqpt[0], eqpt[1], 'k.', eqpt[1], eqpt[0], 'k.',
eqpt[2], eqpt[2], 'k.') # 'MarkerSize', AM_data_markersize*3)
plt.xlabel('z1, f(z2)')
plt.ylabel('z2, f(z1)')

# Time traces
mpl.figure(3); mpl.clf(); # subplot(221);
mpl.plot(tim1, sol1[:,0], 'b-', tim1, sol1[:,1], 'g--');
# set(pl, 'LineWidth', AM_data_linewidth);
mpl.plot([tim1[-1], tim1[-1]+1],
[sol1[-1,0], sol2[0,1]], 'ko:',
[tim1[-1], tim1[-1]+1], [sol1[-1,1], sol2[0,0]], 'ko:');
# set(pl, 'LineWidth', AM_data_linewidth, 'MarkerSize', AM_data_markersize);
mpl.plot(tim2, sol2[:,0], 'b-', tim2, sol2[:,1], 'g--');
# set(pl, 'LineWidth', AM_data_linewidth);
mpl.axis([0, 25, 0, 5]);
plt.figure(3); plt.clf() # subplot(221)
plt.plot(tim1, sol1[:, 0], 'b-', tim1, sol1[:, 1], 'g--')
# set(pl, 'LineWidth', AM_data_linewidth)
plt.plot([tim1[-1], tim1[-1] + 1],
[sol1[-1, 0], sol2[0, 1]], 'ko:',
[tim1[-1], tim1[-1] + 1], [sol1[-1, 1], sol2[0, 0]], 'ko:')
# set(pl, 'LineWidth', AM_data_linewidth, 'MarkerSize', AM_data_markersize)
plt.plot(tim2, sol2[:, 0], 'b-', tim2, sol2[:, 1], 'g--')
# set(pl, 'LineWidth', AM_data_linewidth)
plt.axis([0, 25, 0, 5])

mpl.xlabel('Time {\itt} [scaled]');
mpl.ylabel('Protein concentrations [scaled]');
mpl.legend(('z1 (A)', 'z2 (B)')) # 'Orientation', 'horizontal');
# legend(legh, 'boxoff');
plt.xlabel('Time {\itt} [scaled]')
plt.ylabel('Protein concentrations [scaled]')
plt.legend(('z1 (A)', 'z2 (B)')) # 'Orientation', 'horizontal')
# legend(legh, 'boxoff')

# Phase portrait
mpl.figure(2); mpl.clf(); # subplot(221);
mpl.axis([0, 5, 0, 5]); # set(gca, 'DataAspectRatio', [1, 1, 1]);
phase_plot(genswitch, X0 = box_grid([0, 5, 6], [0, 5, 6]), T = 10,
timepts = [0.2, 0.6, 1.2])
plt.figure(2)
plt.clf() # subplot(221)
plt.axis([0, 5, 0, 5]) # set(gca, 'DataAspectRatio', [1, 1, 1])
phase_plot(genswitch, X0=box_grid([0, 5, 6], [0, 5, 6]), T=10,
timepts=[0.2, 0.6, 1.2])

# Add the stable equilibrium points
mpl.plot(eqpt[0], eqpt[1], 'k.', eqpt[1], eqpt[0], 'k.',
eqpt[2], eqpt[2], 'k.') # 'MarkerSize', AM_data_markersize*3);
plt.plot(eqpt[0], eqpt[1], 'k.', eqpt[1], eqpt[0], 'k.',
eqpt[2], eqpt[2], 'k.') # 'MarkerSize', AM_data_markersize*3)

mpl.xlabel('Protein A [scaled]');
mpl.ylabel('Protein B [scaled]'); # 'Rotation', 90);
plt.xlabel('Protein A [scaled]')
plt.ylabel('Protein B [scaled]') # 'Rotation', 90)

if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
mpl.show()
plt.show()
Loading