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