# HG changeset patch # User Shantanu # Date 1271488842 -19800 # Node ID 3893bac8e4248d9162ec1dbc995f953ac6c0ff00 # Parent 099a2cc6c7d2964a7becf447342419b18dc03157 Added presentation for solving-equations. diff -r 099a2cc6c7d2 -r 3893bac8e424 presentations/least-square.tex --- a/presentations/least-square.tex Fri Apr 16 16:16:13 2010 +0530 +++ b/presentations/least-square.tex Sat Apr 17 12:50:42 2010 +0530 @@ -150,7 +150,9 @@ \begin{frame}[fragile] \frametitle{Summary} + \begin{block}{} Obtaining the least fit curve from a data set + \end{block} \end{frame} \begin{frame} diff -r 099a2cc6c7d2 -r 3893bac8e424 presentations/solving-equations.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/presentations/solving-equations.tex Sat Apr 17 12:50:42 2010 +0530 @@ -0,0 +1,154 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +% Title page +\title{Python for Scientific Computing : Least Square Fit} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date{} + +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +\begin{frame} + \frametitle{About the Session} + \begin{block}{Goal} +Finding least square fit of given data-set + \end{block} + \begin{block}{Checklist} + \begin{itemize} + \item pendulum.txt + \end{itemize} + \end{block} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solution of equations} +Consider, + \begin{align*} + 3x + 2y - z & = 1 \\ + 2x - 2y + 4z & = -2 \\ + -x + \frac{1}{2}y -z & = 0 + \end{align*} +Solution: + \begin{align*} + x & = 1 \\ + y & = -2 \\ + z & = -2 + \end{align*} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Scipy Methods - \typ{roots}} +\begin{itemize} +\item Calculates the roots of polynomials +\item To calculate the roots of $x^2-5x+6$ +\end{itemize} +\begin{lstlisting} + In []: coeffs = [1, -5, 6] + In []: roots(coeffs) + Out[]: array([3., 2.]) +\end{lstlisting} +\vspace*{-.2in} +\begin{center} +\includegraphics[height=1.6in, interpolate=true]{data/roots} +\end{center} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Scipy Methods - \typ{fsolve}} +\begin{small} +\begin{lstlisting} + In []: from scipy.optimize import fsolve +\end{lstlisting} +\end{small} +\begin{itemize} +\item Finds the roots of a system of non-linear equations +\item Input arguments - Function and initial estimate +\item Returns the solution +\end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Summary} + \begin{block}{} + \begin{itemize} + \item Solving linear equations + \item Finding roots + \item Roots of non linear equations + \item Basics of Functions + \end{itemize} + \end{block} +\end{frame} + +\begin{frame} + \frametitle{Thank you!} + \begin{block}{} + This session is part of \textcolor{blue}{FOSSEE} project funded by: + \begin{center} + \textcolor{blue}{NME through ICT from MHRD, Govt. of India}. + \end{center} + \end{block} +\end{frame} + +\end{document} diff -r 099a2cc6c7d2 -r 3893bac8e424 solving-equations.org --- a/solving-equations.org Fri Apr 16 16:16:13 2010 +0530 +++ b/solving-equations.org Sat Apr 17 12:50:42 2010 +0530 @@ -13,13 +13,17 @@ of polynomials and other non-linear equations. In the process, we shall look at defining functions. + We would be using concepts related to arrays which we have covered + in previous session + Let's begin with solving linear equations. {show a slide of the equations} We shall use the solve function, to solve this system of linear - equations. Solve requires the coefficients and the constants to + equations. Solve requires the coefficients and the constants to be in the form of matrices to solve the system of linear equations. - We begin by entering the coefficients and the constants as + Lets start ipython -pylab interpreter. + Then we begin by entering the coefficients and the constants as matrices. In []: A = array([[3,2,-1], @@ -33,8 +37,9 @@ Type x, to look at the solution obtained. - Next, we verify the solution by obtaining a product of A and x, - and comparing it with b. Note that we should use the dot function + Equation is of the form Ax = b, so we verify the solution by + obtaining a matrix product of A and x, and comparing it with b. + As we have covered earlier that we should use the dot function here, and not the * operator. In []: Ax = dot(A, x) @@ -60,22 +65,26 @@ In []: coeffs = [1, -5, 6] In []: roots(coeffs) - As you can see, roots returns the coefficients in an array. + As you can see, roots returns the result in an array. + # It even works for polynomials with imaginary solutions. + # roots([1, 1, 1]) - To find the roots of any arbitrary function, we use the fsolve + To find the roots of non linear equations, we use the fsolve function. We shall use the function sin(x)+cos^2(x) as our - function, in this tutorial. First, of all we import fsolve, since it - is not already available to us. + function, in this tutorial. This function is not part of pylab + package which we import during starting, so we will have to + import it. It is part of scipy package. In []: from scipy.optimize import fsolve - Now, let's look at the arguments of fsolve using fsolve? + Now, let's look at the documentation of fsolve using fsolve? In []: fsolve? - The first argument, func, is a python function that takes atleast - one argument. So, we should now define a python function for the - given mathematical expression sin(x)+cos^2(x). + As mentioned in docs the first argument, func, is a python + function that takes atleast one argument. So, we should now + define a python function for the given mathematical expression + sin(x)+cos^2(x). The second argument, x0, is the initial estimate of the roots of the function. Based on this initial guess, fsolve returns a root. @@ -86,16 +95,21 @@ given mathematical expression (sin(x)+cos^2(x)) for a each input. In []: def f(x): - return sin(x)+cos(x)*cos(x) + ... return sin(x)+cos(x)*cos(x) + ... + ... + hit return thrice for coming out of function definition. def, is a key word in python that tells the interpreter that a function definition is beginning. f, here, is the name of the function and x is the lone argument of the function. The whole - definition of the function is done with in an indented block. Our - function f has just one line in it's definition. + definition of the function is done with in an indented block same + as for loops and conditional statements we have used in our + earlier sessions. Our function f has just one line in it's + definition. You can test your function, by calling it with an argument for - which the output value is know, say x = 0. We can see that + which the output value is known, say x = 0. We can see that sin(x) + cos^2(x) has a value of 1, when x = 0. Let's check our function definition, by calling it with 0 as an @@ -111,8 +125,9 @@ In []: fsolve(f, 0) fsolve has returned a root of sin(x)+cos^2(x) that is close to 0. - That brings us to the end of this tutorial on solving linear - equations, finding roots of polynomials and other non-linear + That brings us to the end of this tutorial. We have covered solution + of linear equations, finding roots of polynomials and other + non-linear equations. We have also learnt how to define functions and call them.