solving-equations.org
changeset 81 2eff0ebac2dc
parent 79 3893bac8e424
child 82 c7abfeddc958
equal deleted inserted replaced
79:3893bac8e424 81:2eff0ebac2dc
     7 ********* working knowledge of arrays
     7 ********* working knowledge of arrays
     8 
     8 
     9 *** Script
     9 *** Script
    10     Welcome. 
    10     Welcome. 
    11     
    11     
    12     In this tutorial we shall look at solving linear equations, roots
    12     In this tutorial we shall look at solving linear equations, obtaining
    13     of polynomials and other non-linear equations. In the process, we
    13     roots of polynomial and other non-linear equations. In the process, we
    14     shall look at defining functions. 
    14     shall look at defining functions as well. 
    15 
    15 
    16     We would be using concepts related to arrays which we have covered
    16     We would be using concepts related to arrays which we have covered
    17     in previous session
    17     in a previous tutorial.
    18 
    18 
    19     Let's begin with solving linear equations. 
    19     Let's begin with solving linear equations. 
    20     {show a slide of the equations}
    20     {show a slide of the equations}
    21     We shall use the solve function, to solve this system of linear
    21     We shall use the solve function, to solve this system of linear
    22     equations. Solve requires the coefficients and the constants to
    22     equations. Solve requires the coefficients and the constants to
    23     be in the form of matrices to solve the system of linear equations. 
    23     be in the form of matrices to solve the system of linear equations. 
    24 
    24 
    25     Lets start ipython -pylab interpreter.    
    25     Lets start ipython -pylab interpreter.    
    26     Then we begin by entering the coefficients and the constants as
    26     We begin by entering the coefficients and the constants as
    27     matrices. 
    27     matrices. 
    28 
    28 
    29     In []: A = array([[3,2,-1],
    29     In []: A = array([[3,2,-1],
    30                       [2,-2,4],                   
    30                       [2,-2,4],                   
    31                       [-1, 0.5, -1]])
    31                       [-1, 0.5, -1]])
    43     here, and not the * operator. 
    43     here, and not the * operator. 
    44 
    44 
    45     In []: Ax = dot(A, x)
    45     In []: Ax = dot(A, x)
    46     In []: Ax
    46     In []: Ax
    47 
    47 
    48     The result Ax, doesn't look exactly like b, but if you carefully
    48     The result Ax, doesn't look exactly like b, but if we carefully
    49     observe, you will see that it is the same as b. To save yourself
    49     observe, we will see that it is the same as b. To save ourself
    50     this trouble, you can use the allclose function. 
    50     all this trouble, we can use the allclose function. 
    51 
    51 
    52     allclose checks if two matrices are close enough to each other
    52     allclose checks if two matrices are close enough to each other
    53     (with-in the specified tolerance level). Here we shall use the
    53     (with-in the specified tolerance level). Here we shall use the
    54     default tolerance level of the function. 
    54     default tolerance level of the function. 
    55 
    55 
    56     In []: allclose(Ax, b)
    56     In []: allclose(Ax, b)
    57     The function returns True, which implies that the product of A &
    57     The function returns True, which implies that the product of A &
    58     x, and b are close enough. This validates our solution x. 
    58     x, and b are close enough. This validates our solution x. 
    59 
    59 
    60     Let's move to finding the roots of polynomials. We shall use the
    60     Let's move to finding the roots of polynomials. We shall use the
    61     roots function to calculate the roots of the polynomial x^2-5x+6. 
    61     roots function to calculate the roots of a polynomial. 
    62 
    62 
    63     The function requires an array of the coefficients of the
    63     The function requires an array of the coefficients of the
    64     polynomial in the descending order of powers. 
    64     polynomial in the descending order of powers. 
       
    65     Consider the polynomial x^2-5x+6
    65     
    66     
    66     In []: coeffs = [1, -5, 6]
    67     In []: coeffs = [1, -5, 6]
    67     In []: roots(coeffs)
    68     In []: roots(coeffs)
    68     As you can see, roots returns the result in an array. 
    69     As we can see, roots returns the result in an array. 
    69     # It even works for polynomials with imaginary solutions.
    70     It even works for polynomials with imaginary roots.
    70     # roots([1, 1, 1])
    71     roots([1, 1, 1])
    71 
    72 
    72     To find the roots of non linear equations, we use the fsolve
    73     What if I want the solution of non linear equations?
    73     function. We shall use the function sin(x)+cos^2(x) as our
    74     For that we use the fsolve function. We shall use the function
    74     function, in this tutorial. This function is not part of pylab
    75     sin(x)+cos^2(x) as our function, in this tutorial. This function 
    75     package which we import during starting, so we will have to 
    76     is not part of pylab package which we import at the beginning,
    76     import it. It is part of scipy package.
    77     so we will have to import it. It is part of scipy package. Let's
       
    78     import it using.
    77 
    79 
    78     In []: from scipy.optimize import fsolve
    80     In []: from scipy.optimize import fsolve
    79 
    81 
    80     Now, let's look at the documentation of fsolve using fsolve?    
    82     Now, let's look at the documentation of fsolve by typing fsolve?    
    81     
    83     
    82     In []: fsolve?
    84     In []: fsolve?
    83 
    85 
    84     As mentioned in docs the first argument, func, is a python 
    86     As mentioned in documentation the first argument, func, is a python 
    85     function that takes atleast one argument. So, we should now 
    87     function that takes atleast one argument. So, we should now 
    86     define a python function for the given mathematical expression
    88     define a python function for the given mathematical expression
    87     sin(x)+cos^2(x). 
    89     sin(x)+cos^2(x). 
    88 
    90 
    89     The second argument, x0, is the initial estimate of the roots of
    91     The second argument, x0, is the initial estimate of the roots of
    96 
    98 
    97     In []: def f(x):
    99     In []: def f(x):
    98     ...        return sin(x)+cos(x)*cos(x)
   100     ...        return sin(x)+cos(x)*cos(x)
    99     ...
   101     ...
   100     ...
   102     ...
   101     hit return thrice for coming out of function definition. 
   103     hit the enter key thrice for coming out of function definition. 
   102    
   104    
   103     def, is a key word in python that tells the interpreter that a
   105     def, is a key word in python that tells the interpreter that a
   104     function definition is beginning. f, here, is the name of the
   106     function definition is beginning. f, here, is the name of the
   105     function and x is the lone argument of the function. The whole
   107     function and x is the lone argument of the function. The whole
   106     definition of the function is done with in an indented block same
   108     definition of the function is done with in an indented block same
   107     as for loops and conditional statements we have used in our 
   109     as the loops and conditional statements we have used in our 
   108     earlier sessions. Our function f has just one line in it's 
   110     earlier tutorials. Our function f has just one line in it's 
   109     definition. 
   111     definition. 
   110 
   112 
   111     You can test your function, by calling it with an argument for
   113     We can test our function, by calling it with an argument for
   112     which the output value is known, say x = 0. We can see that
   114     which the output value is known, say x = 0. We can see that
   113     sin(x) + cos^2(x) has a value of 1, when x = 0. 
   115     sin(x) + cos^2(x) has a value of 1, when x = 0. 
   114 
   116 
   115     Let's check our function definition, by calling it with 0 as an
   117     Let's check our function definition, by calling it with 0 as an
   116     argument. 
   118     argument. 
   124 
   126 
   125     In []: fsolve(f, 0)
   127     In []: fsolve(f, 0)
   126     fsolve has returned a root of sin(x)+cos^2(x) that is close to 0. 
   128     fsolve has returned a root of sin(x)+cos^2(x) that is close to 0. 
   127 
   129 
   128     That brings us to the end of this tutorial. We have covered solution
   130     That brings us to the end of this tutorial. We have covered solution
   129     of linear equations, finding roots of polynomials and other 
   131     of linear equations, finding roots of polynomials and non-linear
   130     non-linear
       
   131     equations. We have also learnt how to define functions and call
   132     equations. We have also learnt how to define functions and call
   132     them. 
   133     them. 
   133 
   134 
   134     Thank you!
   135     Thank you!
   135 
   136