Advertisement
Guest User

1 a/b - HW2

a guest
Oct 22nd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. #Determinar a Inversa de A
  4. import numpy as np
  5. import pylab as pl
  6.  
  7.  
  8.  
  9. epsilon = 0.1
  10.  
  11. A = 0.5* np.array([1+epsilon, 1-epsilon, 1, 1]).reshape(2,2) #matriz esta definida
  12.  
  13. def isSquare (m): return all (len (row) == len (m) for row in m)
  14.  
  15. def determinante(a):
  16.     if not isSquare(a):
  17.         return False
  18.     return a[0,0]*a[1,1] - a[1,0]*a[0,1]
  19.    
  20.    
  21. def inversa(a):
  22.     det = determinante(a)
  23.     inv = np.array( [a[1,1],-1*a[0,1],-1*a[1,0],a[0,0] ]).reshape(2,2)
  24.     return 1./det * inv
  25.    
  26. def modulo(a): #Modulo de Frobenius
  27.     return np.sqrt(np.sum(a*a))
  28.    
  29.    
  30. def cond(a):
  31.     mod1 = modulo(inversa(a))
  32.     mod2 = modulo(a)
  33.     return mod1 * mod2
  34.    
  35.  
  36.  
  37. # Parte do MAIN    
  38. print 'Inversa'
  39. print inversa(A)
  40.  
  41.  
  42. set_e = np.linspace(0.001, 0.1, 300)
  43.  
  44. c_nr = np.zeros(np.size(set_e))
  45.  
  46. for l in np.arange(np.size(c_nr)):
  47.     epsilon = set_e[l]
  48.     A = 0.5* np.array([1+epsilon, 1-epsilon, 1, 1]).reshape(2,2)
  49.     c_nr[l] = cond(A)
  50.    
  51. pl.figure()
  52.  
  53. pl.plot(set_e, c_nr)
  54.  
  55. pl.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement