Branches merged.
--- a/least-squares.org Sat Apr 17 12:57:03 2010 +0530
+++ b/least-squares.org Mon Apr 19 10:55:55 2010 +0530
@@ -21,26 +21,34 @@
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 that we used in the tutorial on plotting from files.
- Hope you have the file with you.
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.
+ minimum.
+
+ Previously, when we plotted the data from pendulum.txt we got a
+ scatter plot of points as shown.
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.
+ proportional to the square of the time-period. But when we plot
+ the data using lines we get a distorted line as shown. What
+ we expect ideally, is something like the redline in this graph.
+ From the problem we know that L is directly proportional to T^2.
+ But experimental data invariably contains errors and hence does
+ not produce an ideal plot. The best fit curve for this data has
+ to be a linear curve and this can be obtained by performing least
+ square fit on the data set. We shall use the lstsq function to
+ obtain the least squares fit curve.
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 the equation is represented as shown,
- Tsq = A.p where Tsq is an NX1 matrix, and A is an NX2 as shown.
+ Tsq = A.p where Tsq is an NX1 matrix, and A is an NX2 matrix as shown.
And p is a 2X1 matrix of the slope and Y-intercept. In order to
obtain the least square fit curve we need to find the matrix p
@@ -65,9 +73,14 @@
A = array([l, ones_like(l)])
As we have seen in a previous tutorial, ones_like() gives an array similar
- in shape to the given array, in this case l, with all the elements as 1.
+ in shape to the given array, in this case l, with all the elements as 1.
+ Please note, this is how we create an array from an existing array.
- A = A.T to get the transpose of the given array.
+ Let's now look at the shape of A.
+ A.shape
+ This is an 2X90 matrix. But we need a 90X2 matrix, so we shall transpose it.
+
+ A = A.T
Type A, to confirm that we have obtained the desired array.
A
@@ -88,14 +101,15 @@
line, we have noted before. T^2 = mL + c.
Tline = coef[0]*l + coef[1]
- plot(l, Tline)
+ plot(l, Tline, 'r')
Also, it would be nice to have a plot of the points. So,
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.
+ set of points using lstsq. There are other curve fitting functions
+ available in Pylab such as polyfit.
Hope you enjoyed it. Thanks.
--- a/solving-equations.org Sat Apr 17 12:57:03 2010 +0530
+++ b/solving-equations.org Mon Apr 19 10:55:55 2010 +0530
@@ -9,26 +9,31 @@
*** 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.
+ In this tutorial we shall look at solving linear equations, obtaining
+ roots of polynomial and non-linear equations. In the process, we
+ shall look at defining functions as well.
We would be using concepts related to arrays which we have covered
- in previous session
+ in a previous tutorial.
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
+ Consider the set of equations,
+ 3x + 2y -z = 1, 2x-2y + 4z = -2, -x+ half y-z = 0.
+ We shall use the solve function, to solve the given 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.
+ be in the form of matrices of the form Ax = b to solve the system of linear equations.
Lets start ipython -pylab interpreter.
- Then we begin by entering the coefficients and the constants as
+ We begin by entering the coefficients and the constants as
matrices.
- In []: A = array([[3,2,-1],
- [2,-2,4],
+ In []: A = array([[3,2,-1],
+ [2,-2,4],
[-1, 0.5, -1]])
+
+ A is a 3X3 matrix of the coefficients of x, y and z
+
In []: b = array([1, -2, 0])
Now, we can use the solve function to solve the given system.
@@ -45,9 +50,9 @@
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.
+ The result Ax, doesn't look exactly like b, but if we carefully
+ observe, we will see that it is the same as b. To save ourself
+ all this trouble, we 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
@@ -55,33 +60,35 @@
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.
+ x is very close to the value of b. 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.
+ Let's move to finding the roots of a polynomial. We shall use the
+ roots function for this.
The function requires an array of the coefficients of the
polynomial in the descending order of powers.
+ Consider the polynomial x^2-5x+6 = 0
In []: coeffs = [1, -5, 6]
In []: roots(coeffs)
- As you can see, roots returns the result in an array.
- # It even works for polynomials with imaginary solutions.
- # roots([1, 1, 1])
+ As we can see, roots returns the result in an array.
+ It even works for polynomials with imaginary roots.
+ roots([1, 1, 1])
+ As you can see, the roots of that equation are of the form a + bj
- 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. 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.
+ What if I want the solution of non linear equations?
+ For that we use the fsolve function. In this tutorial, we shall use
+ the equation sin(x)+cos^2(x). fsolve is not part of the pylab
+ package which we imported at the beginning, so we will have to import
+ it. It is part of scipy package. Let's import it using.
In []: from scipy.optimize import fsolve
- Now, let's look at the documentation of fsolve using fsolve?
+ Now, let's look at the documentation of fsolve by typing fsolve?
In []: fsolve?
- As mentioned in docs the first argument, func, is a python
+ As mentioned in documentation 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).
@@ -98,17 +105,17 @@
... return sin(x)+cos(x)*cos(x)
...
...
- hit return thrice for coming out of function definition.
+ hit the enter key to come 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 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 of the function is done with in an indented block similar
+ to the loops and conditional statements we have used in our
+ earlier tutorials. Our function f has just one line in it's
definition.
- You can test your function, by calling it with an argument for
+ We can test our function, by calling it with an argument for
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.
@@ -126,8 +133,7 @@
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. We have covered solution
- of linear equations, finding roots of polynomials and other
- non-linear
+ of linear equations, finding roots of polynomials and non-linear
equations. We have also learnt how to define functions and call
them.