<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FLeast-square-fit%2FEnglish</id>
		<title>Python/C3/Least-square-fit/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FLeast-square-fit%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Least-square-fit/English&amp;action=history"/>
		<updated>2026-05-13T02:03:05Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C3/Least-square-fit/English&amp;diff=944&amp;oldid=prev</id>
		<title>Pravin1389: moved Python/C3/Least-square-fit /English to Python/C3/Least-square-fit/English</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Least-square-fit/English&amp;diff=944&amp;oldid=prev"/>
				<updated>2012-12-02T07:18:27Z</updated>
		
		<summary type="html">&lt;p&gt;moved &lt;a href=&quot;/index.php/Python/C3/Least-square-fit_/English&quot; class=&quot;mw-redirect&quot; title=&quot;Python/C3/Least-square-fit /English&quot;&gt;Python/C3/Least-square-fit /English&lt;/a&gt; to &lt;a href=&quot;/index.php/Python/C3/Least-square-fit/English&quot; title=&quot;Python/C3/Least-square-fit/English&quot;&gt;Python/C3/Least-square-fit/English&lt;/a&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 07:18, 2 December 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan='2' style='text-align: center;'&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Pravin1389</name></author>	</entry>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C3/Least-square-fit/English&amp;diff=487&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and welcome to the tutorial on '…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Least-square-fit/English&amp;diff=487&amp;oldid=prev"/>
				<updated>2012-11-29T06:11:14Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and welcome to the tutorial on &amp;#039;…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello friends and welcome to the tutorial on 'Least Square Fit'.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Generate the least square fit line for a given set of points.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Pre-requisite slide &lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;Using plot interactively&amp;quot;, &amp;quot;Loading data from files&amp;quot; and &amp;quot;Getting started with arrays&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Let us start this tutorial with the help of an example.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4&lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| Generate a least square fit line for l v/s t^2 using the data in the file 'pendulum.txt'.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open the file 'pendulum.txt' and show &lt;br /&gt;
| We have an input file generated from a simple pendulum experiment.&lt;br /&gt;
&lt;br /&gt;
It contains two columns of data. The first column is the length of the pendulum and the second is the corresponding time period of the pendulum.&lt;br /&gt;
&lt;br /&gt;
As we know, the square of time period of a pendulum is directly proportional to its length, we shall plot l vs t^2 and verify this.&lt;br /&gt;
&lt;br /&gt;
To read the input file and parse the data, we are going to use the loadtxt function. Switch to the terminal.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  ipython -pylab&lt;br /&gt;
 l, t = loadtxt(&amp;quot;/home/fossee/pendulum.txt&amp;quot;, unpack=True)&lt;br /&gt;
 l&lt;br /&gt;
 t&lt;br /&gt;
| We can see that l and t are two sequences containing length and time values correspondingly.&lt;br /&gt;
&lt;br /&gt;
Let us first plot l vs t^2.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  tsq = t * t&lt;br /&gt;
 plot(l, tsq, 'bo')&lt;br /&gt;
&lt;br /&gt;
Switch to the plot window &lt;br /&gt;
| We can see that there is a visible linear trend, but we do not get a straight line connecting them. We shall, therefore, generate a least square fit line.&lt;br /&gt;
&lt;br /&gt;
We will first generate the two matrices tsq and A. Then we will use the &amp;lt;tt&amp;gt;lstsq&amp;lt;/tt&amp;gt; function to find the values of m and c.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 inter_mat = array((l, ones_like(l)))&lt;br /&gt;
 inter_mat&lt;br /&gt;
| let us now generate the A matrix with l values. We shall first generate a 2 x 90 matrix with the first row as l values and the second row as ones. Then take the transpose of it. Type&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  A = inter_mat.T&lt;br /&gt;
 A&lt;br /&gt;
| We see that we have intermediate matrix. Now we need the transpose. Type&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  result = lstsq(A, tsq)&lt;br /&gt;
| Now we have both the matrices A and tsq. We only need to use the &amp;lt;tt&amp;gt;lstsq&amp;lt;/tt&amp;gt; Type&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;m, c = result[0]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 m&lt;br /&gt;
 c&lt;br /&gt;
| The result is a sequence of values. The first item in this sequence, is the matrix p i.e., the values of m and c.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  tsq_fit = m * l + c&lt;br /&gt;
 plot(l, tsq, 'bo')&lt;br /&gt;
 plot(l, tsq_fit, 'r')&lt;br /&gt;
| Now that we have m and c, we need to generate the fitted values of t^2. Type&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| We get the least square fit of l vs t^2.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5&lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of the tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Generate a least square fit using matrices.&lt;br /&gt;
# Use the function &amp;lt;tt&amp;gt;lstsq()&amp;lt;/tt&amp;gt; to generate a least square fit line.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;nowiki&amp;gt;What does ones_like([1, 2, 3]) produce&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
** &amp;lt;nowiki&amp;gt;array([1, 1, 1])&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
** &amp;lt;nowiki&amp;gt;[1, 1, 1]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
** &amp;lt;nowiki&amp;gt;[1.0, 1.0, 1.0]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
** Error&lt;br /&gt;
# The plot of &amp;lt;tt&amp;gt;u&amp;lt;/tt&amp;gt; vs &amp;lt;tt&amp;gt;v&amp;lt;/tt&amp;gt; is a bunch of scattered points that show a linear trend. How do you find the least square fit line of &amp;lt;tt&amp;gt;u&amp;lt;/tt&amp;gt; vs &amp;lt;tt&amp;gt;v&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# The function &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;ones_like([1, 2, 3])&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&amp;lt;nowiki&amp;gt; will generate 'array([1, 1, 1])'.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
# The following set of commands will produce the least square fit line of &amp;lt;tt&amp;gt;u&amp;lt;/tt&amp;gt; vs &amp;lt;tt&amp;gt;v&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enumerated list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
 A = array(u, ones_like(u)).T&lt;br /&gt;
 result = lstsq(A, v)&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;m, c = result[0]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 lst_line = m * u + c&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>