Python/C3/Least-square-fit/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
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,

Generate the least square fit line for a given set of points.

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. 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!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha