Pages

Tuesday, May 8, 2012

Finding zeros of equations

Mathcalc8 has a function called 'zeros' to search for roots of function f(x) = 0. However, it has one limitation. Below is an example to show this limitation :
Find the roots of cos(4x) + cos(2x) = 0 for 0 <= x <= 2π ?

  • Input 1 : f='cos(4*x)+cos(2*x)'
     
  • Input 2 : zeros('f',0,2*Pi)
    Ans. 0.524, 2.62, 3.67, 5.76
     

  • There is four roots found. However, if we make a plot for the function :

  • Input 3 : plot('line','f',0,2*Pi,0.1)
     

  • The resulting graph is :


    We can see that there are actually six roots. The two missing ones are those zero points that are just touching the x-axis but not crossing it. For this type of roots, the function zeros failed to detect them.

    To find the missing two roots, we noticed that the slope of those roots are 0, therefore we can use the derivative function to detect them. There are several ways to do it. One method is to evaluate the product of f(x) and its derivative df(x)/dx instead of f(x) :

  • Input 4 : y=zeros('table(f,z)*diff(f,z)',0,2*Pi,'z')
    Ans. set y = 0, 0.524, 0.912, 1.57, 2.23, 2.619, 3.14, 3.666, 4.054, 4.71, 5.37, 5.76, 6.283
     

  • In using the modified function, we can indeed found all the global/local minimums, global/local maximums and zeros of a function. To distinguish which are the zero points, it is only needed to evaluate their corresponding values :

  • Input 5 : table('f',y)
    Ans. 2, -0,0004, -1.12, 0, -1.125, 0.0004, 2, -0.0004, -1.125, 0, -1.125, 0.0004, 2
     

  • We can see that the values of the 2th, 4th, 6th, 8th, 10th, 12th are very near to zero, so they are the roots.

    In summary, the roots of cos(4x) + cos(2x) = 0 for 0 <= x <= 2π are 0.524, 1.57, 2.62, 3.67, 4.71, 5.76.

    One reason we do not implement the above method into the function zeros is because of its long calculation time. Calculation of a function's derivative can cause a significant time delay which in turn determined by our phone's CPU capability. So, if your phone is running fast, you can use the modified function f(x) df(x)/dx to be inserted to function zeros to find roots for f(x).

    Next, we will show a common roots finding method called 'Newton-Raphson' (NR) Method. The advantage of this method is that no matter the root is just touching the x-axis or crossing it, it can still work. However, because this method uses recursive sequence, it may not guarantee convergence if we choose the wrong initial point.

    To find the root of the function f(x) = 0, NR method uses the following recursive formula :

    xi+1 = xi - f(xi)/(df(x)/dx)x = xi
    

    First, we will make a script called 'nr' for this method as follow :

  • Input 6 : y=one(10)
     
  • Input 7 : y(0)=a
     
  • Input 8 : table('y(z+1)=y(z)-table(f,y(z))/diff(f,y(z))',id(9),'z')
     
  • Input 9 : y
     
  • Input 10 : save('nr')
     

  • Noticed that the value of y(0) is set to another variable a which we can set it externally.

    Now, our script is ready to use. Let us try some examples :
    Find one real root of x3 - x - 1 = 0 ?

  • Input 11 : a=1
    Ans. set a = 1
     

  • This is set to be the initial value of y(0).

  • Input 12 : f='pow(x,3)-x-1'
     
  • Input 13 : load('nr')
     

  • The script would be run and the list y is evaluated :

  • Input 14 : y
    Ans. 1, 1.5, 1.348,......, 1.325
     

  • The list converges to 1.325 which is the real root of the equation x3 - x - 1 = 0. We can check this result through the table function :

  • Input 15 : table('f',y(9))
    Ans. 2.22e-16
     

  • Alternately, we can also use function zeros and find the same root :

  • Input 16 : zeros('f',-5,5)
    Ans. 1.325
     

  • Below is another example :

    Find root of x = e-x ?

  • Input 17 : a=0.5
    Ans. set a = 0.5
     
  • Input 18 : f='x-exp(-x)'
     
  • Input 19 : load('nr')
     
  • Input 20 : y
    Ans. 0.5, 0.566, 0.567,......, 0.5671
     

  • Since y converges to 0.5671, it is the root. To verify the result, we substitute the value back to the equation :

  • Input 21 : table('f',y(9))
    Ans. -1.1e-16
     

  • Also from function zeros :

  • Input 22 : zeros('f',-5,5)
    Ans. 0.567
     

  • The Newton-Raphson method can also be used to solve multi-variables non-linear equations :

    Suppose we want to find n unknown values x = (x1,....., xn) which 
     
    satisfy n non-linear equations
    
    fi(x) = 0 where i is ranged from 1 to n
    
    the Newton-Raphson recursive formula to solve the problem is :
    
    xi+1 = xi - (f1(xi),......, fn(xi))D-1(xi)
    
    where D-1 is the inverse matrix of D with items :
    
    Dij(x) = ∂fj(x)/∂xi
    
    
    It should be noticed that the recursive formula is now in vector form and involved a matrix multiplication term. We can also see that when n equal to 1, D becomes a single value, df/dx and D-1 is 1/(df/dx), the formula would becomes the previous NR formula with single variable.

    In order to handle matrix operations, we need to introduce two new functions from Mathcalc8, namely 'mmult' and 'inv'. The function mmult is used matrix multiplication and inv is used to calculate the inverse of a matrix.

    Below is an example showing how we can solve a set of non-linear equations by multi-variable NR method :

    Find solution for the following two equations :
    
    f1 = x2 + y2 - 15 = 0 
     
    f2 = xy - 5 = 0
    
    for x,y ≥ 0
    

    First, we try the method of 'substitution' to solve this problem. We substitute y = 5/x into equation f1 and uses function zeros :

  • Input 23 : zeros('x*x+pow(5/x,2)-15',0.1,15)
    Ans. 1.381, 3.618
     

  • Two roots of 1.382 and 3.618 are found.

    It is also noticed that we cannot start the searching from 0 due to the term 5/x on the equation which would gives overflow error immediately.

    Now we try the multivariables NR method. First, we need to find the formula for the 2 x 2 matrix D :

    D = (∂f1/∂x , ∂f1/∂y) = (2x , 2y)
        (∂f2/∂x , ∂f2/∂y)   ( y ,  x)
    

    Next,

  • Input 24 : xx=one(10)
     
  • Input 25 : y=one(10)
     
  • Input 26 : xx(0)=2
     

  • We set the initial value for x to be 2 and the initial value of y to be 1.

  • Input 27 : table('a=mmult((pow(xx(x),2)+pow(y(x),2)-15,xx(x)*y(x)-5),inv(((2*xx(x),2*y(x)),(y(x),xx(x)))))|xx(x+1)=xx(x)-a(0)|y(x+1)=y(x)-a(1)',id(9))
     
  • Input 26 : xx
    Ans. 2, 4.833,......, 3.618
     
  • Input 27 : y
    Ans. 1, -0.333,......, 1.382
     

  • The list xx converges to 3.618 and y converges to 1.382. Therefore, one solution of (3.618, 1.382) is found. Moreover, since both x and y are symmetrical on the two equations. We can immediate guess the other solution (1.382, 3.618). These two solutions are in consistence with the values found by function zeros.

    One more limitation of function zeros we want to discuss is that the function that we are searching for zero points must be continuous within the searching range that we specified. Below is an example showing this limitation :

    Find the roots for x - tan(x) = 0 for 0<= x <= 10 ?

    Since tan(x) is not a continuous function within 0 to 10. Applying function zeros to the function x - tan(x) would not work. In general, we need to split our searching range into several continuous ranges to search or in this case, we can modify the equation as follows :

    x - tan(x) = x - sin(x)/cos(x) = (x cos(x) - sin(x))/cos(x) = 0 
    

    So, the roots of x cos(x) - sin(x), which is a continuous function within 0 to 10, are the same as the original equation.

  • Input 28 : zeros('x*cos(x)-sin(x)',0,10)
    Ans. 0, 4.4928, 7.7246
     
  • No comments:

    Post a Comment

    Note: Only a member of this blog may post a comment.