You are on page 1of 1

Ejemplos en Python

Curso de operación de sistemas eléctricos

Alejandro Garcés (alejandro.garces@utp.edu.co)

1 Instalación 5 numpy
• Instalar Anaconda
1 import numpy as np
• Instalar Build Tools for Visual Studio 2 S = np . matrix ([[1+8 j , 2+3 j ] , [3 , 4+2 j ]])
3 print ( S )
• Abrir Anaconda Promopt como administrador: 4 d = np . linalg . det ( S )
– python -m pip install –upgrade pip 5 print ( d )
6 W = np . linalg . inv ( S )
– pip install cvxpy
7 print ( W )
– pip install nose 8 Q = np . linalg . eigvals ( S )
– nosetests cvxpy 9 print ( Q )

6 Funciones
2 Grafica simple
1 import numpy as np
1 import numpy as np
2 import matplotlib . pyplot as gr
2 import matplotlib . pyplot as gr
3 def T ( x ):
3 x = np . arange (0 , 5 , 0.1)
4 " mapa de contraccion "
4 y = np . sin ( x )
5 return np . sqrt (5* x )
5 z = np . cos ( x )
6 def f ( x ):
6 gr . plot (x ,y , label = " sine function " )
7 " funcion original "
7 gr . plot (x ,z , label = " cosine function " )
8 return x - np . sqrt (5* x )
8 gr . grid ( True )
9 x = 100
9 gr . xlabel ( ’ values x ’)
10 v = [ f ( x )]
10 gr . ylabel ( ’ values y ’)
11 for k in range (0 ,20):
11 gr . legend ()
12 x = T(x)
12 gr . show ()
13 print ( ’ variable ’ ,x , ’ funcion ’ , f ( x ))
3 Grafica de barras 14 v +=[ f ( x )]
15 gr . plot ( v )
16 gr . grid ()
1 import numpy as np
2 import matplotlib . pyplot as bp 7 Ejemplo programación cuadrática
3 m = ( ’A ’ , ’B ’ , ’C ’ , ’D ’ , ’F ’)
4 s = [10 , 3 , 15 , 8 , 12 ]
5 bp . rcdefaults () 1 import cvxpy as cvx
6 bp . bar (m , s ) 2 import numpy as np
7 bp . grid ( True ) 3 x = cvx . Variable ()
4 y = cvx . Variable ()
4 Grafico en 3D 5 objective = cvx . Minimize (10* x **2+20* y **2)
6 constraints = [x >= 0 ,
7 y >=0 ,
1 from mpl_toolkits . mplot3d import Axes3D
8 x + y ==100 ,
2 import matplotlib . pyplot as plt
9 x <=80 ,
3 import numpy as np
10 y <=80]
4 fig = plt . figure ()
11 prob = cvx . Problem ( objective , constraints )
5 ax = fig . gca ( projection = ’3 d ’)
12 prob . solve ( verbose = True )
6 x = np . arange ( -1 , 1 , 0.01)
13 print ( " optimal value " , prob . value )
7 y = np . arange ( -1 , 1 , 0.01)
14 print ( " optimal var " , x . value , y . value )
8 x , y = np . meshgrid (x , y )
15 print ( constraints [0]. dual_value )
9 z = x*x + y*y
10 surf = ax . plot_surface (x , y , z )
11 plt . show ()

You might also like