solving-equations.org
changeset 128 fa5c77536e4e
parent 127 76fd286276f7
child 129 dcb9b50761eb
child 146 b92b4e7ecd7b
equal deleted inserted replaced
127:76fd286276f7 128:fa5c77536e4e
     1 * Solving Equations
       
     2 *** Outline
       
     3 ***** Introduction
       
     4 ******* What are we going to do?
       
     5 ******* How are we going to do?
       
     6 ******* Arsenal Required
       
     7 ********* working knowledge of arrays
       
     8 
       
     9 *** Script
       
    10     Welcome. 
       
    11     
       
    12     In this tutorial we shall look at solving linear equations, obtaining
       
    13     roots of polynomial and non-linear equations. In the process, we
       
    14     shall look at defining functions as well. 
       
    15 
       
    16     We would be using concepts related to arrays which we have covered
       
    17     in a previous tutorial.
       
    18 
       
    19     Let's begin with solving linear equations. 
       
    20     {show a slide of the equations}
       
    21     Consider the set of equations,
       
    22     3x + 2y -z = 1, 2x-2y + 4z = -2, -x+ half y-z = 0.
       
    23     We shall use the solve function, to solve the given system of linear
       
    24     equations. Solve requires the coefficients and the constants to
       
    25     be in the form of matrices of the form Ax = b to solve the system of linear equations. 
       
    26 
       
    27     Lets start ipython -pylab interpreter.    
       
    28     We begin by entering the coefficients and the constants as
       
    29     matrices. 
       
    30 
       
    31     In []: A = array([[3,2,-1], 
       
    32                       [2,-2,4],
       
    33                       [-1, 0.5, -1]])
       
    34 
       
    35     A is a 3X3 matrix of the coefficients of x, y and z
       
    36 
       
    37     In []: b = array([1, -2, 0])
       
    38 
       
    39     Now, we can use the solve function to solve the given system. 
       
    40     
       
    41     In []: x = solve(A, b)
       
    42 
       
    43     Type x, to look at the solution obtained. 
       
    44 
       
    45     Equation is of the form Ax = b, so we verify the solution by 
       
    46     obtaining a matrix product of A and x, and comparing it with b. 
       
    47     As we have covered earlier that we should use the dot function 
       
    48     here, and not the * operator. 
       
    49 
       
    50     In []: Ax = dot(A, x)
       
    51     In []: Ax
       
    52 
       
    53     The result Ax, doesn't look exactly like b, but if we carefully
       
    54     observe, we will see that it is the same as b. To save ourself
       
    55     all this trouble, we can use the allclose function. 
       
    56 
       
    57     allclose checks if two matrices are close enough to each other
       
    58     (with-in the specified tolerance level). Here we shall use the
       
    59     default tolerance level of the function. 
       
    60 
       
    61     In []: allclose(Ax, b)
       
    62     The function returns True, which implies that the product of A &
       
    63     x is very close to the value of b. This validates our solution x. 
       
    64 
       
    65     Let's move to finding the roots of a polynomial. We shall use the
       
    66     roots function for this.
       
    67 
       
    68     The function requires an array of the coefficients of the
       
    69     polynomial in the descending order of powers. 
       
    70     Consider the polynomial x^2-5x+6 = 0
       
    71     
       
    72     In []: coeffs = [1, -5, 6]
       
    73     In []: roots(coeffs)
       
    74     As we can see, roots returns the result in an array. 
       
    75     It even works for polynomials with imaginary roots.
       
    76     roots([1, 1, 1])
       
    77     As you can see, the roots of that equation are of the form a + bj
       
    78 
       
    79     What if I want the solution of non linear equations?
       
    80     For that we use the fsolve function. In this tutorial, we shall use
       
    81     the equation sin(x)+cos^2(x). fsolve is not part of the pylab
       
    82     package which we imported at the beginning, so we will have to import
       
    83     it. It is part of scipy package. Let's import it using.
       
    84 
       
    85     In []: from scipy.optimize import fsolve
       
    86 
       
    87     Now, let's look at the documentation of fsolve by typing fsolve?    
       
    88     
       
    89     In []: fsolve?
       
    90 
       
    91     As mentioned in documentation the first argument, func, is a python 
       
    92     function that takes atleast one argument. So, we should now 
       
    93     define a python function for the given mathematical expression
       
    94     sin(x)+cos^2(x). 
       
    95 
       
    96     The second argument, x0, is the initial estimate of the roots of
       
    97     the function. Based on this initial guess, fsolve returns a root. 
       
    98 
       
    99     Before, going ahead to get a root of the given expression, we
       
   100     shall first learn how to define a function in python. 
       
   101     Let's define a function called f, which returns values of the
       
   102     given mathematical expression (sin(x)+cos^2(x)) for a each input. 
       
   103 
       
   104     In []: def f(x):
       
   105     ...        return sin(x)+cos(x)*cos(x)
       
   106     ...
       
   107     ...
       
   108     hit the enter key to come out of function definition. 
       
   109    
       
   110     def, is a key word in python that tells the interpreter that a
       
   111     function definition is beginning. f, here, is the name of the
       
   112     function and x is the lone argument of the function. The whole
       
   113     definition of the function is done with in an indented block similar
       
   114     to the loops and conditional statements we have used in our 
       
   115     earlier tutorials. Our function f has just one line in it's 
       
   116     definition. 
       
   117 
       
   118     We can test our function, by calling it with an argument for
       
   119     which the output value is known, say x = 0. We can see that
       
   120     sin(x) + cos^2(x) has a value of 1, when x = 0. 
       
   121 
       
   122     Let's check our function definition, by calling it with 0 as an
       
   123     argument. 
       
   124     In []: f(0)
       
   125     We can see that the output is as expected. 
       
   126 
       
   127     Now, that we have our function, we can use fsolve to obtain a root
       
   128     of the expression sin(x)+cos^2(x). Recall that fsolve takes
       
   129     another argument, the initial guess. Let's use 0 as our initial
       
   130     guess. 
       
   131 
       
   132     In []: fsolve(f, 0)
       
   133     fsolve has returned a root of sin(x)+cos^2(x) that is close to 0. 
       
   134 
       
   135     That brings us to the end of this tutorial. We have covered solution
       
   136     of linear equations, finding roots of polynomials and non-linear
       
   137     equations. We have also learnt how to define functions and call
       
   138     them. 
       
   139 
       
   140     Thank you!
       
   141 
       
   142 *** Notes