Esempio n. 1
0
def F(x):
    global v, weight
    lam = 100.0
    c = 2.0*sqrt(2.0)
    A = array([[c*x[1] + x[2], -x[2], x[2]]  [-x[2], x[2], -x[2]],  [ x[2],  -x[2], c*x[0] + x[2]]])/c
    
    b = array([0.0, -1.0, 0.0])
    v = gaussElimin(A,b)
    weight = x[0] + x[1] + sqrt(2.0)*x[2]
    penalty = max(0.0,abs(v[1]) - 1.0)**2 + max(0.0,-x[0])**2 + max(0.0,-x[1])**2 + max(0.0,-x[2])**2
    return weight + penalty*lam
Esempio n. 2
0
# [ 2.66666667 1.33333333 4.88888889]
# R = 20.0 ohms
# The currents are (in amps):
# [ 2.4516129 1.41935484 4.77419355]
#
######################################################

R = [5.0, 10.0, 20.0]
for r in R:
    a = np.zeros([3, 3])
    a[0, :] = [50.0 + r, -r, -30.0]
    a[1, :] = [-r, 65.0 + r, -15.0]
    a[2, :] = [-30.0, -15.0, 45.0]
    b = np.array([0, 0, 120.0])
    print("\nR =", r, "ohms")
    print("The currents are (in amps):\n", gaussElimin(a, b))
print("--------------------------------------------")

## problem2_2_18
######################################################
# 1. Populate a and b below with the values from
#    problem 18 on page 82.
#
# Correct Output:
# The loop currents are (in amps):
# [-4.18239492 -2.66455194 -2.71213323 -1.20856463]
######################################################

a = np.zeros([4, 4])
a[0, :] = [80.0, -50.0, -30.0, 0.0]
a[1, :] = [-50.0, 100.0, -10.0, -25.0]
Esempio n. 3
0
import numpy as np
from gaussElimin import *


def vandermode(v):
    n = len(v)
    a = np.zeros((n, n))
    for j in range(n):
        a[:, j] = v**(n - j - 1)
    return a


v = np.array([1.0, 1.2, 1.4, 1.6, 1.8, 2.0])
b = np.array([0.0, 1.0, 0.0, 1.0, 0.0, 1.0])
a = vandermode(v)
aOrig = a.copy()  # Save original matrix
bOrig = b.copy()  # and the constant vector
x = gaussElimin(a, b)
det = np.prod(np.diagonal(a))
print("x =", x)
print("det =", det)
print("Check result: [a]{x} - b = ", np.dot(aOrig, x) - bOrig)
input("Press return to exit")
Esempio n. 4
0
import numpy
from numpy import *
import scipy
from scipy import linalg
from gaussPivot import *
from gaussElimin import *
from numpy.linalg import solve
from LUdecomp import *
import matplotlib.pylab as plt
from scipy import *

G = array(
    [[2.0, -1.0, 0.0, 0.0], [0.0, 0.0, -1.0, 1.0], [0.0, -1.0, 2.0, -1.0],
     [-1.0, 2.0, -1.0, 1.0]]
)  #setting up a matrix for G in order to solve system of simultaneous equations
print "G=", G
H = array([[1.0], [0.0], [0.0], [1.0]])
print "H=", H

I = gaussPivot(G, H)
print "Gauss Pivot solutions are", I
print "x1=1, x2=1, x3=1, x4=1"

J = gaussElimin(
    G, H
)  # this gives different solutions. gauss pivot is designed to avoid dividing by zero or dividing by small numbers
print "Gaussian Elimination solutions are", J
b = "-V1+3V2-V4=0"
c = "-V1+3V3-V4=V(+)"
d = "-V1-V2-V3+4V4=0"

print 'The 4 equations for the voltages are:', a, b, c, d

#Question 1(b)
print 'Since these are simultaneous equations, we can solve them by writing them in matrix form; Ax=B. Matrix A will display the left-hand sides of the equations; B will be a vector consisting of the right-hand sides of the equations. V+=5V.'

A = numpy.array([[4.0, -1.0, -1.0, -1.0], [-1.0, 3.0, 0.0, -1.0],
                 [-1.0, 0.0, 3.0, -1.0], [-1.0, -1.0, -1.0, 4.0]])
B = numpy.array([[5.0], [0.0], [5.0], [0.0]])
print 'A=', A
print 'B=', B
print 'We are now looking for the vector x, which contains the solutions to our equations. We can find x using the gaussElimin package.'
x = gaussElimin(A, B)
print x
print 'From x we can see that V1=3;V2=1.66666667; V3=3.33333333; V4=2'
#Question 1(c)
C = LUdecomp(A)
print 'This is the LU-Decomposition of matrix A:', C

#Question 1(d)
print 'Below are vectors B, as before, and D, where V0=1 instead of 0'
B = numpy.array([[5.0], [0.0], [5.0], [0.0]])
D = numpy.array([[5.0], [1.0], [5.0], [1.0]])
print 'B=', B
print 'D=', D
e = A.copy()
f = solve(A, B)
g = solve(e, D)
#4v1-v2-v3-v4=v+
#-v1+3v2+0v3-v4=0
#-v1+0v2+3v3-v4=v+
#-v1-v2-v3+4v4=0
print '4v1-v2-v3-v4=v+'
print '-v1+3v2+0v3-v4=0'
print '-v1+0v2+3v3-v4=v+'
print '-v1-v2-v3+4v4=0'

print 'Q1b'
x=np.array([[4.0,-1.0,-1.0,-1.0],[-1.0,3.0,0.0,-1.0],[-1.0,0.0,3.0,-1.0],[-1.0,-1.0,-1.0,4.0]], dtype = float)
print x
y=np.array([[5.0],[0.0],[5.0],[0.0]], dtype = float) #creates a matrix
print y

z=gaussElimin(x,y)
print z

print 'Q1c - Using LuDecomposition of our matrix'

from LUdecomp import *

P,L,U=linalg.lu(x) #in this case P is just an identitiy matrix because its just 1

print 'The original matrix'
print (x)
print ''
print 'The L matrix'
print (L)
print ''
print 'The U matrix'
Esempio n. 7
0
#LU Decompositon
from scipy import linalg

#from Q1.py assesment 1 part C
P, L, U = linalg.lu(A)
#where L is the lower triangular matrix, U is the upper triangular matrix and P is the permutaion matrix and A is your matrix you are wishing to decompose

#to solve the syatem Ax=b using the L and U matrices (from Q1.py assesment 1 part d) you have made use
from gaussElimin import *

xL = gaussElimin(L, b)  #gaussElimin changes b so no need to recombine xL and x

x = gaussElimin(
    U,
    b)  #where x is shown as a vector and is the final answer or 'x' from Ax=b

print dot(dot(L, P), U)  #checks answer by geting back to origonal matix
Esempio n. 8
0
## example2_4
from numarray import zeros,Float64,array,product, \
                     diagonal,matrixmultiply
from gaussElimin import *

def vandermode(v):
    n = len(v)
    a = zeros((n,n),type=Float64)
    for j in range(n):
        a[:,j] = v**(n-j-1)
    return a

v = array([1.0, 1.2, 1.4, 1.6, 1.8, 2.0])
b = array([0.0, 1.0, 0.0, 1.0, 0.0, 1.0])
a = vandermode(v)
aOrig = a.copy()    # Save original matrix
bOrig = b.copy()    # and the constant vector
x = gaussElimin(a,b)
det = product(diagonal(a))
print 'x =\n',x
print '\ndet =',det
print '\nCheck result: [a]{x} - b =\n', \
      matrixmultiply(aOrig,x) - bOrig
raw_input("\nPress return to exit")
# GaussElimination

#when you have a matrix A and a vector b you can solve you system using gaussian elimination in the following way say you would solve Ax=b where x is your output 

# x=gaussElimin(A,b) #where A and b are matrices and vectors defined as arrays  
# print x #gives out a vector

#example (assesment 1 Q1)

import numpy as np
from gaussElimin import * 

A=np.array([[4.0,-1.0,-1.0,-1.0],[-1.0,3.0,0.0,-1.0],[-1.0,0.0,3.0,-1.0],[-1.0,-1.0,-1.0,4.0]])
b=np.array([5.0,0.0,5.0,0.0])

x=gaussElimin(A,b)
print x
Esempio n. 10
0
# [ 2.66666667 1.33333333 4.88888889]
# R = 20.0 ohms
# The currents are (in amps):
# [ 2.4516129 1.41935484 4.77419355]
#
######################################################

R = [5.0, 10.0, 20.0]
for r in R:
   a = zeros([3,3])
   a[0,:] = [(50 + r), -r, -30]
   a[1,:] = [-r, (65+r), -15]
   a[2,:] = [-30,-15, 45]
   b = array([0,0,120], float)
   print("\nR =",r,"ohms")
   print("The currents are (in amps):\n",gaussElimin(a,b))
print("--------------------------------------------")

## problem2_2_18
######################################################
# 1. Populate a and b below with the values from
#    problem 18 on page 82.
#
# Correct Output:
# The loop currents are (in amps):
# [-4.18239492 -2.66455194 -2.71213323 -1.20856463]
######################################################

a = zeros([4,4])
a[0,:] = [80, -50, -30, 0]
a[1,:] = [-50, 100, -10, -25]
#LU Decompositon 
from scipy import linalg
 
#from Q1.py assesment 1 part C
P, L, U = linalg.lu(A)
#where L is the lower triangular matrix, U is the upper triangular matrix and P is the permutaion matrix and A is your matrix you are wishing to decompose 

#to solve the syatem Ax=b using the L and U matrices (from Q1.py assesment 1 part d) you have made use 
from gaussElimin import *

xL=gaussElimin(L,b) #gaussElimin changes b so no need to recombine xL and x

x=gaussElimin(U,b) #where x is shown as a vector and is the final answer or 'x' from Ax=b

print dot(dot(L,P),U)#checks answer by geting back to origonal matix 

#Problem sheet 2

from numpy import array
from gaussPivot import *
from gaussElimin import *


a= array([[2.0,-1.0,0.0,0.0], [0.0,0.0,-1.0,1.0], [0.0,-1.0,2.0,-1.0],
[-1.0,2.0,-1.0,1.0]])
b= array([1.0,0.0,0.0,1.0]) #builds the matrix a and b

print 'original matrix'
print a
print 'matrix b'
print b

x=gaussPivot(a,b) #pivots the matrix 
print 'pivot'
print x
print 'part 2'
a= array([[2.0,-1.0,0.0,0.0], [0.0,0.0,-1.0,1.0], [0.0,-1.0,2.0,-1.0],
[-1.0,2.0,-1.0,1.0]])
b= array([1.0,0.0,0.0,1.0]) #builds the matrix a and b

y=gaussElimin(a,b) #does not give an answer, more a nan error

print x
print y


Esempio n. 13
0
    linea[s] = newl.strip()

linea.append('')

itera = int(linea[0])
linea = linea[1:]

for i in range(itera):
    print "Problem #%d" %(i+10)
    print ""
    a, b, linea = getMat(linea)     ##reads matrices from file
    
    printAugMat(a,b)        ##prints original augmented matrix

    print ""
    
    a, b = gaussElimin(a, b)        ##does the gaussian elimination

##    print "Ax = b implies x = %s T." %str(b)
##    print ""
##
##    print "Ax – b = %s T." %str(a)

    printAugMat(a,b)        ##prints solved augmented matrix
    print ""


    


Esempio n. 14
0
------------------------------------------------------------------------------------------------------------------------------------------

#for a matrix that is given as inital values use an array for example
A=np.array([[4.0,-1.0,-1.0,-1.0],[-1.0,3.0,0.0,-1.0],[-1.0,0.0,3.0,-1.0],[-1.0,-1.0,-1.0,4.0]]) 

----------------------------------------------------------------------------------------------------------------------------------------

# GaussElimination / scipy.linalg
#uses the imports 
from gaussElimin import * #where gaussElimin is a file on study direct
from scipy import linalg 

#when you have a matrix A and a vector b you can solve you system using gaussian elimination in the following way say you would solve Ax=b where x is your output 

#from Q1.py assesment 1 part B
x=gaussElimin(A,b) #where A and b are matrices and vectors defined as arrays  
print x #gives out a vector

-----------------------------------------------------------------------------------------
#scipy.linalg

x=linalg.solve(A,b)#where A and b are matrices and vectors defined as arrays solves Ax=b where x is your output given as a vector 

-----------------------------------------------------------------------------------
#LU Decompositopn 
from scipy import linalg
 
#from Q1.py assesment 1 part C
P, L, U = linalg.lu(A)
#where L is the lower triangular matrix, U is the upper triangular matrix and P is the permutaion matrix 
Esempio n. 15
0
    in005=input('Please enter a number: ')
if in001==4:
    in006=raw_input("Please enter the name of the text file that contains your data. Don't forget to affix '.txt': ")
    a=np.loadtxt(in006)#Retrieves data from .txt file.
    b=np.linspace(0,len(a),len(a)) #Creates a variable for the data to be plotted against.
    in007=input('What percentage of the values would you like to remove for smoothing?: ')
    in008=raw_input('Please provide an x axis label: ')
    in009=raw_input('Please provide a y axis label: ')
    in010=raw_input('Please provide a title: ')

##########Workers##########
###Gaussian Elimination
#solves a set of simultanious equations

if in001==1:
    d=gaussElimin(c,b) #performs the elimination
    print 'The variables are: ' '\n' , d #outputs result of gaussElimin

###Lower/Upper decomposition
#solves a set of simultanious equations, uses the decomp method from scipy

if in001==2 and in005==1:
    P,L,U=sp.linalg.lu(c) #LU decomposition using linalg. Arguments:(square coefficients matrix). First element of result is lower matrix, second element is upper matrix.
    z=gaussElimin(dot(L,P),b) #Arguments: (Lower triangular matrix, colomb answer matrix)
    x=gaussElimin(U,b) #Arguments: (Upper triangular matrix, result of gaussElimin of Lower tri and answer matrix)
    print 'Upper matrix: ', '\n',U, '\n', 'Lower matrix: ','\n', np.dot(L,P) #displays the upper and lower matrices
    print 'Testing L dot U, this should be equal to a: ', '\n', np.dot(np.dot(L,P),U)
    print 'The variables are: ', '\n', x #outputs the result of the gaussElimin

#solves a set of simultanious equations, uses the decomp method from textbook