0

I have the vector x1 and x0

x1=np.array([float number, float number, float number])
x0=np.array([float number, float number, float number])

For example, x1=[2.5, 3.1, 0.0], x2=[0.0, 1.0, 1.2] Iteration (i) is a number of my iteration. When (i) is small x1 != x2 but when i is large we can see that x1=x2 I want to find (i) when x1 = x2

For example, i=20, x1=[0.42, 0.53, 0.17], x2=[0.42, 0.53, 0.17] You can see that x1 == x2 so this is my answer. i =20

for i in range(100):
    x1 = iteration(x0)
    print('number iteration=', i, 'x1=', x1, 'x0=', x0)
    if (str(x0[0]) == str(x1[0])) and (str(x0[1]) == str(x1[1])) and    (str(x0[2]) == str(x1[2])):
        print('We solved the problem:')
        break
    x0 = x1
    print(x0)
    print('i=', i, ' our solve: ', x0)

My problem. I see that my answer is (i)= 11 because x1= [ 0.78561487 0.49584664 0.37006133] x0= [ 0.78561487 0.49584664 0.37006133]

Of course, x1 == x2, but Numpy does not see that. Numpy write that my answer is (i)=15 x1= [ 0.78561487 0.49584664 0.37006133] x0= [ 0.78561487 0.49584664 0.37006133]

Why does it happen? I used strings to compare my numbers. How can I get right answer? I mean (i)=11.

My attempt to find a solution I have read this: Why can't decimal numbers be represented exactly in binary?

Ok it's interesting to read but anyway I don't understand how can I solve my problem.

For example, The root (mathematical) reason is that when you are dealing with integers, they are countably infinite. Which means, even though there are an infinite amount of them, we could "count out" all of the items in the sequence, without skipping any. That means if we want to get the item in the 610000000000000th position in the list, we can figure it out via a formula. However, real numbers are uncountably infinite. You can't say "give me the real number at position 610000000000000" and get back an answer. The reason is because, even between 0 and 1, there are an infinite number of values, when you are considering floating-point values. The same holds true for any two floating point numbers.

But this text does not help for me. For example,

input: print(1/3)
output: 0.3333333333333333

We can see very big float number: 0.3333333333333333 But my float numbers are shorter: 0.785614865367

My questions: I have found that number iteration= 11 (i=11). But my program wrote that number_iteration=15. But who is right? Maybe my program knows more and this answer (i=15) is correct? I mean, if I will do so, then whether I will find the right answers? I mean maybe i=11 is not correct answer and i=15 is not correct answer too. Maybe I need to do something else

This is my program:

import numpy as np
# The goal to my program is to find vector v: f1(v)=0, f2(v)=0, f3(v)=0, v=[x,y,z]
#f1(x,y,z)=x^2+y^2+z^2-1=0
#f2(x,y,z)=2x^2+y^2-4z=0
#f3(x,y,z)=3x^2-4y+z^2=0

def f(v):
 # v is a vector, I calculate f1(v), f2(v) and f3(v).
 #return f1(v), f2(v), f3(v)
 return v[0]**2+v[1]**2+v[2]**2-1, 2*v[0]**2+v[1]**2-4*v[2], 3*v[0]**2-4**v[1]+v[2]**2

#calculate Jacobi matrix
def matr_yakobi(v):
        return np.matrix([[2*v[0], 2*v[1], 2*v[2]], [4*v[0], 2*v[1], -4], [6*v[0], -8*v[1], 2*v[2]]])

def iteration(x0):
    #x0 is a vector, my goal is to return vector x2, that will more close   to solve equations f1(x2)=0, f2(x2)=0, f3(x2)=0,
    #My last x2 will solve these equations
    fx0 = np.array(f(x0))
    fxot = fx0[np.newaxis]

    Yakobi = matr_yakobi(x0)
    det = np.linalg.det(Yakobi)
    if det == 0.0:
        print('DET=0, ERROR')
    Yakobi_inverse = Yakobi.I

    x1 = x0.T - (Yakobi_inverse * fxot.T).T
    x2 = np.array([x1[0, 0], x1[0, 1], x1[0, 2]])

    return x2

x0 = np.array([0.5, 0.5, 0.5], dtype='float')
print(x0)

for i in range(100):
    x1 = iteration(x0)
    print('number iteration=', i, 'x1=', x1, 'x0=', x0)
    if (str(x0[0]) == str(x1[0])) and (str(x0[1]) == str(x1[1])) and (str(x0[2]) == str(x1[2])):
        print('We solved the problem:')
        break
    x0 = x1
    print(x0)
print('i=', i, ' our solve: ', x0)
Martin S
  • 814
  • 2
  • 11
  • 24
vjg
  • 131
  • 1
  • 13
  • I'm not sure if there is a better numpy-specific way for this but you can write a function to compare 2 floating point numbers with an "acceptable deviation" range. for example the function would return true if the difference between floats is less than 0.000000001 (or more/less depending on your precision need) – Sweeney Todd Oct 31 '16 at 23:34
  • You are looking for `numpy.isclose` – juanpa.arrivillaga Oct 31 '16 at 23:37
  • juanpa.arrivillaga and Sweeney Todd, thank you for your answers. – vjg Oct 31 '16 at 23:45

0 Answers0