|
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, roots |
|
13 of polynomials and other non-linear equations. In the process, we |
|
14 shall look at defining functions. |
|
15 |
|
16 Let's begin with solving linear equations. |
|
17 {show a slide of the equations} |
|
18 We shall use the solve function, to solve this system of linear |
|
19 equations. Solve requires the coefficients and the constants to |
|
20 be in the form of matrices to solve the system of linear equations. |
|
21 |
|
22 We begin by entering the coefficients and the constants as |
|
23 matrices. |
|
24 |
|
25 In []: A = array([[3,2,-1], |
|
26 [2,-2,4], |
|
27 [-1, 0.5, -1]]) |
|
28 In []: b = array([1, -2, 0]) |
|
29 |
|
30 Now, we can use the solve function to solve the given system. |
|
31 |
|
32 In []: x = solve(A, b) |
|
33 |
|
34 Type x, to look at the solution obtained. |
|
35 |
|
36 Next, we verify the solution by obtaining a product of A and x, |
|
37 and comparing it with b. Note that we should use the dot function |
|
38 here, and not the * operator. |
|
39 |
|
40 In []: Ax = dot(A, x) |
|
41 In []: Ax |
|
42 |
|
43 The result Ax, doesn't look exactly like b, but if you carefully |
|
44 observe, you will see that it is the same as b. To save yourself |
|
45 this trouble, you can use the allclose function. |
|
46 |
|
47 allclose checks if two matrices are close enough to each other |
|
48 (with-in the specified tolerance level). Here we shall use the |
|
49 default tolerance level of the function. |
|
50 |
|
51 In []: allclose(Ax, b) |
|
52 The function returns True, which implies that the product of A & |
|
53 x, and b are close enough. This validates our solution x. |
|
54 |
|
55 Let's move to finding the roots of polynomials. We shall use the |
|
56 roots function to calculate the roots of the polynomial x^2-5x+6. |
|
57 |
|
58 The function requires an array of the coefficients of the |
|
59 polynomial in the descending order of powers. |
|
60 |
|
61 In []: coeffs = [1, -5, 6] |
|
62 In []: roots(coeffs) |
|
63 As you can see, roots returns the coefficients in an array. |
|
64 |
|
65 To find the roots of any arbitrary function, we use the fsolve |
|
66 function. We shall use the function sin(x)+cos^2(x) as our |
|
67 function, in this tutorial. First, of all we import fsolve, since it |
|
68 is not already available to us. |
|
69 |
|
70 In []: from scipy.optimize import fsolve |
|
71 |
|
72 Now, let's look at the arguments of fsolve using fsolve? |
|
73 |
|
74 In []: fsolve? |
|
75 |
|
76 The first argument, func, is a python function that takes atleast |
|
77 one argument. So, we should now define a python function for the |
|
78 given mathematical expression sin(x)+cos^2(x). |
|
79 |
|
80 The second argument, x0, is the initial estimate of the roots of |
|
81 the function. Based on this initial guess, fsolve returns a root. |
|
82 |
|
83 Before, going ahead to get a root of the given expression, we |
|
84 shall first learn how to define a function in python. |
|
85 Let's define a function called f, which returns values of the |
|
86 given mathematical expression (sin(x)+cos^2(x)) for a each input. |
|
87 |
|
88 In []: def f(x): |
|
89 return sin(x)+cos(x)*cos(x) |
|
90 |
|
91 def, is a key word in python that tells the interpreter that a |
|
92 function definition is beginning. f, here, is the name of the |
|
93 function and x is the lone argument of the function. The whole |
|
94 definition of the function is done with in an indented block. Our |
|
95 function f has just one line in it's definition. |
|
96 |
|
97 You can test your function, by calling it with an argument for |
|
98 which the output value is know, say x = 0. We can see that |
|
99 sin(x) + cos^2(x) has a value of 1, when x = 0. |
|
100 |
|
101 Let's check our function definition, by calling it with 0 as an |
|
102 argument. |
|
103 In []: f(0) |
|
104 We can see that the output is as expected. |
|
105 |
|
106 Now, that we have our function, we can use fsolve to obtain a root |
|
107 of the expression sin(x)+cos^2(x). Recall that fsolve takes |
|
108 another argument, the initial guess. Let's use 0 as our initial |
|
109 guess. |
|
110 |
|
111 In []: fsolve(f, 0) |
|
112 fsolve has returned a root of sin(x)+cos^2(x) that is close to 0. |
|
113 |
|
114 That brings us to the end of this tutorial on solving linear |
|
115 equations, finding roots of polynomials and other non-linear |
|
116 equations. We have also learnt how to define functions and call |
|
117 them. |
|
118 |
|
119 Thank you! |
|
120 |
|
121 *** Notes |