{"id":4047,"date":"2012-09-13T09:46:11","date_gmt":"2012-09-13T09:46:11","guid":{"rendered":"http:\/\/uranchimeg.com\/Education\/?p=4047"},"modified":"2012-11-21T09:54:20","modified_gmt":"2012-11-21T09:54:20","slug":"mit-matlab-lesson-5","status":"publish","type":"post","link":"https:\/\/uranchimeg.com\/Education\/?p=4047","title":{"rendered":"MIT. Matlab lesson 5"},"content":{"rendered":"<p>\u0425\u0438\u0447\u044d\u044d\u043b \u2116 5<\/p>\n<h3>\u0414\u0430\u0432\u0442\u043b\u0430\u0433\u0430 \u0434\u0430\u0441\u0433\u0430\u043b:<\/h3>\n<ul>\n<li>\u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0438\u0439\u043d \u044d\u0445\u043d\u0438\u0439 20-\u043d \u0442\u043e\u043e\u0433 \u043e\u043b\u0436 \u0431\u0430\u0439\u0433\u0430\u0430 \u0434\u0430\u0432\u0442\u0430\u043b\u0442 \u0431\u0438\u0447. \u04ae\u0440 \u0434\u04af\u043d\u0433 vectorFibo \u043c\u0430\u0441\u0441\u0438\u0432\u0442 \u0445\u0430\u0434\u0433\u0430\u043b. \u0423\u043b\u043c\u0430\u0430\u0440\u00a0n \u0434\u0443\u0433\u0430\u0430\u0440 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0439\u0433\u00a0Fibo(n) \u0434\u0430\u0440\u0430\u0430\u0445 \u0431\u0430\u0439\u0434\u043b\u0430\u0430\u0440 \u0434\u0443\u0443\u0434:<\/li>\n<\/ul>\n<p>F(1)=1<\/p>\n<p>F(2)=1<\/p>\n<p>for n&gt;2<\/p>\n<p>F(n)=F(n-1)+F(n-2)<\/p>\n<h2>while \u043a\u043e\u043c\u0430\u043d\u0434<\/h2>\n<p>\u0425\u044d\u0440\u044d\u0432 \u0434\u0430\u0432\u0442\u0430\u043b\u0442\u044b\u043d \u0442\u043e\u043e \u0443\u0440\u044c\u0434\u0447\u0438\u043b\u0430\u043d \u0442\u043e\u0434\u043e\u0440\u0445\u043e\u0439 \u0431\u0438\u0448 \u0431\u043e\u043b \u0434\u0430\u0432\u0442\u0430\u043b\u0442\u0430\u043d\u0434 \u04e9\u04e9\u0440 \u043a\u043e\u043c\u0430\u043d\u0434 \u0430\u0448\u0438\u0433\u043b\u0430\u0445 \u043d\u044c \u0437\u04af\u0439\u0442\u044d\u0439 \u0431\u0430\u0439\u0434\u0430\u0433. \u0414\u043e\u043e\u0440 while \u043a\u043e\u043c\u0430\u043d\u0434\u044b\u043d \u0431\u0438\u0447\u0438\u0433\u043b\u044d\u043b\u0438\u0439\u0433 \u0445\u0430\u0440\u0443\u0443\u043b\u0430\u0432:<\/p>\n<p>while expression<\/p>\n<p>a<\/p>\n<p>bunch<\/p>\n<p>of<\/p>\n<p>statements<\/p>\n<p>end<\/p>\n<p>\u0414\u0430\u0432\u0442\u0430\u043b\u0442\u044b\u043d \u0430\u043b\u0445\u0430\u043c \u0431\u04af\u0440\u0438\u0439\u043d \u044d\u0445\u044d\u043d\u0434\u00a0Matlab \u00a0\u04e9\u0433\u04e9\u0433\u0434\u0441\u04e9\u043d \u043d\u04e9\u0445\u0446\u04e9\u043b\u0438\u0439\u0433 \u0448\u0430\u043b\u0433\u0430\u0430\u0434 \u0445\u044d\u0440\u044d\u0432 \u043d\u04e9\u0445\u0446\u04e9\u043b \u0431\u0438\u0435\u043b\u0436 \u0431\u0430\u0439\u0433\u0430\u0430 \u0431\u043e\u043b \u043a\u043e\u043c\u0430\u043d\u0434\u044b\u0433 \u0433\u04af\u0439\u0446\u044d\u0442\u0433\u044d\u0436 \u04af\u0433\u04af\u0439 \u0431\u043e\u043b \u0434\u0430\u0432\u0442\u0430\u043b\u0442\u044b\u043d \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u0430\u0441 \u0433\u0430\u0440\u0434\u0430\u0433.<\/p>\n<p>\u0416\u0438\u0448\u044d\u044d\u043b\u0431\u044d\u043b:<\/p>\n<p>&gt;&gt; x=10;<\/p>\n<p>&gt;&gt; while x&gt;0<\/p>\n<p>x=x-1<\/p>\n<p>end<\/p>\n<p>&gt;&gt;<\/p>\n<p>\u0433\u044d\u0441\u044d\u043d \u043a\u043e\u043c\u0430\u043d\u0434 9-\u044d\u044d\u0441 0 \u0445\u04af\u0440\u0442\u043b\u044d\u0445 \u0442\u043e\u043e\u0433 \u04e9\u04e9\u0440\u0441\u0434\u0438\u0439\u0433 \u043d\u044c \u043e\u0440\u0443\u0443\u043b\u0430\u043d \u0445\u044d\u0432\u043b\u044d\u043d\u044d.<\/p>\n<p>Matlab \u043d\u04e9\u0445\u0446\u04e9\u043b\u0438\u0439\u0433 \u0434\u0430\u043d\u0434\u0430\u0430 \u0448\u0430\u043b\u0433\u0430\u0445\u0433\u04af\u0439. \u0428\u0438\u043d\u044d \u0434\u0430\u0432\u0442\u0430\u043b\u0442 \u044d\u0445\u043b\u044d\u0445 \u04af\u0435\u0434 \u043b \u0448\u0430\u043b\u0433\u0430\u0434\u0430\u0433.<\/p>\n<h3>\u0414\u0430\u0441\u0433\u0430\u043b:<\/h3>\n<ul>\n<li>\u041c\u0430\u0441\u0441\u0438\u0432 \u0430\u0448\u0438\u0433\u043b\u0430\u043b\u0433\u04af\u0439\u0433\u044d\u044d\u0440 while \u043a\u043e\u043c\u0430\u043d\u0434\u044b\u0433 \u0430\u0448\u0438\u0433\u043b\u0430\u043d \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438\u0439\u043d 100 \u0434\u0430\u0445\u044c \u0442\u043e\u043e\u0433 \u043e\u043b.<\/li>\n<li>n \u0442\u043e\u043e\u0433 \u0448\u0430\u043b\u0433\u0430\u0436 \u0431\u0430\u0439\u0433\u0430\u0430 while \u0434\u0430\u0432\u0442\u0430\u043b\u0442 \u0431\u0438\u0447. \u0425\u044d\u0440\u044d\u0432 n \u0442\u044d\u0433\u0448 \u0442\u043e\u043e \u0431\u043e\u043b n \u0442\u043e\u043e\u0433 n\/2 \u0431\u043e\u043b\u0433\u043e, \u0441\u043e\u043d\u0434\u0433\u043e\u0439 \u0431\u043e\u043b 3n+1 \u0431\u043e\u043b\u0433\u043e. \u0425\u0430\u0440\u0438\u043d 1-\u0442\u044d\u0439 \u0442\u044d\u043d\u0446\u04af\u04af \u0431\u043e\u043b \u0434\u0430\u0432\u0442\u0430\u043b\u0442\u0430\u0430\u0441 \u0433\u0430\u0440. \u042d\u043d\u044d \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u0430\u0430 \u04e9\u04e9\u0440 \u0443\u0442\u0433\u0430\u0430\u0440 \u044d\u0445\u043b\u04af\u04af\u043b\u0436 1 \u0431\u043e\u043b\u043e\u0445 \u0445\u04af\u0440\u0442\u044d\u043b \u0445\u0438\u0447\u043d\u044d\u044d\u043d \u0430\u043b\u0445\u0430\u043c \u0448\u0430\u0430\u0440\u0434\u0430\u0433\u0434\u0430\u0436 \u0431\u0430\u0439\u0433\u0430\u0430\u0433 \u0442\u043e\u043e\u043b.<\/li>\n<\/ul>\n<h2>\u0425\u044d\u0440\u044d\u0433\u043b\u044d\u0433\u0447\u0438\u0439\u043d \u0444\u0443\u043d\u043a\u0446<\/h2>\n<p>Matlab \u043f\u0440\u043e\u0433\u0440\u0430\u043c \u043d\u044c \u04e9\u04e9\u0440\u0442\u04e9\u04e9 \u043e\u043b\u043e\u043d \u0442\u043e\u043e\u043d\u044b \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 \u0444\u0443\u043d\u043a\u0446\u0442\u044d\u0439 \u0431\u043e\u043b\u043e\u0432\u0447 \u0445\u044d\u0440\u044d\u0433\u043b\u044d\u0433\u0447\u04e9\u04e9\u0440\u04e9\u04e9 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0439\u043d \u043d\u044d\u0433\u044d\u043d \u0430\u0434\u0438\u043e \u0431\u0438\u0435\u043b\u044d\u0433\u0434\u044d\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439\u0433 \u04af\u04af\u0441\u0433\u044d\u0436 \u0431\u043e\u043b\u043d\u043e.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u0439\u0433 \u0442\u0443\u0441\u0433\u0430\u0439 \u0444\u0430\u0439\u043b\u0434 \u0431\u0438\u0447\u043d\u044d. \u0424\u0430\u0439\u043b\u044b\u043d \u043d\u044d\u0440 \u043d\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0439\u043d \u043d\u044d\u0440\u0442\u044d\u0439 \u0438\u0436\u0438\u043b \u0431\u0430\u0439\u0445 \u0451\u0441\u0442\u043e\u0439. \u0416\u0438\u0448\u044d\u044d\u043b\u0431\u044d\u043b,\u00a0example_function_ 1 \u0444\u0443\u043d\u043a\u0446 \u04af\u04af\u0441\u0433\u044d\u043b\u044d\u044d \u0433\u044d\u0436 \u0431\u043e\u0434\u043e\u0445\u043e\u0434 \u0443\u0433 \u0444\u0443\u043d\u043a\u0446\u0438\u0439\u0433 \u0430\u0433\u0443\u0443\u043b\u0430\u0445 \u0444\u0430\u0439\u043b\u00a0example_function_1.m \u043d\u044d\u0440\u0442\u044d\u0439 \u0431\u0430\u0439\u0445 \u0451\u0441\u0442\u043e\u0439.<\/p>\n<p>function y = example_function_1(x)<\/p>\n<p>is the first line of the file. let&#8217;s dissect this first line:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<ul>\n<li>function<\/li>\n<\/ul>\n<\/td>\n<td>As I said, the first word must be &#8220;function&#8221;.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<ul>\n<li>y =<\/li>\n<\/ul>\n<\/td>\n<td>This declares the local name of a variable whose value will be returned when the execution of the function is over.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<ul>\n<li>example_function_1<\/li>\n<\/ul>\n<\/td>\n<td>The name of the function. This must match the name of the file.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<ul>\n<li>(x)<\/li>\n<\/ul>\n<\/td>\n<td>The local name of the variable that will contain the input value.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So the rest of the file could be<\/p>\n<p>y=2*x;<\/p>\n<p>end<\/p>\n<p>admittedly, a very boring function: It takes the input value (locally called x), doubles it, and assigns that result to y. Since y is declared to be the return value and the function has<\/p>\n<p>ended, the return value is simply twice the input.<\/p>\n<p>we call the function by giving it a value:<\/p>\n<p>&gt;&gt; example_function_1(17)<\/p>\n<p>or<\/p>\n<p>&gt;&gt; example_function_1(rand)<\/p>\n<p>The return value can then be assigned to another variable:<\/p>\n<p>&gt;&gt; z= example_function_1(18);<\/p>\n<p>&gt;&gt; z<\/p>\n<h3>Variable Scope<\/h3>\n<p>An important difference between a function and a script (the files that don&#8217;t have &#8220;function&#8221; as their first word are called scripts) is that of &#8220;variable scope&#8221;.<\/p>\n<p>When executing a script from the command-line, the scripts has access to all the variables that are defined on the command line. For instance, if you have a script called script1.m whose contents are<\/p>\n<p>x+1<\/p>\n<p>y=y\/2<\/p>\n<p>and you run this script from the commandline like so:<\/p>\n<p>&gt;&gt; x=2<\/p>\n<p>&gt;&gt; y = 6<\/p>\n<p>&gt;&gt; script1<\/p>\n<p>&gt;&gt; y<\/p>\n<p>You will see that the script knows that x=2 and that y=6. Not only this, note that y is changed in the scripts, the new value of y, 3 is also the value of y when check on the command<\/p>\n<p>line. This seems natural, perhaps, but this is\u00a0<strong>not<\/strong>\u00a0how functions behave. A function with the same content function1.m<\/p>\n<p>function function1<\/p>\n<p>x+1<\/p>\n<p>y=y\/2<\/p>\n<p>will fail to run, since within the function there are no such variables x and y. The variables of the function, are different from the variables outside the function, even if they have<\/p>\n<p>the same name.<\/p>\n<ul>\n<li>change your prime checking program so that the input is the number, and the output is 1 if the number is prime and 0 if not.<\/li>\n<li>change your n-changing program, so that the input is the initial value of n and the output is the number of steps it took to reach 1.<\/li>\n<li>use your primechecking function in another program (can be a script) that finds (a) the 1000th prime number and (b) the first prime number greater than 1000000.<\/li>\n<li>use your n-changing function in another program (can be a script) that finds the number of iterations needed for the first 100 numbers, and plots then\u00a0<strong>nicely.\u00a0<\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u0425\u0438\u0447\u044d\u044d\u043b \u2116 5<\/p>\n<p> \u0414\u0430\u0432\u0442\u043b\u0430\u0433\u0430 \u0434\u0430\u0441\u0433\u0430\u043b: \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0438\u0439\u043d \u044d\u0445\u043d\u0438\u0439 20-\u043d \u0442\u043e\u043e\u0433 \u043e\u043b\u0436 \u0431\u0430\u0439\u0433\u0430\u0430 \u0434\u0430\u0432\u0442\u0430\u043b\u0442 \u0431\u0438\u0447. \u04ae\u0440 \u0434\u04af\u043d\u0433 vectorFibo \u043c\u0430\u0441\u0441\u0438\u0432\u0442 \u0445\u0430\u0434\u0433\u0430\u043b. \u0423\u043b\u043c\u0430\u0430\u0440\u00a0n \u0434\u0443\u0433\u0430\u0430\u0440 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0439\u0433\u00a0Fibo(n) \u0434\u0430\u0440\u0430\u0430\u0445 \u0431\u0430\u0439\u0434\u043b\u0430\u0430\u0440 \u0434\u0443\u0443\u0434: <\/p>\n<p>F(1)=1<\/p>\n<p>F(2)=1<\/p>\n<p>for n&gt;2<\/p>\n<p>F(n)=F(n-1)+F(n-2)<\/p>\n<p> while \u043a\u043e\u043c\u0430\u043d\u0434 <\/p>\n<p>\u0425\u044d\u0440\u044d\u0432 \u0434\u0430\u0432\u0442\u0430\u043b\u0442\u044b\u043d \u0442\u043e\u043e \u0443\u0440\u044c\u0434\u0447\u0438\u043b\u0430\u043d \u0442\u043e\u0434\u043e\u0440\u0445\u043e\u0439 \u0431\u0438\u0448 \u0431\u043e\u043b \u0434\u0430\u0432\u0442\u0430\u043b\u0442\u0430\u043d\u0434 \u04e9\u04e9\u0440 \u043a\u043e\u043c\u0430\u043d\u0434 \u0430\u0448\u0438\u0433\u043b\u0430\u0445 \u043d\u044c \u0437\u04af\u0439\u0442\u044d\u0439 \u0431\u0430\u0439\u0434\u0430\u0433. \u0414\u043e\u043e\u0440 while \u043a\u043e\u043c\u0430\u043d\u0434\u044b\u043d \u0431\u0438\u0447\u0438\u0433\u043b\u044d\u043b\u0438\u0439\u0433 \u0445\u0430\u0440\u0443\u0443\u043b\u0430\u0432:<\/p>\n<p>while expression<\/p>\n<p>a<\/p>\n<p>bunch<\/p>\n<p> <span style=\"color:#777\"> . . . &rarr; Read More: <a href=\"https:\/\/uranchimeg.com\/Education\/?p=4047\">MIT. Matlab lesson 5<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,12,20],"tags":[47,68,65,55,24],"_links":{"self":[{"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=\/wp\/v2\/posts\/4047"}],"collection":[{"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4047"}],"version-history":[{"count":0,"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=\/wp\/v2\/posts\/4047\/revisions"}],"wp:attachment":[{"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}