author | Santosh G. Vattam <vattam.santosh@gmail.com> |
Sat, 17 Apr 2010 15:51:43 +0530 | |
changeset 81 | 2eff0ebac2dc |
parent 79 | 3893bac8e424 |
child 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 |
13 |
roots of polynomial and other non-linear equations. In the process, we |
|
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} |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
21 |
We shall use the solve function, to solve this system of linear |
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
22 |
equations. Solve requires the coefficients and the constants to |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
23 |
be in the form of matrices to solve the system of linear equations. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
24 |
|
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
25 |
Lets start ipython -pylab interpreter. |
81 | 26 |
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
|
27 |
matrices. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
28 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
29 |
In []: A = array([[3,2,-1], |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
30 |
[2,-2,4], |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
31 |
[-1, 0.5, -1]]) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
32 |
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
|
33 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
34 |
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
|
35 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
36 |
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
|
37 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
38 |
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
|
39 |
|
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
40 |
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
|
41 |
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
|
42 |
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
|
43 |
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
|
44 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
45 |
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
|
46 |
In []: Ax |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
47 |
|
81 | 48 |
The result Ax, doesn't look exactly like b, but if we carefully |
49 |
observe, we will see that it is the same as b. To save ourself |
|
50 |
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
|
51 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
52 |
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
|
53 |
(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
|
54 |
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
|
55 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
56 |
In []: allclose(Ax, b) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
57 |
The function returns True, which implies that the product of A & |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
58 |
x, and b are close enough. This validates our solution x. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
59 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
60 |
Let's move to finding the roots of polynomials. We shall use the |
81 | 61 |
roots function to calculate the roots of a polynomial. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
62 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
63 |
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
|
64 |
polynomial in the descending order of powers. |
81 | 65 |
Consider the polynomial x^2-5x+6 |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
66 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
67 |
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
|
68 |
In []: roots(coeffs) |
81 | 69 |
As we can see, roots returns the result in an array. |
70 |
It even works for polynomials with imaginary roots. |
|
71 |
roots([1, 1, 1]) |
|
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
72 |
|
81 | 73 |
What if I want the solution of non linear equations? |
74 |
For that we use the fsolve function. We shall use the function |
|
75 |
sin(x)+cos^2(x) as our function, in this tutorial. This function |
|
76 |
is not part of pylab package which we import at the beginning, |
|
77 |
so we will have to import it. It is part of scipy package. Let's |
|
78 |
import it using. |
|
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
79 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
80 |
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
|
81 |
|
81 | 82 |
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
|
83 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
84 |
In []: fsolve? |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
85 |
|
81 | 86 |
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
|
87 |
function that takes atleast one argument. So, we should now |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
88 |
define a python function for the given mathematical expression |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
89 |
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
|
90 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
99 |
In []: def f(x): |
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
100 |
... return sin(x)+cos(x)*cos(x) |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
101 |
... |
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
102 |
... |
81 | 103 |
hit the enter key thrice for coming 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
|
104 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
function and x is the lone argument of the function. The whole |
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
108 |
definition of the function is done with in an indented block same |
81 | 109 |
as the loops and conditional statements we have used in our |
110 |
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
|
111 |
definition. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
112 |
|
81 | 113 |
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
|
114 |
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
|
115 |
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
|
116 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
117 |
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
|
118 |
argument. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
119 |
In []: f(0) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
120 |
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
|
121 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
122 |
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
|
123 |
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
|
124 |
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
|
125 |
guess. |
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 |
In []: fsolve(f, 0) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
128 |
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
|
129 |
|
79
3893bac8e424
Added presentation for solving-equations.
Shantanu <shantanu@fossee.in>
parents:
2
diff
changeset
|
130 |
That brings us to the end of this tutorial. We have covered solution |
81 | 131 |
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
|
132 |
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
|
133 |
them. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
134 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
135 |
Thank you! |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
136 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
137 |
*** Notes |