0

I have an assignment for school. First of all can you help me with confirming I have interpreted the question right? And also does the code seem somewhat ok? There have been other tasks before this like create the class with a two dimensional function, write the newton method and so on. And now this question. Im not finished programming it, but Im a bit stuck and I feel like I dont know exactly what to do. On what do I run my Newton method? On the point P. Do I create it like I have done in the Plot method??

This is the question:

Write a method plot that checks the dependence of Newton’s method on several initial vectors x0. This method should plot what is described in the following steps:
• Use the meshgrid command to set up a grid of N2 points in the set G = [a, b]×[c, d] (the parameters N, a, b, c and d are parameters of the methods). You obtain two matrices X and Y where a specific grid point is defined as pij = (Xij , Yij )

class fractals2D(object):
Allzeroes = []       #a list to add all stored values from each run of newtons method
def __init__(self,f, x):
    self.f=f
    f0 = self.f(x)          #giving a variable name with the function to use in ckass
    n=len(x)               #for size of matrice
    jac=zeros([n])        #creates an array to use for jacobian matrice
    h=1.e-8                 #to set h for derivative
    self.jac = jac
    for i in range(n):     #creating loop to take partial derivatives of x and y from x in f
        temp=x[i]  
        #print(x[i])
        x[i]=temp +h        #why setting x[i] two times?  
        #print(x[i])
        f1=f(x)
        x[i]=temp
        #print(x[i])
        jac[:,i]=(f1-f0)/h
def Newtons_method(self,guess):
    f_val = f(guess)
    self.guess = guess

    for i in range(40):


        delta = solve(self.jac,-f_val)
        guess = guess +delta
        if norm((delta),ord=2)<1.e-9:
            return guess    #alist for storing zeroes from one run
def ZeroesMethod(self, point):
    point = self.guess
    self.Newtons_method(point)             
                  #adds zeroes from the run of newtons to a list to store them all
    self.Allzeroes.append(self.guess)
    return (len(self.Allzeroes))          #returns how many zeroes are found 

def plot(self, N, a, b, c, d):
    x = np.linspace(a, b, N)
    y = np.linspace(c, d, N)
    P = [X, Y] = np.meshgrid(x, y)
    return P    #calling ZeroesMethos with our newly meshed point of several arrays







x0 = array([2.0, 1.0])    #creates an x and y value?  
x1= array([1, -5]) 
a= array([2, 8])
b = array([-2, -6])



def f(x):
    f = np.array(
            [x[0]**2 - x[1] + x[0]*cos(pi*x[0]),
             x[0]*x[1] + exp(-x[1]) - x[0]**(-1)])

This is the errormessage im receiving:

delta = solve(self.jac,-f_val) TypeError: bad operand type for unary -: 'NoneTyp

  • Without knowing the code for `Newtons_method`, we can't tell. But there's likely a statement similar to `if data:`; that works fine when `data` is a list, but not when data is a NumPy array. – 9769953 Jan 21 '20 at 16:20
  • You should generally also include the complete traceback: not just a single (last) error line. – 9769953 Jan 21 '20 at 16:21

0 Answers0