Branches merged.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore Tue Mar 30 19:17:35 2010 +0530
@@ -0,0 +1,33 @@
+# use glob syntax.
+syntax: glob
+
+*.aux
+*.dvi
+*.log
+*.nav
+*.snm
+*.toc
+*.pdf
+*.vrb
+*.out
+*.sty
+*.pyc
+*.zip
+*~
+.project
+.pydevproject
+app.yaml
+build
+tests/coverageResults
+*,cover
+tests/.coverage
+*.git
+*.egg-info
+eggs
+parts
+.installed.cfg
+bin
+develop-eggs
+.gitignore
+.DS_Store
+index.yaml
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/arrays.org Tue Mar 30 19:17:35 2010 +0530
@@ -0,0 +1,165 @@
+* 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(number of elements) of new array must
+ be unchanged.
+
+ We check re-shaped 'a' by
+ In []: a
+
+ 'ones' function can be used to get an array with all the entries as
+ 1s. We pass it the shape of the required array. For ex.
+
+ In []: b = ones((3, 4))
+
+ b is 3(cross)4 array with all 1s
+ 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 function.
+ 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, lets 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(second row
+ third column), 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.
+
+ Additionally 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]
+ second column, from second row(1) till third(2) and excluding it
+ 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
--- a/basic-plot.txt Tue Mar 30 19:17:12 2010 +0530
+++ b/basic-plot.txt Tue Mar 30 19:17:35 2010 +0530
@@ -1,57 +1,149 @@
* Script
+**********
+Some greeting - Hi or Hello or Welcome - would be polite to start with
+**********
-In this tutorial, we will cover the basics of Plotting features available in Python. We shall use Ipython and pylab. Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion, help etc. pylab is python library which provides plotting functionality.
+Hello, in this tutorial, we will cover the basics of the Plotting features available in Python. We shall use Ipython and pylab. Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion, help etc. pylab is python library which provides plotting functionality.
-Lets start ipython. Open up your terminal and type the following.
+I am assuming that you have them installed on your system.
+
+Lets start IPython. Click Applications - Accessories - Terminal. The terminal window will open. Type the following command.
$ ipython -pylab
+press RETURN
This will give us a prompt where we can get started.
-First, we create an array with equally spaced points from 0 to 2*pi
+First, we create a sequence of numbers which are equally spaced starting from 0 till/to(?) 2*pi
+
+In []: x = lins<Tab> will auto complete the function. This is one of the feature of IPython.
+
In []: x = linspace(0, 2*pi, 100)
-We have passed three arguments to linspace function - the first point, the last point and the total number of points.
-lets see what is x
+To check or read documentation on 'linspace' function type
+
+In []: lins<Tab>pace?
+
+It shows documentation related to linspace function. 'help' talks in detail about arguments to be passed, return values, some examples on usage. (To scroll down the page use 'SPACE' key and to scroll up use 'b')To navigate through content use arrow(/Page Up and Page Down) keys. ':See Also' section hints about other related or similar functions which might be useful. To exit help (mode) press 'q'.
+
+In our case, we have passed three arguments to the linspace function - the starting point, the last point and the total number of points.
+Check value of x by
In []: x
- x is a sequence of 100 points starting from 0 to 2*pi.
+ x is a sequence of 100 points starting from 0 to 2*pi. Length of x can be seen via function
In []: len(x)
- Shows the length of x to be 100 points.
+which shows the length of x to be 100 points.
-To obtain the plot we say,
+To obtain the plot we say,
In []: plot(x, sin(x))
-
-It gives a plot of x vs y, where y is sin values of x, with the default color and line properties.
+***
+As you can see a plot has come on the screen.
+***
+A plot of x vs sin(x) appears on screen, with the default color and line properties.
Both 'pi' and 'sin' come from 'pylab'.
Now that we have a basic plot, we can label and title the plot.
-In []: xlabel('x') adds a label to the x-axis. Note that 'x' is enclosed in quotes.
-In []: ylabel('sin(x)') adds a label to the y-axis.
-In []: title('Sinusoid') adds a title to the plot.
+In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes.
+Similarly
+In []: ylabel('sin(x)') adds a label to the y-axis.
+To add a title to plot we simply use
+In []: tit<TAB>le('Sinusoid').
-Now we add a legend to the plot.
+Now we will add a legend to the plot.
In []: legend(['sin(x)'])
-We can specify the location of the legend, by passing an additional argument to the function.
+To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back.
+We can modify previous command to specify the location of the legend, by passing an additional argument to the function.
In []: legend(['sin(2y)'], loc = 'center')
-other positions which can be tried are
+other positions which can be tried are
'best'
'right'
-We now annotate, i.e add a comment at, the point with maximum sin value.
+We now annotate, i.e add a comment, at the point with maximum sin value.
In []: annotate('local max', xy=(1.5, 1))
The first argument is the comment and second one is the position for it.
-Now, we save the plot
-In []: savefig('sin.png') saves the figure as sin.png in current directory.
+Now, we save the plot as follows
+In []: savefig('sin.png') saves the figure as sin.png in the current directory.
+
+?#other supported formats are: eps, ps, pdf etc.
+
+When we use plot again by default plots get overlaid.
+In []: plot(x, cos(x))
+
+we update Y axis label
+In []: ylabel('f(x)')
+
+Now in these situations with overlaid graphs legend becomes absolutely essential. To add multiple legends, we pass the strings within quotes separated by commas and enclosed within square brackets as shown.
+
+In []: legend( [ 'sin(y)' , 'cos(y)'] )
+
+In []: clf()
+clears the plot area and start afresh.
+
+In case we want to create multiple plots rather than overlaid plots, we use 'figure' function.
+The figure command is used to open a plain figure window without any plot.
+In []: figure(1)
+
+plot() plot command plots a sin plot on figure(1)
+In []: plot(y, sin(y))
+
+to creates a new plain figure window without any plot.
+In []: figure(2)
+figure() also shifts the focus between multiple windows.
+
+Any command issued henceforth applies to this window only.
+In []: plot(x, cos(x))
+The previous plot window remains unchanged to these commands.
+
+In []: savefig('cosine.png')
+
+figure(1) shifts the focus back to figure(1).
+In []: figure(1)
-?#other supported formats are: eps, emf, ps, pdf, ps, raw, rgba, svg
+title() sets the title of figure(1)
+In []: title('sin(y)')
+
+Here we save the plot of figure(1).
+In []: savefig('sine.png')
+
+close() closes figure(1). Now there is just one figure that is open and hence
+the focus is automatically shifted to figure(2).
+In []: close()
+
+close() now closes the figure(2).
+In []: close()
+
+The plot command takes the following optional parameters such as 'r' which generates the plot in red color.
+Use up arrow key to get till this command
+In []: plot(x, cos(x), 'r') and add argument.
+
+# For other color options you may check out 'plot?'
+
+In []: clf()
+
+Passing the linewidth=2 option to plot, generates the plot with linewidth of two units.
+In []: plot(x, sin(x), 'g', linewidth=2)
+
+In []: clf()
+
+In order to plot points in black color you can pass 'k.' parameter to plot
+In []: plot(x, , 'k.')
+
+In []: clf()
+
+A plot using dashed lines can be generated by passing the '--' parameter
+In []: plot(x, y, '--')
+
+You may look at more options related to colors and type of lines using plot?
+
+In []: clf()
and finally to close the plot
In []: close()
-
****************
+This brings us to the end of this tutorial. This tutorial is first in the series of Python for Scientific Computing Tutorials.
+****************
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/least-squares.org Tue Mar 30 19:17:35 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 19:17:35 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 19:17:35 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 19:17:35 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?
--- a/plotting-files.txt Tue Mar 30 19:17:12 2010 +0530
+++ b/plotting-files.txt Tue Mar 30 19:17:35 2010 +0530
@@ -5,13 +5,10 @@
variables. Please make sure you have pendulum.txt file, we will
be using it for introduction of concepts(requirements page!).
-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.
+We shall look at inputting experimental 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?)
+The data could be input either as lists or read from plain text/
+binary files
# Before starting with this video, you should be comfortable with
# - Lists
@@ -26,7 +23,7 @@
Let's begin with inputting the data as lists and plotting it.
x = [0, 1, 2.1, 3.1, 4.2, 5.2]
-here x is a list. I python list is a container that holds number of
+here x is a list. In python list is a container that holds number of
objects. Various functions related to lists will be covered in more
detail later.
@@ -45,8 +42,7 @@
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
+experiment.
In []: cat pendulum.txt (windows?)
@@ -60,42 +56,60 @@
and square of the time-period.
l = []
-#len? (confusion over 1 and l(damm they are really same looking:P))
t = []
tsq = []
Now we open the file and read it line by line.
for line in open('pendulum.txt'):
+':' in end of for statement marks the start of block.
+
open returns a iterable object which we traverse using for loop. In
-python iterates over items of any sequence.
+python for iterates over items of any sequence.
#we will cover more of 'for' loop in later sections
line is a string variable storing one line at a time as for loop
iterates through file.
-We split each line at the space
+
+We split each line at the space using
+
point = line.split()
-mind the indentation here.
+split function will return list. In this case it will have two
+elements, first is length and second being time.
+
+mind the indentation here. Everything inside 'for' loop has to be
+indented by 4 spaces.
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])
-For any given list to access last element python provides '-1' index,
-so we use t[-1].
+ l.append(float(point[0]))
+append is function to append one element to the list.
+ t.append(float(point[1]))
+
+By this time we have time and length values in two lists. Now to get
+square of time values we will write one more for loop which this time
+iterate through list 't'
+
+for time in t:
+ tsq.append(time*time)
-Now the lists l, t have the required data. We can simply plot them, as
-we did already.
+# We also calculate the squares of the time-period and append to the
+# end of the tsq list.
+# tsq.append(t[-1]*t[-1])
+# For any given list to access last element python provides '-1'
+# index, so we use t[-1].
-plot(l, t, 'o')
+Now lists l(en) and tsq have the required data. We can simply plot
+them, as we did already.
-Enjoy!
+plot(l, tsq, 'o')
+Enjoy(Thank you)!
+
+For alternate ways of loading data from files go through tutorial on
+loadtxt
******************
-We should have two tutorials here, one should be basic, using for loops
-and lists
+We should have two tutorials here, one should be basic, using for
+loops and lists
Second one using loadtxt.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/solving-equations.org Tue Mar 30 19:17:35 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