I am working on coding a backward Euler method in Python and I am having problems coding the Newton part. We are given a tolerance of 1e-4 and using this I am getting very small numbers in the output vector for my Newton's method.
This is my code:
def Newt(U,x,tol): #takes in matrix, x vector, tolerance
error=np.matrix([[1],[1]]) #set an error matrix of 1,1
while abs(LA.norm(error))>tol:
func=function(U,x) #define a f(x) vector
jac=functionprime(U,x) #define the inverse jacobian vector
y0=jac*func #the change vector is the jacobian inverse times the function
xn=x-y0 #the new x is the difference
error=xn-x #set the error
x=xn
print(x)
For this problem, I am using these function functions:
A=np.matrix([[-33.4, 66.6], [33.3, -66.7]]) #A matrix
x0=np.matrix([[3],[0]]) #x0 verctor
def function(A,x):
z=A*x
return(z) #all my function does is multiply the matrix by the x vector
def functionprime(A,x0):
b=(1/10)*np.matrix([[-66.7,-66.6],[-33.3,-33.4]]) #tried to code this to just output the inverse jacobian
return(b)
When I run it I get a matrix:
[[ 4.31664687e-27],
[ 2.15832344e-27]]
which is way too small to use in my backwards Euler function, which makes me think there is something wrong with my Newton. Can anyone help me figure out what I am doing wrong here? From my understanding of the Newton function this seems like it should be the correct thing, but clearly it's not doing exactly what I need it to do.
Also to run this function at the top of my code I have:
import matplotlib.pyplot as plt
import math
import numpy as np
from pylab import *
from numpy import linalg as LA
which are not all needed for this, but some are!