author | Shantanu <shantanu@fossee.in> |
Thu, 19 Nov 2009 22:26:00 +0530 | |
changeset 317 | 0eca6c542fce |
parent 295 | 39d7c4e14585 |
child 321 | 8bf99f747817 |
child 323 | 4e44d7741c94 |
permissions | -rwxr-xr-x |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
1 |
\documentclass[12pt]{article} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
2 |
\title{Solving Equations \& ODEs} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
3 |
\author{FOSSEE} |
317
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
4 |
\usepackage{listings} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
5 |
\lstset{language=Python, |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
6 |
basicstyle=\ttfamily, |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
7 |
commentstyle=\itshape\bfseries, |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
8 |
showstringspaces=false, |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
9 |
} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
10 |
\newcommand{\typ}[1]{\lstinline{#1}} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
11 |
\usepackage[english]{babel} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
12 |
\usepackage[latin1]{inputenc} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
13 |
\usepackage{times} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
14 |
\usepackage[T1]{fontenc} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
15 |
\usepackage{ae,aecompl} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
16 |
\usepackage{mathpazo,courier,euler} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
17 |
\usepackage[scaled=.95]{helvet} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
18 |
\begin{document} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
19 |
\date{} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
20 |
\vspace{-1in} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
21 |
\begin{center} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
22 |
\LARGE{Solving Equations \& ODEs}\\ |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
23 |
\large{FOSSEE} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
24 |
\end{center} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
25 |
\section{Solving linear equations} |
317
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
26 |
Condier following sets of equations:\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
27 |
\begin{align*} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
28 |
3x + 2y - z & = 1 \\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
29 |
2x - 2y + 4z & = -2 \\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
30 |
-x + $\frac{1}{2}$y -z & = 0 |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
31 |
\end{align*}\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
32 |
The matrix representation is:\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
33 |
\begin{center} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
34 |
$A*x = B$ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
35 |
\end{center} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
36 |
Where A is coefficient matrix(in this case 3x3)\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
37 |
B is constant matrix(1x3)\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
38 |
x is the required solution.\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
39 |
\begin{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
40 |
In []: A = array([[3,2,-1], [2,-2,4], [-1, 0.5, -1]]) |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
41 |
In []: B = array([[1], [-2], [0]]) |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
42 |
In []: x = solve(A, B) |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
43 |
\end{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
44 |
Solve the equation $A x = B$ for $x$.\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
45 |
To check whether solution is correct try this: |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
46 |
\begin{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
47 |
In []: Ax = dot(A,x) #Matrix multiplication of A and x(LHS) |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
48 |
In []: allclose(Ax, B) |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
49 |
Out[]: True |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
50 |
\end{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
51 |
\typ{allclose} Returns \typ{True} if two arrays(in above case Ax and B) are element-wise equal within a tolerance. |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
52 |
\newpage |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
53 |
\section{Finding roots} |
317
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
54 |
\subsection{Polynomials} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
55 |
\begin{center} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
56 |
$x^2+6x+13=0$ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
57 |
\end{center} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
58 |
to find roots, pylab provides \typ{roots} function. |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
59 |
\begin{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
60 |
In []: coeffs = [1, 6, 13] #list of all coefficients |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
61 |
In []: roots(coeffs) |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
62 |
\end{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
63 |
\subsection{functions} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
64 |
Functions can be defined and used by following syntax: |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
65 |
\begin{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
66 |
def func_name(arg1, arg2): |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
67 |
#function code |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
68 |
return ret_value |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
69 |
\end{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
70 |
A simple example can be |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
71 |
\begin{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
72 |
def expression(x): |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
73 |
y = x*sin(x) |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
74 |
return y |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
75 |
\end{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
76 |
Above function when called with a argument, will return $xsin(x)$ value for that argument. |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
77 |
\begin{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
78 |
In [95]: expression(pi/2) |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
79 |
Out[95]: 1.5707963267948966 |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
80 |
|
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
81 |
In [96]: expression(pi/3) |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
82 |
Out[96]: 0.90689968211710881 |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
83 |
\end{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
84 |
\subsection{Roots of non-linear eqations} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
85 |
For Finding the roots of a non linear equation(defined as $f(x)=0$), around a starting estimate we use \typ{fsolve}:\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
86 |
\typ{In []: from scipy.optimize import fsolve}\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
87 |
\typ{fsolve} is not part of \typ{pylab}, instead it is part of \textbf{optimize} package of \textbf{scipy}, and hence we \textbf{import} it.\\ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
88 |
%\typ{fsolve} takes first argument as name of function, which evaluates $f(x)$, whose roots one wants to find. And second argument is starting estimate, around which roots are found. |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
89 |
For illustration, we want to find roots of equation: |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
90 |
\begin{center} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
91 |
$f(x)=sin(x)+cos(x)^2$ |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
92 |
\end{center} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
93 |
So just like we did above, we define a function: |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
94 |
\begin{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
95 |
In []: def f(x): |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
96 |
....: return sin(x)+cos(x)**2 |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
97 |
....: |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
98 |
\end{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
99 |
Now to find roots of this non linear equation, around a initial estimate value, say 0, we use \typ{fsolve} in following way: |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
100 |
\begin{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
101 |
In []: fsolve(f, 0) #arguments are function name and estimate |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
102 |
Out[]: -0.66623943249251527 |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
103 |
\end{lstlisting} |
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
104 |
|
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
105 |
\section{ODE} |
317
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
106 |
\begin{lstlisting} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
107 |
In []: def epid(y, t): |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
108 |
.... k, L = 0.00003, 25000 |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
109 |
.... return k*y*(L-y) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
110 |
.... |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
111 |
|
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
112 |
In []: t = arange(0, 12, 0.2) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
113 |
|
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
114 |
In []: y = odeint(epid, 250, t) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
115 |
|
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
116 |
In []: plot(t, y) |
317
0eca6c542fce
Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
117 |
\end{lstlisting} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
118 |
\end{document} |