author | Shantanu <shantanu@fossee.in> |
Thu, 22 Apr 2010 13:08:06 +0530 | |
changeset 102 | 84e1dcb52908 |
parent 82 | c7abfeddc958 |
permissions | -rw-r--r-- |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
1 |
* Solving Equations |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
2 |
*** Outline |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
3 |
***** Introduction |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
4 |
******* What are we going to do? |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
5 |
******* How are we going to do? |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
6 |
******* Arsenal Required |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
7 |
********* working knowledge of arrays |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
8 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
9 |
*** Script |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
10 |
Welcome. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
11 |
|
81 | 12 |
In this tutorial we shall look at solving linear equations, obtaining |
82 | 13 |
roots of polynomial and non-linear equations. In the process, we |
81 | 14 |
shall look at defining functions as well. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
15 |
|
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
16 |
We would be using concepts related to arrays which we have covered |
81 | 17 |
in a previous tutorial. |
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
18 |
|
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
19 |
Let's begin with solving linear equations. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
20 |
{show a slide of the equations} |
82 | 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 |
|
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
24 |
equations. Solve requires the coefficients and the constants to |
82 | 25 |
be in the form of matrices of the form Ax = b to solve the system of linear equations. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
26 |
|
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
27 |
Lets start ipython -pylab interpreter. |
81 | 28 |
We begin by entering the coefficients and the constants as |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
29 |
matrices. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
30 |
|
82 | 31 |
In []: A = array([[3,2,-1], |
32 |
[2,-2,4], |
|
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
33 |
[-1, 0.5, -1]]) |
82 | 34 |
|
35 |
A is a 3X3 matrix of the coefficients of x, y and z |
|
36 |
||
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
37 |
In []: b = array([1, -2, 0]) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
38 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
39 |
Now, we can use the solve function to solve the given system. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
40 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
41 |
In []: x = solve(A, b) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
42 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
43 |
Type x, to look at the solution obtained. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
44 |
|
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
45 |
Equation is of the form Ax = b, so we verify the solution by |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
46 |
obtaining a matrix product of A and x, and comparing it with b. |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
47 |
As we have covered earlier that we should use the dot function |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
48 |
here, and not the * operator. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
49 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
50 |
In []: Ax = dot(A, x) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
51 |
In []: Ax |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
52 |
|
81 | 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. |
|
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
56 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
57 |
allclose checks if two matrices are close enough to each other |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
58 |
(with-in the specified tolerance level). Here we shall use the |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
59 |
default tolerance level of the function. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
60 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
61 |
In []: allclose(Ax, b) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
62 |
The function returns True, which implies that the product of A & |
82 | 63 |
x is very close to the value of b. This validates our solution x. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
64 |
|
82 | 65 |
Let's move to finding the roots of a polynomial. We shall use the |
66 |
roots function for this. |
|
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
67 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
68 |
The function requires an array of the coefficients of the |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
69 |
polynomial in the descending order of powers. |
82 | 70 |
Consider the polynomial x^2-5x+6 = 0 |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
71 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
72 |
In []: coeffs = [1, -5, 6] |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
73 |
In []: roots(coeffs) |
81 | 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]) |
|
82 | 77 |
As you can see, the roots of that equation are of the form a + bj |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
78 |
|
81 | 79 |
What if I want the solution of non linear equations? |
82 | 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. |
|
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
84 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
85 |
In []: from scipy.optimize import fsolve |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
86 |
|
81 | 87 |
Now, let's look at the documentation of fsolve by typing fsolve? |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
88 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
89 |
In []: fsolve? |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
90 |
|
81 | 91 |
As mentioned in documentation the first argument, func, is a python |
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
92 |
function that takes atleast one argument. So, we should now |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
93 |
define a python function for the given mathematical expression |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
94 |
sin(x)+cos^2(x). |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
95 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
96 |
The second argument, x0, is the initial estimate of the roots of |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
97 |
the function. Based on this initial guess, fsolve returns a root. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
98 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
99 |
Before, going ahead to get a root of the given expression, we |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
100 |
shall first learn how to define a function in python. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
101 |
Let's define a function called f, which returns values of the |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
102 |
given mathematical expression (sin(x)+cos^2(x)) for a each input. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
103 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
104 |
In []: def f(x): |
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
105 |
... return sin(x)+cos(x)*cos(x) |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
106 |
... |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
107 |
... |
82 | 108 |
hit the enter key to come out of function definition. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
109 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
110 |
def, is a key word in python that tells the interpreter that a |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
111 |
function definition is beginning. f, here, is the name of the |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
112 |
function and x is the lone argument of the function. The whole |
82 | 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 |
|
81 | 115 |
earlier tutorials. Our function f has just one line in it's |
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
116 |
definition. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
117 |
|
81 | 118 |
We can test our function, by calling it with an argument for |
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
119 |
which the output value is known, say x = 0. We can see that |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
120 |
sin(x) + cos^2(x) has a value of 1, when x = 0. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
121 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
122 |
Let's check our function definition, by calling it with 0 as an |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
123 |
argument. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
124 |
In []: f(0) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
125 |
We can see that the output is as expected. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
126 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
127 |
Now, that we have our function, we can use fsolve to obtain a root |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
128 |
of the expression sin(x)+cos^2(x). Recall that fsolve takes |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
129 |
another argument, the initial guess. Let's use 0 as our initial |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
130 |
guess. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
131 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
132 |
In []: fsolve(f, 0) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
133 |
fsolve has returned a root of sin(x)+cos^2(x) that is close to 0. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
134 |
|
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
135 |
That brings us to the end of this tutorial. We have covered solution |
81 | 136 |
of linear equations, finding roots of polynomials and non-linear |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
137 |
equations. We have also learnt how to define functions and call |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
138 |
them. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
139 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
140 |
Thank you! |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
141 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
142 |
*** Notes |