{"id":980,"date":"2010-11-15T16:03:27","date_gmt":"2010-11-15T16:03:27","guid":{"rendered":"http:\/\/pchero21.com\/?p=980"},"modified":"2010-11-15T16:03:27","modified_gmt":"2010-11-15T16:03:27","slug":"lec-6-mit-6-00-introduction-to-computer-science-and-programming-fall-2008","status":"publish","type":"post","link":"http:\/\/pchero21.com\/?p=980","title":{"rendered":"Lec 6 | MIT 6.00 Introduction to Computer Science and Programming, Fall 2008"},"content":{"rendered":"<h1><span class=\"\" id=\"parent-fieldname-title\">6: Bisection Methods, Newton\/Raphson, Introduction to Lists<br \/>\n        <\/span><\/h1>\n<p><embed src=\"http:\/\/ocw.mit.edu\/jw-player-free\/player.swf\" flashvars=\"file=http:\/\/www.youtube.com\/v\/hVHqs38fPe8\" allowfullscreen=\"true\" allowscriptaccess=\"always\" id=\"player1\" name=\"player1\" height=\"325\" width=\"530\"><\/p>\n<blockquote><p>def squareRootBi(x, epsilon):<br \/>&nbsp; &nbsp; &#8220;&#8221;&#8221;Assumes x &gt;= 0 and epsilon &gt; 0<br \/>&nbsp; &nbsp; Return y s.t. y * y is within epsilon of x&#8221;&#8221;&#8221;<br \/>&nbsp; &nbsp; assert x &gt;= 0, &#8216;x must be non-negative, not&#8217; + str(x)<br \/>&nbsp; &nbsp; assert epsilon &gt; 0, &#8216;epsilon must be positive, not&#8217; + str(epsilon)<br \/>&nbsp; &nbsp; low = 0<br \/>&nbsp; &nbsp; high = max(x, 1)<br \/>&nbsp; &nbsp; guess = (low + high) \/ 2.0<br \/>&nbsp; &nbsp; ctr = 1<br \/>&nbsp; &nbsp; while abs(guess ** 2 &#8211; x) &gt; epsilon and ctr &lt;= 100:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; #print &#8216;low:&#8217;, low, &#8216;high:&#8217;, high, &#8216;guess:&#8217;, guess<br \/>&nbsp; &nbsp; &nbsp; &nbsp; if guess ** 2 &lt; x:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; low = guess<br \/>&nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; high = guess<br \/>&nbsp; &nbsp; &nbsp; &nbsp; guess = (low + high) \/ 2.0<br \/>&nbsp; &nbsp; &nbsp; &nbsp; ctr += 1<br \/>&nbsp; &nbsp; assert ctr &lt;= 100, &#8216;Iteration count exceeded&#8217;<br \/>&nbsp; &nbsp; print &#8216;Bi method. Num. iteratins:&#8217;, ctr, &#8216;Estimate:&#8217;, guess<br \/>&nbsp; &nbsp; return guess<\/p>\n<p>def squareRootNR(x, epsilon):<br \/>&nbsp; &nbsp; &#8220;&#8221;&#8221;Assumes x &gt;= 0 and epsilon &gt; 0<br \/>&nbsp; &nbsp; Return y s.t. y * y is within epsilon of x&#8221;&#8221;&#8221;<br \/>&nbsp; &nbsp; assert x &gt;= 0, &#8216;x must be non-negative, not&#8217; + str(x)<br \/>&nbsp; &nbsp; assert epsilon &gt; 0, &#8216;epsilon must be positive, not&#8217; + str(epsilon)<br \/>&nbsp; &nbsp; x = float(x)<br \/>&nbsp; &nbsp; guess = x \/ 2.0<br \/>&nbsp; &nbsp; guess = 0.001<br \/>&nbsp; &nbsp; diff = guess ** 2 &#8211; x<br \/>&nbsp; &nbsp; ctr = 1<br \/>&nbsp; &nbsp; while abs(diff) &gt; epsilon and ctr &lt;= 100:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; #print &#8216;Error:&#8217;, diff, &#8216;guess:&#8217;, guess<br \/>&nbsp; &nbsp; &nbsp; &nbsp; guess = guess &#8211; diff \/ (2.0 * guess)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; diff = guess ** 2 &#8211; x<br \/>&nbsp; &nbsp; &nbsp; &nbsp; ctr += 1<br \/>&nbsp; &nbsp; assert ctr &lt;= 100, &#8216;Iteration count exceeded&#8217;<br \/>&nbsp; &nbsp; print &#8216;NR method. Num. iterations:&#8217;, ctr, &#8216;Estimate:&#8217;, guess<br \/>&nbsp; &nbsp; return guess<\/p>\n<p>squareRootBi(9, 0.001)<\/p>\n<p>squareRootBi(4, 0.001)<\/p>\n<p>squareRootBi(2, 0.001)<\/p>\n<p>squareRootBi(0.25, 0.001)<\/p>\n<p>squareRootBi(4, 0.00001)<br \/>squareRootNR(4, 0.00001)<\/p>\n<p>squareRootBi(123, 0.00001)<br \/>squareRootNR(123, 0.00001)<\/p>\n<p>squareRootBi(123456789, 0.00001)<br \/>squareRootNR(123456789, 0.00001)<\/p>\n<p>Techs = [&#8216;MIT&#8217;, &#8216;Cal Tech&#8217;]<br \/>print Techs<\/p>\n<p>Ivys = [&#8216;Harvard&#8217;, &#8216;Yale&#8217;, &#8216;Brown&#8217;]<br \/>print Ivys<br \/>Univs = []<br \/>Univs.append(Techs)<br \/>print Univs<br \/>Univs.append(Ivys)<br \/>raw_input()<br \/>print Univs<br \/>raw_input()<br \/>for e in Univs:<br \/>&nbsp; &nbsp; print e<br \/>&nbsp; &nbsp; for c in e:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; print c<br \/>raw_input()<br \/>Univs = Techs + Ivys<br \/>print Univs<br \/>raw_input()<br \/>Ivys.remove(&#8216;Harvard&#8217;)<br \/>print Univs<br \/>Ivys[1] = -1<br \/>print Ivys<\/p>\n<p>L1 = [1, 2, 3]<br \/>L2 = L1<br \/>L1[0] = 4<br \/>print L2<br \/>def f(L):<br \/>&nbsp; &nbsp; L[0] = 4<br \/>L1 = [1, 2, 3]<br \/>L2 = [1, 2, 3]<br \/>L3 = L1<br \/>print L1 == L2<br \/>f(L1)<br \/>print L1 == L2<br \/>print L1<br \/>print L2<br \/>print L3<\/p>\n<p>L1 = [1, 2, 3]<br \/>L2 = L1[:]&nbsp; # make a copy of L1<\/p><\/blockquote>\n<p><img loading=\"lazy\" src=\"http:\/\/pchero21.com\/wp-content\/uploads\/1\/XCtlSCDanE.png\" class=\"aligncenter\" width=\"400\" height=\"667\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>6: Bisection Methods, Newton\/Raphson, Introduction to Lists def squareRootBi(x, epsilon):&nbsp; &nbsp; &#8220;&#8221;&#8221;Assumes x &gt;= 0 and epsilon &gt; 0&nbsp; &nbsp; Return y s.t. y * y is within epsilon of x&#8221;&#8221;&#8221;&nbsp; &nbsp; assert x &gt;= 0, &#8216;x must be non-negative, &hellip; <a href=\"http:\/\/pchero21.com\/?p=980\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[259,293],"_links":{"self":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/980"}],"collection":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=980"}],"version-history":[{"count":0,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/980\/revisions"}],"wp:attachment":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=980"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}