예제 #1
0
def newtonRaphson(x, tol=1.0e-9):
    def f(x):
        return x * x - 1

    def jacobian(f, x):
        h = 1.0e-4
        n = len(x)
        jac = np.zeros((n, n))
        f0 = f(x)
        for i in range(n):
            temp = x[i]
            x[i] = temp + h
            f1 = f(x)
            x[i] = temp
            jac[:, i] = (f1 - f0) / h
        return jac, f0

    for i in range(30):
        jac, f0 = jacobian(f, x)
        if math.sqrt(np.dot(f0, f0) / len(x)) < tol:
            print(x)
        dx = gaussPivot(jac, -f0)
        x = x + dx
        if math.sqrt(np.dot(dx, dx)) < tol * max(max(abs(x)), 1.0): return x
    print('Too many iterations')
def newtonRaphson2(f,x,tol=1.0e-9,s=None):
    
    def jacobian(f,x):
        h = 1.0e-4
        n = len(x)
        jac = np.zeros((n,n))
        f0 = f(x)
        for i in range(n):
            temp = x[i]
            x[i] = temp + h
            f1 = f(x)
            x[i] = temp
            jac[:,i] = (f1 - f0)/h
        return jac,f0
    
    for i in range(30):
        jac,f0 = jacobian(f,x)
        if math.sqrt(np.dot(f0,f0)/len(x)) < tol:
            return x
        dx = gaussPivot(jac,-f0)
        x = x + dx
        if s != None:
           s.append(x)
        if math.sqrt(np.dot(dx,dx)) < tol*max(max(abs(x)),1.0): return x
    print('Too many iterations')
예제 #3
0
def polyFit(xData,yData,m):
    a = zeros((m+1,m+1))
    b = zeros(m+1)
    s = zeros(2*m+1)
    for i in range(len(xData)):
        temp = yData[i]
        for j in range(m+1):
            b[j] = b[j] + temp
            temp = temp*xData[i]
        temp = 1.0
        for j in range(2*m+1):
            s[j] = s[j] + temp
            temp = temp*xData[i]
    for i in range(m+1):
        for j in range(m+1):
            a[i,j] = s[i+j]
    return gaussPivot(a,b)
예제 #4
0
def polyFit(xData, yData, m):
    a = zeros((m + 1, m + 1))
    b = zeros(m + 1)
    s = zeros(2 * m + 1)
    for i in range(len(xData)):
        temp = yData[i]
        for j in range(m + 1):
            b[j] = b[j] + temp
            temp = temp * xData[i]
        temp = 1.0
        for j in range(2 * m + 1):
            s[j] = s[j] + temp
            temp = temp * xData[i]
    for i in range(m + 1):
        for j in range(m + 1):
            a[i, j] = s[i + j]
    return gaussPivot(a, b)
예제 #5
0
def newtonRaphsonSEnoL(f, x, tol=1.0e-9):
    def jacobian(f, x):
        h = 1.0e-4
        n = len(x)
        jac = zeros((n, n), dtype=float)
        f0 = f(x)
        for i in range(n):
            temp = x[i]
            x[i] = temp + h
            f1 = f(x)
            x[i] = temp
            jac[:, i] = (f1 - f0) / h
        return jac, f0

    for i in range(30):
        jac, f0 = jacobian(f, x)
        if sqrt(dot(f0, f0) / len(x)) < tol: return x
        dx = gaussPivot(jac, -f0)
        x = x + dx
        if sqrt(dot(dx, dx)) < tol * max(max(abs(x)), 1.0): return x
    print 'Too many iterations'
예제 #6
0
def newtonRaphson2(f,x,tol=1.0e-9):
    
    def jacobian(f,x):
        h = 1.0e-4
        n = len(x)
        jac = zeros((n,n),type=Float64)
        f0 = f(x)
        for i in range(n):
            temp = x[i]
            x[i] = temp + h
            f1 = f(x)
            x[i] = temp
            jac[:,i] = (f1 - f0)/h
        return jac,f0
    
    for i in range(30):
        jac,f0 = jacobian(f,x)
        if sqrt(dot(f0,f0)/len(x)) < tol: return x
        dx = gaussPivot(jac,-f0)
        x = x + dx
        if sqrt(dot(dx,dx)) < tol*max(max(abs(x)),1.0): return x
    print 'Too many iterations'
 def newtonRaphson2(f,q,tol=1.0e-15): ##1.0e-9 may be too large
     for i in range(50):                                                                                                                                    
         h = 1.0e-4      ##1.0e-4 may be too large.
         n = len(q)
         jac = zeros((n,n))
         if 1.0+q[0]-x<0.0:
             #print "Problem occurs i=",i
             #print "PROBLEM OCCURS.......... 1.0+q[0]-x=",1.0+q[0]-x
             q[0] = x-1.0 + 0.0001
             #print "NOW problem is fixed as  1.0+q[0]-x=",1.0+q[0]-x
         f0 = f(q)
         for i in range(n):
             temp = q[i]
             q[i] = temp + h
             f1 = f(q)
             q[i] = temp
             jac[:,i] = (f1 - f0)/h
         if sqrt(dot(f0,f0)/len(q)) < tol: return q
         dq = gaussPivot(jac,-f0)
         q = q + dq
         if sqrt(dot(dq,dq)) < tol*max(max(abs(q)),1.0): return q
     print('Too many iterations')    
 def newtonRaphson2(f,q,tol=1.0e-15): ##1.0e-9 may be too large
     for i in range(50):                                                                                                                                    
         h = 1.0e-4      ##1.0e-4 may be too large.
         n = len(q)
         jac = zeros((n,n))
         if 1.0+q[0]-x<0.0:
             #print "Problem occurs i=",i
             #print "PROBLEM OCCURS.......... 1.0+q[0]-x=",1.0+q[0]-x
             q[0] = x-1.0 + 0.0001
             #print "NOW problem is fixed as  1.0+q[0]-x=",1.0+q[0]-x
         f0 = f(q)
         for i in range(n):
             temp = q[i]
             q[i] = temp + h
             f1 = f(q)
             q[i] = temp
             jac[:,i] = (f1 - f0)/h
         if sqrt(dot(f0,f0)/len(q)) < tol: return q
         dq = gaussPivot(jac,-f0)
         q = q + dq
         if sqrt(dot(dq,dq)) < tol*max(max(abs(q)),1.0): return q
     print 'Too many iterations'    
예제 #9
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
예제 #10
0
#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


예제 #11
0
    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

if in001==2 and in005==2:
    d=LUdecomp(c) #LU decomposition using LUdecomp. Arguments:(square coefficients matrix)
    e=LUsolve(d,b) #solves the decomposed matrix. Arguments:(result of LUdecomp, dependent variable matrix)
    print 'The variables are: ', '\n', e #outputs the result of the LUsolve
    
###Gaussian Pivot
#solves a set of simultanious equations

if in001==3:
    d=gaussPivot(c,b)
    print 'The variables are: ' '\n' , d #outputs result of gaussPivot

###Fourier Transform
#finds the coefficients of a Fourier series and plots the result

if in001==4:
    FT001=np.fft.rfft(a) #Performs the real Fast Fourier Transform. Arguments:(source data)
    for i in range(len(FT001)): #loop removes the requested percentage of data from the array of coefficients, this smooths the graph
        if i>(len(FT001))*(1-(in007*0.01)):
            FT001[i]=0
    iFT001=np.fft.irfft(FT001) #performs the inverse real Fast Fourier Transform. Argument:(Fourier coefficient array)
    plt.figure(1)
    plt.plot(b,a,'g-',label='Original Data')
    plt.plot(b,iFT001,'r-',label='Smoothed Fourier Transform')
    plt.xlabel(in008)