Difference between revisions of "Python/C3/Least-square-fit/English-timed"
From Script | Spoken-Tutorial
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | + | |'''Time''' | |
− | + | |'''Narration''' | |
+ | |||
|- | |- | ||
− | | | + | | 00:01 |
| Hello friends and welcome to the tutorial on 'Least Square Fit'. | | Hello friends and welcome to the tutorial on 'Least Square Fit'. | ||
|- | |- | ||
− | | | + | | 00:05 |
| At the end of this tutorial, you will be able to, | | At the end of this tutorial, you will be able to, | ||
Line 13: | Line 14: | ||
|- | |- | ||
− | | | + | | 00:11 |
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Loading data from files" and "Getting started with arrays". | | Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Loading data from files" and "Getting started with arrays". | ||
|- | |- | ||
− | | | + | | 00:22 |
| Let us start this tutorial with the help of an example. | | Let us start this tutorial with the help of an example. | ||
|- | |- | ||
− | | | + | | 00:27 |
| Generate a least square fit line for l versus t square using the data in the file 'pendulum.txt'. | | Generate a least square fit line for l versus t square using the data in the file 'pendulum.txt'. | ||
|- | |- | ||
− | | | + | | 00:36 |
| We have an input file generated from a simple pendulum experiment. | | We have an input file generated from a simple pendulum experiment. | ||
|- | |- | ||
− | | | + | | 00:40 |
| It contains two columns of data. | | It contains two columns of data. | ||
|- | |- | ||
− | | | + | |00:43 |
| The first column is the length of the pendulum and the second is the corresponding time series of the pendulum. | | The first column is the length of the pendulum and the second is the corresponding time series of the pendulum. | ||
|- | |- | ||
− | | | + | | 00:48 |
| As we know, the square of time period of a pendulum is directly proportional to its length, | | As we know, the square of time period of a pendulum is directly proportional to its length, | ||
|- | |- | ||
− | | | + | |00:54 |
| we shall plot l versus t square and verify this. | | we shall plot l versus t square and verify this. | ||
|- | |- | ||
− | | | + | |00:58 |
| To read the file input and parse the data, we are going to use the loadtxt function. | | To read the file input and parse the data, we are going to use the loadtxt function. | ||
|- | |- | ||
− | | | + | |01:04 |
| Switch to the terminal. | | Switch to the terminal. | ||
|- | |- | ||
− | | | + | |01:08 |
| We can see that l and t are two sequences containing length and time values correspondingly. | | We can see that l and t are two sequences containing length and time values correspondingly. | ||
|- | |- | ||
− | | | + | |01:15 |
| Let us first plot l versus t square. | | Let us first plot l versus t square. | ||
|- | |- | ||
− | | | + | |01:18 |
|Type in the terminal ipython hypen pylab | |Type in the terminal ipython hypen pylab | ||
|- | |- | ||
− | | | + | |01:27 |
|Type l comma t = loadtxt within brackets in double quotes slash home slash fossee slash pendulum.txt comma unpack=True then bracket closed. | |Type l comma t = loadtxt within brackets in double quotes slash home slash fossee slash pendulum.txt comma unpack=True then bracket closed. | ||
Then type | Then type | ||
Line 73: | Line 74: | ||
|- | |- | ||
− | | | + | |02:05 |
| We can see that there is a visible linear trend, but we do not get a straight line connecting them. | | We can see that there is a visible linear trend, but we do not get a straight line connecting them. | ||
|- | |- | ||
− | | | + | |02:11 |
| We shall, therefore, generate a least square fit line. | | We shall, therefore, generate a least square fit line. | ||
|- | |- | ||
− | | | + | | 02:15 |
| We will first generate the two matrices tsq and A. | | We will first generate the two matrices tsq and A. | ||
|- | |- | ||
− | | | + | | 02:22 |
| Then we will use the lstsq function to find the values of m and c. | | Then we will use the lstsq function to find the values of m and c. | ||
|- | |- | ||
− | | | + | |02:32 |
|Type tsq = t star t hit enter | |Type tsq = t star t hit enter | ||
Type plot within brackets l comma tsq comma within single quotes bo hit enter | Type plot within brackets l comma tsq comma within single quotes bo hit enter | ||
|- | |- | ||
− | | | + | | 02:57 |
| let us now generate the A matrix with l values. | | let us now generate the A matrix with l values. | ||
|- | |- | ||
− | | | + | | 03:01 |
| We shall first generate a 2 into 90 matrix with the first row as l values and the second row as ones. | | We shall first generate a 2 into 90 matrix with the first row as l values and the second row as ones. | ||
|- | |- | ||
− | | | + | |03:13 |
| Then take the transpose of it. | | Then take the transpose of it. | ||
|- | |- | ||
− | | | + | |03:16 |
|Type in the terminal inter underscore mat = array within brackets l comma ones underscore like within brackets one then closing bracket | |Type in the terminal inter underscore mat = array within brackets l comma ones underscore like within brackets one then closing bracket | ||
|- | |- | ||
− | | | + | |03:44 |
|Now we can see an error. | |Now we can see an error. | ||
|- | |- | ||
− | | | + | |03:47 |
|The error is in the one underscore like function we typed one instead of l so you have to put l there. | |The error is in the one underscore like function we typed one instead of l so you have to put l there. | ||
|- | |- | ||
− | | | + | | 03:59 |
| We can see that we have intermediate matrix. | | We can see that we have intermediate matrix. | ||
|- | |- | ||
− | | | + | | 04:02 |
| Now we need the transpose. | | Now we need the transpose. | ||
|- | |- | ||
− | | | + | |04:04 |
|So type on the terminal | |So type on the terminal | ||
A = inter underscore mat.T | A = inter underscore mat.T | ||
|- | |- | ||
− | | | + | |04:19 |
|Type A and hit enter | |Type A and hit enter | ||
|- | |- | ||
− | | | + | | 04:22 |
| Now we have both the matrices A and tsq. | | Now we have both the matrices A and tsq. | ||
|- | |- | ||
− | | | + | | 04:27 |
| We only need to use the lstsq . | | We only need to use the lstsq . | ||
|- | |- | ||
− | | | + | |04:33 |
|Type result = lstsq within brackets A comma tsq where lstsq stands for least square | |Type result = lstsq within brackets A comma tsq where lstsq stands for least square | ||
|- | |- | ||
− | | | + | | 04:49 |
| The result is a sequence of values. | | The result is a sequence of values. | ||
|- | |- | ||
− | | | + | | 04:51 |
| The first item in this sequence, is the matrix p i.e., the values of m and c. | | The first item in this sequence, is the matrix p i.e., the values of m and c. | ||
|- | |- | ||
− | | | + | |04:58 |
|Type m comma c = result within square brackets 0 .Then type | |Type m comma c = result within square brackets 0 .Then type | ||
m | m | ||
Line 162: | Line 163: | ||
|- | |- | ||
− | | | + | | 05:20 |
| Now that we have m and c,So we need to generate the fitted values of t square. | | Now that we have m and c,So we need to generate the fitted values of t square. | ||
|- | |- | ||
− | | | + | |05:28 |
|Type on terminal. | |Type on terminal. | ||
tsq underscore fit = m star l plus c | tsq underscore fit = m star l plus c | ||
Line 172: | Line 173: | ||
|- | |- | ||
− | | | + | |06:06 |
|Before typing that plot we have to type clear function. | |Before typing that plot we have to type clear function. | ||
Type plot within brackets l comma tsq comma bo. | Type plot within brackets l comma tsq comma bo. | ||
|- | |- | ||
− | | | + | |06:24 |
|Now i can see the plot | |Now i can see the plot | ||
Type plot within brackets l comma tsq underscore fit comma r | Type plot within brackets l comma tsq underscore fit comma r | ||
|- | |- | ||
− | | | + | | 06:41 |
| We get the least square fit of l versus t square. | | We get the least square fit of l versus t square. | ||
|- | |- | ||
− | | | + | | 06:49 |
| This brings us to the end of this tutorial. | | This brings us to the end of this tutorial. | ||
|- | |- | ||
− | | | + | | 06:52 |
| In this tutorial, we have learnt to, 1. Generate a least square fit using matrices. | | In this tutorial, we have learnt to, 1. Generate a least square fit using matrices. | ||
|- | |- | ||
− | | | + | | 06:57 |
| 2. Use the function lstsq() to generate a least square fit line. | | 2. Use the function lstsq() to generate a least square fit line. | ||
|- | |- | ||
− | | | + | | 07:03 |
| Here are some self assessment questions for you to solve | | Here are some self assessment questions for you to solve | ||
|- | |- | ||
− | | | + | | 07:07 |
| 1. What does ones underscore like 1 comma 2 comma 3 produce | | 1. What does ones underscore like 1 comma 2 comma 3 produce | ||
array 1 comma 1 comma 1 | array 1 comma 1 comma 1 | ||
Line 210: | Line 211: | ||
|- | |- | ||
− | | | + | | 07:25 |
| 2. The plot of u versus v is a bunch of scattered points that show a linear trend. | | 2. The plot of u versus v is a bunch of scattered points that show a linear trend. | ||
|- | |- | ||
− | | | + | | 07:33 |
| How do you find the least square fit line of u versus v . | | How do you find the least square fit line of u versus v . | ||
|- | |- | ||
− | | | + | | 07:38 |
| And will look at the answers, | | And will look at the answers, | ||
|- | |- | ||
− | | | + | | 07:42 |
| 1. The function ones underscore like in 1 comma 2 comma 3 will generate array 1 comma 1 comma 1. | | 1. The function ones underscore like in 1 comma 2 comma 3 will generate array 1 comma 1 comma 1. | ||
So that is the first questions answer. | So that is the first questions answer. | ||
|- | |- | ||
− | | | + | | 07:54 |
| 2. The following set of commands will produce the least square fit line of u versus v | | 2. The following set of commands will produce the least square fit line of u versus v | ||
Line 236: | Line 237: | ||
|- | |- | ||
− | | | + | | 08:34 |
| Hope you have enjoyed this tutorial and found it useful. | | Hope you have enjoyed this tutorial and found it useful. | ||
|- | |- | ||
− | | | + | |08:37 |
|Thank you! | |Thank you! | ||
|} | |} |
Revision as of 14:52, 10 July 2014
Time | Narration |
00:01 | Hello friends and welcome to the tutorial on 'Least Square Fit'. |
00:05 | At the end of this tutorial, you will be able to,
|
00:11 | Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Loading data from files" and "Getting started with arrays". |
00:22 | Let us start this tutorial with the help of an example. |
00:27 | Generate a least square fit line for l versus t square using the data in the file 'pendulum.txt'. |
00:36 | We have an input file generated from a simple pendulum experiment. |
00:40 | It contains two columns of data. |
00:43 | The first column is the length of the pendulum and the second is the corresponding time series of the pendulum. |
00:48 | As we know, the square of time period of a pendulum is directly proportional to its length, |
00:54 | we shall plot l versus t square and verify this. |
00:58 | To read the file input and parse the data, we are going to use the loadtxt function. |
01:04 | Switch to the terminal. |
01:08 | We can see that l and t are two sequences containing length and time values correspondingly. |
01:15 | Let us first plot l versus t square. |
01:18 | Type in the terminal ipython hypen pylab |
01:27 | Type l comma t = loadtxt within brackets in double quotes slash home slash fossee slash pendulum.txt comma unpack=True then bracket closed.
Then type l Secondly type t |
02:05 | We can see that there is a visible linear trend, but we do not get a straight line connecting them. |
02:11 | We shall, therefore, generate a least square fit line. |
02:15 | We will first generate the two matrices tsq and A. |
02:22 | Then we will use the lstsq function to find the values of m and c. |
02:32 | Type tsq = t star t hit enter
Type plot within brackets l comma tsq comma within single quotes bo hit enter |
02:57 | let us now generate the A matrix with l values. |
03:01 | We shall first generate a 2 into 90 matrix with the first row as l values and the second row as ones. |
03:13 | Then take the transpose of it. |
03:16 | Type in the terminal inter underscore mat = array within brackets l comma ones underscore like within brackets one then closing bracket |
03:44 | Now we can see an error. |
03:47 | The error is in the one underscore like function we typed one instead of l so you have to put l there. |
03:59 | We can see that we have intermediate matrix. |
04:02 | Now we need the transpose. |
04:04 | So type on the terminal
A = inter underscore mat.T |
04:19 | Type A and hit enter |
04:22 | Now we have both the matrices A and tsq. |
04:27 | We only need to use the lstsq . |
04:33 | Type result = lstsq within brackets A comma tsq where lstsq stands for least square |
04:49 | The result is a sequence of values. |
04:51 | The first item in this sequence, is the matrix p i.e., the values of m and c. |
04:58 | Type m comma c = result within square brackets 0 .Then type
m Then type c so you can see the output. |
05:20 | Now that we have m and c,So we need to generate the fitted values of t square. |
05:28 | Type on terminal.
tsq underscore fit = m star l plus c Type plot within brackets l comma tsq comma within single quote bo |
06:06 | Before typing that plot we have to type clear function.
Type plot within brackets l comma tsq comma bo. |
06:24 | Now i can see the plot
Type plot within brackets l comma tsq underscore fit comma r |
06:41 | We get the least square fit of l versus t square. |
06:49 | This brings us to the end of this tutorial. |
06:52 | In this tutorial, we have learnt to, 1. Generate a least square fit using matrices. |
06:57 | 2. Use the function lstsq() to generate a least square fit line. |
07:03 | Here are some self assessment questions for you to solve |
07:07 | 1. What does ones underscore like 1 comma 2 comma 3 produce
array 1 comma 1 comma 1 1 comma 1 comma 1 in square brackets. then square brackets 1 point 0 comma 1 point 0 comma 1 point 0 Error |
07:25 | 2. The plot of u versus v is a bunch of scattered points that show a linear trend. |
07:33 | How do you find the least square fit line of u versus v . |
07:38 | And will look at the answers, |
07:42 | 1. The function ones underscore like in 1 comma 2 comma 3 will generate array 1 comma 1 comma 1.
So that is the first questions answer. |
07:54 | 2. The following set of commands will produce the least square fit line of u versus v
A = array within brackets u comma ones underscore like within brackets u.T result = lstsq within brackets A comma v m comma c = result within square brackets 0 lst underscore line = m star u plus c |
08:34 | Hope you have enjoyed this tutorial and found it useful. |
08:37 | Thank you! |