Added scripts for session-4 and session-6 of day-1.
authorPuneeth Chaganti <punchagan@gmail.com>
Tue, 30 Mar 2010 14:53:58 +0530
changeset 2 008c0edc6eac
parent 1 f48921e39df1
child 3 093edb39f292
Added scripts for session-4 and session-6 of day-1.
arrays.org
least-squares.org
matrices.org
odes.org
plotting-files.org
solving-equations.org
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arrays.org	Tue Mar 30 14:53:58 2010 +0530
@@ -0,0 +1,161 @@
+* Arrays
+*** Outline
+***** Introduction
+******* Why do we want to do that?
+******* We shall use arrays (introduced before) for matrices
+******* Arsenal Required
+********* working knowledge of lists
+***** Initializing matrices
+******* Entering element-wise
+******* using special functions
+***** Accessing and Changing elements
+******* Slicing
+******* Striding
+***** {{Simple operations??}}
+*** Script
+    Welcome to the Tutorial on arrays. 
+
+    As mentioned in [the previous tutorial] arrays are much faster and
+    more efficient. In this tutorial we shall look at creating arrays,
+    accessing elements and changing them. 
+
+    ---
+
+    Let's start with creating simple arrays. We've already seen how to
+    convert lists to arrays. Entering an array is similar to that. 
+
+    a = array([5, 8, 10, 13])
+    
+    We enter a multi-dimensional array this way -
+    
+    In []: c = array([[11,12,13],
+                      [21,22,23],
+                      [31,32,33]])
+
+    To see what c is, we just type c in the prompt. 
+		      
+    In []: c
+
+    To see the dimensions of the array c, we use c.shape
+    In []: c.shape 
+
+    Now let us look at some special methods of creating an
+    array. There are various functions that allow us to create special
+    arrays. 
+
+    The first one we shall look at is, arange. arange is similar to
+    the range command, except that it returns an array and accepts
+    float arguments. [is range covered?]
+    
+    In []: a = arange(10)
+    
+    In []: a
+    This is the array we just created. 
+    
+    In []: a.shape
+    Note that a is one dimensional and has 10 elements, as expected. 
+
+    We could also use a.shape to change the shape of the array a. 
+    In []: a.shape = 2,5
+    Note that the total size of new array must be unchanged. 
+
+    We type a, to see what it looks like
+    In []: a
+
+    ones command can be used to get an array with all the entries as
+    1s. We pass it the shape of the array that we require. 
+    
+    In []: b = ones((3, 4))
+
+    Look at b, by printing it out. 
+    In []: b 
+
+    To create an array with all entries as ones, with a shape similar to
+    an already existing array, we use the ones_like command. 
+    In []: b = ones_like(a)
+
+    zeros and zeros_like are similar commands that can give you arrays
+    with all zeros. empty and empty_like give you empty arrays (arrays
+    with no initialization done.)
+
+    {Do an up arrow and quickly show them a couple of examples?}
+
+    The identity command can be used to obtain a square array with
+    ones on the main diagonal. 
+    
+    In []: identity(3)
+
+    To obtain a 2-D array, that is not necessarily square, eye command
+    can be used. Look at the documentation of eye (using eye?) for
+    more info. 
+
+    ---
+    
+    Now that we have learnt how to create arrays, let move on to
+    accessing elements and changing them. 
+    
+    Let's work with the c, array which we had already created. 
+
+    In []: c 
+
+    Let's say we want to access the element 23 in c, we say
+
+    In []: c[1][2]
+    Note that this is similar to accessing an element inside a list of
+    lists. Also, note that counting again starts from 0. 
+    
+    But arrays provide a more convenient way to access the elements. 
+    In []: c[1, 2]
+    
+    Now, we can also change the element using a simple assignment. 
+    In []: c[1, 2] = -23
+
+    Let's look at accessing more than one elements at a time. We begin
+    with accessing rows. 
+    In []: c[1] gives us the second row. (counting starts from 0)
+
+    To get a column, we use a syntax that is similar to the one used
+    to access a single element. 
+    In []: c[:,1], gives us the first column. 
+    
+    The colon is a way to tell python to get all elements in that
+    dimension from the array. 
+
+    So, we could use a more explicit way to access a row of a
+    In []: c[1,:]
+    
+    We could use the colon to access specific portions of an array. 
+    In []: c[1,1:2]
+    In []: c[1:2,1]
+    In []: c[1:2,1:2]
+    ...
+    [Oh, by the way this is termed as slicing. :)]
+    {How many examples should we show here?}
+
+    --- 
+    
+    You may have observed the similarity of the semi-colon notation to
+    the range command. As expected, the semi-colon notation also
+    provides a way to specify a jump. This {concept/idea} is termed as
+    Striding. 
+
+    To get every alternate row of c, starting from the first one, we say
+    In []: c[::2,:]
+
+    To get every alternate row of c, starting from the second one, we
+    say
+    In []: c[1::2,:]
+
+
+    In []: c[:,::2]
+    In []: c[::2,::2]
+    {How many examples should we show here?}
+
+    ---
+
+    We come to the end of this tutorial on arrays. In this tutorial,
+    you've learnt how to create arrays and access, change elements. 
+
+    Thank you. 
+
+*** Notes
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/least-squares.org	Tue Mar 30 14:53:58 2010 +0530
@@ -0,0 +1,102 @@
+* Least Squares Fit
+*** Outline
+***** Introduction
+******* What do we want to do? Why?
+********* What's a least square fit?
+********* Why is it useful?
+******* How are we doing it?
+******* Arsenal Required
+********* working knowledge of arrays
+********* plotting
+********* file reading
+***** Procedure
+******* The equation (for a single point)
+******* It's matrix form
+******* Getting the required matrices
+******* getting the solution
+******* plotting
+*** Script
+    Welcome. 
+    
+    In this tutorial we shall look at obtaining the least squares fit
+    of a given data-set. For this purpose, we shall use the same
+    pendulum data used in the tutorial on plotting from files.
+
+    To be able to follow this tutorial comfortably, you should have a
+    working knowledge of arrays, plotting and file reading. 
+
+    A least squares fit curve is the curve for which the sum of the
+    squares of it's distance from the given set of points is
+    minimum. We shall use the lstsq function to obtain the least
+    squares fit curve. 
+
+    In our example, we know that the length of the pendulum is
+    proportional to the square of the time-period. Therefore, we
+    expect the least squares fit curve to be a straight line. 
+
+    The equation of the line is of the form T^2 = mL+c. We have a set
+    of values for L and the corresponding T^2 values. Using this, we
+    wish to obtain the equation of the straight line. 
+
+    In matrix form...
+    {Show a slide here?}
+    
+    We have already seen (in a previous tutorial), how to read a file
+    and obtain the data set. We shall quickly get the required data
+    from our file. 
+
+    In []: l = []
+    In []: t = []
+    In []: for line in open('pendulum.txt'):
+    ....     point = line.split()
+    ....     l.append(float(point[0]))
+    ....     t.append(float(point[1]))
+    ....
+    ....
+
+    Since, we have learnt to use arrays and know that they are more
+    efficient, we shall use them. We convert the lists l and t to
+    arrays and calculate the values of time-period squared. 
+
+    In []: l = array(l)
+    In []: t = array(t)
+    In []: tsq = t*t
+
+    Now we shall obtain A, in the desired form using some simple array
+    manipulation 
+
+    In []: A = array([l, ones_like(l)])
+    In []: A = A.T
+    
+    Type A, to confirm that we have obtained the desired array. 
+    In []: A
+    Also note the shape of A. 
+    In []: A.shape
+
+    We shall now use the lstsq function, to obtain the coefficients m
+    and c. lstsq returns a lot of things along with these
+    coefficients. Look at the documentation of lstsq, for more
+    information. 
+    In []: result = lstsq(A,tsq)
+
+    We take put the required coefficients, which are the first thing
+    in the list of things that lstsq returns, into the variable coef. 
+    In []: coef = result[0]
+
+    To obtain the plot of the line, we simply use the equation of the
+    line, we have noted before. T^2 = mL + c. 
+
+    In []: Tline = coef[0]*l + coef[1]
+    In []: plot(l, Tline)
+
+    Also, it would be nice to have a plot of the points. So, 
+    In []: plot(l, tsq, 'o')
+
+    This brings us to the end of this tutorial. In this tutorial,
+    you've learnt how to obtain a least squares fit curve for a given
+    set of points. 
+
+    Hope you enjoyed it. Thanks. 
+
+*** Notes
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/matrices.org	Tue Mar 30 14:53:58 2010 +0530
@@ -0,0 +1,77 @@
+* Matrices
+*** Outline
+***** Introduction
+******* Why do we want to do that?
+******* We shall use arrays (introduced before) for matrices
+******* Arsenal Required
+********* working knowledge of arrays
+***** Various matrix operations
+******* Transpose
+******* Sum of all elements
+******* Element wise operations
+******* Matrix multiplication
+******* Inverse of a matrix
+******* Determinant
+******* eigen values/vectors
+******* svd
+***** Other things available?
+*** Script
+    Welcome. 
+    
+    In this tutorial, you will learn how to perform some common matrix
+    operations. We shall look at some of the functions available in
+    pylab. Note that, this tutorial just scratches the surface and
+    there is a lot more that can be done. 
+
+    Let's begin with finding the transpose of a matrix. 
+    
+    In []: a = array([[ 1,  1,  2, -1],
+    ...:            [ 2,  5, -1, -9],
+    ...:            [ 2,  1, -1,  3],
+    ...:            [ 1, -3,  2,  7]])
+
+    In []: a.T
+
+    Type a, to observe the change in a. 
+    In []: a
+    
+    Now we shall look at adding another matrix b, to a. It doesn't
+    require anything special, just use the + operator. 
+    
+    In []: b = array([[3, 2, -1, 5],
+                      [2, -2, 4, 9],
+                      [-1, 0.5, -1, -7],
+                      [9, -5, 7, 3]])
+    In []: a + b
+
+    What do you expect would be the result, if we used * instead of
+    the + operator? 
+
+    In []: a*b
+    
+    You get an element-wise product of the two arrays and not a matrix
+    product. To get a matrix product, we use the dot function. 
+    
+    In []: dot(a, b)
+
+    The sum function returns the sum of all the elements of the
+    array. 
+    
+    In []: sum(a)
+
+    The inv command returns the inverse of the matrix. 
+    In []: inv(a)
+
+    In []: det(a)
+
+    In []: eig(a)
+    Returns the eigenvalues and the eigen vectors. 
+    
+    In []: eigvals(a)
+    Returns only the eigenvalues. 
+
+    In []: svd(a)
+    Singular Value Decomposition 
+
+*** Notes
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/odes.org	Tue Mar 30 14:53:58 2010 +0530
@@ -0,0 +1,10 @@
+* Solving ODEs
+*** Outline
+***** Introduction
+******* What are we going to do?
+******* How are we going to do?
+******* Arsenal Required
+********* working knowledge of arrays
+********* working knowledge of functions
+*** Script
+*** Notes
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plotting-files.org	Tue Mar 30 14:53:58 2010 +0530
@@ -0,0 +1,109 @@
+* Plotting Experimental Data
+*** Outline
+***** Introduction
+******* Why do we want to do that?
+******* Inputting data
+********* Lists
+********* Files
+******* Arsenal Required
+********* Lists
+*********** initializing
+*********** appending to lists
+********* for loops
+*********** iterating over a list
+********* basic plotting
+***** Inputting data as lists
+******* Input the dependent and independent variables in two lists
+******* Plot the points
+***** Plotting from files
+******* Look at the file
+******* Read the file and get data into variables
+******* Do any massaging required
+******* Plot the points
+
+*** Script
+
+This video will teach you how to plot experimental data, with two
+variables. 
+
+In general, we don't plot (analytical) functions. We often have
+experimental data points, that we wish to plot. We shall look at
+inputting this data and plotting it. 
+
+The data could be input (or entered) in two formats. For smaller data
+sets we could use lists to input the data and use plain text files for
+(somewhat?) larger ones. (Binary files?)
+
+[[[Before starting with this video, you should be comfortable with
+  - Lists
+    - initializing them
+    - appending elements to lists
+  - for command
+    - iterating over a list
+  - split command
+  - plot command
+    - plotting two variables
+]]]
+
+Let's begin with inputting the data as lists and plotting it. 
+
+x = [0, 1, 2.1, 3.1, 4.2, 5.2]
+y = [0, 0.8, 0.9, 0, -0.9, -0.8]
+Now we have two lists x and y, with the values that we wish to plot. 
+
+We say:
+plot (x, y, 'o')
+
+and there, we have our plot!
+
+[We close the plot window. ]
+
+Now, that we know how to plot data which is in lists, we will look at
+plotting data which is in a text file. Essentially, we read the data
+from the file and massage it into lists again. Then we can easily
+plot it, as we already did. 
+
+As an example we will use the data collected from a simple pendulum
+experiment. We have the data of, the length of pendulum vs. the time
+period of the pendulum in the file pendulum.txt
+
+In []: cat pendulum.txt (windows?)
+
+The cat command, shows the contents of the file. 
+
+The first column is the length of the pendulum and the second column
+is the time. We read the file line-by-line, collect the data into
+lists and plot them. 
+
+We begin with initializing three empty lists for length, time-period
+and square of the time-period. 
+
+l = []
+t = []
+tsq = []
+
+Now we open the file and read it line by line. 
+for line in open('pendulum.txt'):
+
+We split each line at the space 
+    point = line.split()
+
+Then we append the length and time values to the corresponding
+lists. Note that they are converted from strings to floats, before
+appending to the lists
+    l.append(float(point[0])
+    t.append(float(point[1])
+We also calculate the squares of the time-period and append to the end
+of the tsq list. 
+    tsq.append(t[-1]*t[-1])
+As you might be aware, t[-1] gives the last element of the list t. 
+
+Now the lists l, t have the required data. We can simply plot them, as
+we did already. 
+
+plot(l, t, 'o')
+
+Enjoy!
+
+*** Notes
+    - Also put in code snippets?
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/solving-equations.org	Tue Mar 30 14:53:58 2010 +0530
@@ -0,0 +1,121 @@
+* Solving Equations
+*** Outline
+***** Introduction
+******* What are we going to do?
+******* How are we going to do?
+******* Arsenal Required
+********* working knowledge of arrays
+
+*** Script
+    Welcome. 
+    
+    In this tutorial we shall look at solving linear equations, roots
+    of polynomials and other non-linear equations. In the process, we
+    shall look at defining functions. 
+
+    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
+    be in the form of matrices to solve the system of linear equations. 
+
+    We begin by entering the coefficients and the constants as
+    matrices. 
+
+    In []: A = array([[3,2,-1],
+                      [2,-2,4],                   
+                      [-1, 0.5, -1]])
+    In []: b = array([1, -2, 0])
+
+    Now, we can use the solve function to solve the given system. 
+    
+    In []: x = solve(A, b)
+
+    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
+    here, and not the * operator. 
+
+    In []: Ax = dot(A, x)
+    In []: Ax
+
+    The result Ax, doesn't look exactly like b, but if you carefully
+    observe, you will see that it is the same as b. To save yourself
+    this trouble, you can use the allclose function. 
+
+    allclose checks if two matrices are close enough to each other
+    (with-in the specified tolerance level). Here we shall use the
+    default tolerance level of the function. 
+
+    In []: allclose(Ax, b)
+    The function returns True, which implies that the product of A &
+    x, and b are close enough. This validates our solution x. 
+
+    Let's move to finding the roots of polynomials. We shall use the
+    roots function to calculate the roots of the polynomial x^2-5x+6. 
+
+    The function requires an array of the coefficients of the
+    polynomial in the descending order of powers. 
+    
+    In []: coeffs = [1, -5, 6]
+    In []: roots(coeffs)
+    As you can see, roots returns the coefficients in an array. 
+
+    To find the roots of any arbitrary function, 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. 
+
+    In []: from scipy.optimize import fsolve
+
+    Now, let's look at the arguments 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). 
+
+    The second argument, x0, is the initial estimate of the roots of
+    the function. Based on this initial guess, fsolve returns a root. 
+
+    Before, going ahead to get a root of the given expression, we
+    shall first learn how to define a function in python. 
+    Let's define a function called f, which returns values of the
+    given mathematical expression (sin(x)+cos^2(x)) for a each input. 
+
+    In []: def f(x):
+               return sin(x)+cos(x)*cos(x)
+   
+    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. 
+
+    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
+    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
+    argument. 
+    In []: f(0)
+    We can see that the output is as expected. 
+
+    Now, that we have our function, we can use fsolve to obtain a root
+    of the expression sin(x)+cos^2(x). Recall that fsolve takes
+    another argument, the initial guess. Let's use 0 as our initial
+    guess. 
+
+    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
+    equations. We have also learnt how to define functions and call
+    them. 
+
+    Thank you!
+
+*** Notes