8 |
8 |
9 *** Script |
9 *** Script |
10 Welcome. |
10 Welcome. |
11 |
11 |
12 In this tutorial we shall look at solving linear equations, obtaining |
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 |
13 roots of polynomial and non-linear equations. In the process, we |
14 shall look at defining functions as well. |
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 a previous tutorial. |
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 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 |
22 equations. Solve requires the coefficients and the constants to |
24 equations. Solve requires the coefficients and the constants to |
23 be in the form of matrices to solve the system of linear equations. |
25 be in the form of matrices of the form Ax = b to solve the system of linear equations. |
24 |
26 |
25 Lets start ipython -pylab interpreter. |
27 Lets start ipython -pylab interpreter. |
26 We begin by entering the coefficients and the constants as |
28 We begin by entering the coefficients and the constants as |
27 matrices. |
29 matrices. |
28 |
30 |
29 In []: A = array([[3,2,-1], |
31 In []: A = array([[3,2,-1], |
30 [2,-2,4], |
32 [2,-2,4], |
31 [-1, 0.5, -1]]) |
33 [-1, 0.5, -1]]) |
|
34 |
|
35 A is a 3X3 matrix of the coefficients of x, y and z |
|
36 |
32 In []: b = array([1, -2, 0]) |
37 In []: b = array([1, -2, 0]) |
33 |
38 |
34 Now, we can use the solve function to solve the given system. |
39 Now, we can use the solve function to solve the given system. |
35 |
40 |
36 In []: x = solve(A, b) |
41 In []: x = solve(A, b) |
53 (with-in the specified tolerance level). Here we shall use the |
58 (with-in the specified tolerance level). Here we shall use the |
54 default tolerance level of the function. |
59 default tolerance level of the function. |
55 |
60 |
56 In []: allclose(Ax, b) |
61 In []: allclose(Ax, b) |
57 The function returns True, which implies that the product of A & |
62 The function returns True, which implies that the product of A & |
58 x, and b are close enough. This validates our solution x. |
63 x is very close to the value of b. This validates our solution x. |
59 |
64 |
60 Let's move to finding the roots of polynomials. We shall use the |
65 Let's move to finding the roots of a polynomial. We shall use the |
61 roots function to calculate the roots of a polynomial. |
66 roots function for this. |
62 |
67 |
63 The function requires an array of the coefficients of the |
68 The function requires an array of the coefficients of the |
64 polynomial in the descending order of powers. |
69 polynomial in the descending order of powers. |
65 Consider the polynomial x^2-5x+6 |
70 Consider the polynomial x^2-5x+6 = 0 |
66 |
71 |
67 In []: coeffs = [1, -5, 6] |
72 In []: coeffs = [1, -5, 6] |
68 In []: roots(coeffs) |
73 In []: roots(coeffs) |
69 As we can see, roots returns the result in an array. |
74 As we can see, roots returns the result in an array. |
70 It even works for polynomials with imaginary roots. |
75 It even works for polynomials with imaginary roots. |
71 roots([1, 1, 1]) |
76 roots([1, 1, 1]) |
|
77 As you can see, the roots of that equation are of the form a + bj |
72 |
78 |
73 What if I want the solution of non linear equations? |
79 What if I want the solution of non linear equations? |
74 For that we use the fsolve function. We shall use the function |
80 For that we use the fsolve function. In this tutorial, we shall use |
75 sin(x)+cos^2(x) as our function, in this tutorial. This function |
81 the equation sin(x)+cos^2(x). fsolve is not part of the pylab |
76 is not part of pylab package which we import at the beginning, |
82 package which we imported at the beginning, so we will have to import |
77 so we will have to import it. It is part of scipy package. Let's |
83 it. It is part of scipy package. Let's import it using. |
78 import it using. |
|
79 |
84 |
80 In []: from scipy.optimize import fsolve |
85 In []: from scipy.optimize import fsolve |
81 |
86 |
82 Now, let's look at the documentation of fsolve by typing fsolve? |
87 Now, let's look at the documentation of fsolve by typing fsolve? |
83 |
88 |
98 |
103 |
99 In []: def f(x): |
104 In []: def f(x): |
100 ... return sin(x)+cos(x)*cos(x) |
105 ... return sin(x)+cos(x)*cos(x) |
101 ... |
106 ... |
102 ... |
107 ... |
103 hit the enter key thrice for coming out of function definition. |
108 hit the enter key to come out of function definition. |
104 |
109 |
105 def, is a key word in python that tells the interpreter that a |
110 def, is a key word in python that tells the interpreter that a |
106 function definition is beginning. f, here, is the name of the |
111 function definition is beginning. f, here, is the name of the |
107 function and x is the lone argument of the function. The whole |
112 function and x is the lone argument of the function. The whole |
108 definition of the function is done with in an indented block same |
113 definition of the function is done with in an indented block similar |
109 as the loops and conditional statements we have used in our |
114 to the loops and conditional statements we have used in our |
110 earlier tutorials. Our function f has just one line in it's |
115 earlier tutorials. Our function f has just one line in it's |
111 definition. |
116 definition. |
112 |
117 |
113 We can test our function, by calling it with an argument for |
118 We can test our function, by calling it with an argument for |
114 which the output value is known, say x = 0. We can see that |
119 which the output value is known, say x = 0. We can see that |