{"id":4026,"date":"2012-09-12T07:13:04","date_gmt":"2012-09-12T07:13:04","guid":{"rendered":"http:\/\/uranchimeg.com\/Education\/?p=4026"},"modified":"2012-11-21T09:54:20","modified_gmt":"2012-11-21T09:54:20","slug":"mit-matlab-%d1%85%d0%b8%d1%87%d1%8d%d1%8d%d0%bb-%e2%84%96-4","status":"publish","type":"post","link":"https:\/\/uranchimeg.com\/Education\/?p=4026","title":{"rendered":"MIT. Matlab \u0445\u0438\u0447\u044d\u044d\u043b \u2116 4"},"content":{"rendered":"<p>\u0425\u0438\u0447\u044d\u044d\u043b \u2116 4<\/p>\n<h3>\u0414\u0430\u0441\u0433\u0430\u043b:<\/h3>\n<ul>\n<li>n \u0445\u0443\u0432\u044c\u0441\u0430\u0433\u0447 1-\u0441 10 \u0431\u0430\u0439\u0445 \u0445\u043e\u043e\u0440\u043e\u043d\u0434\u00a0-1^(n+1) \/ (2n-1) \u0438\u043b\u044d\u0440\u0445\u0438\u0439\u043b\u043b\u0438\u0439\u0433 \u0431\u043e\u0434. N=100 \u0431\u0430\u0439\u043d\u0430.<\/li>\n<li>\u041d\u0438\u0439\u043b\u0431\u044d\u0440 \u043d\u044c \u00a0pi\/4 \u0431\u0430\u0439\u0445\u00a0N \u0442\u043e\u043e\u043d\u044b \u0443\u0442\u0433\u044b\u0433 \u043e\u043b. (\u044d\u043d\u0434 &lt; 10^-6 \u0431\u0430\u0439\u043d\u0430)<\/li>\n<\/ul>\n<h2>\u041f\u0440\u043e\u0433\u0440\u0430\u043c\u0447\u043b\u0430\u043b\u044b\u043d \u043a\u043e\u043c\u0430\u043d\u0434\u0443\u0443\u0434<\/h2>\n<p>Matlab \u043f\u0440\u043e\u0433\u0440\u0430\u043c \u043d\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u0447\u043b\u0430\u0445 \u0431\u043e\u043b\u043e\u043c\u0436\u0438\u0439\u0433 \u0445\u044d\u0440\u044d\u0433\u043b\u044d\u0433\u0447\u0438\u0434 \u043e\u043b\u0433\u043e\u0434\u043e\u0433. \u0417\u0430\u0440\u0438\u043c \u04af\u043d\u0434\u0441\u044d\u043d \u043a\u043e\u043c\u0430\u043d\u0434\u0443\u0443\u0434\u044b\u0433 \u04af\u0437\u044d\u0446\u0433\u044d\u044d\u0435.<\/p>\n<h3>if \u041a\u043e\u043c\u0430\u043d\u0434<\/h3>\n<p>\u0422\u043e\u0434\u043e\u0440\u0445\u043e\u0439 \u043d\u04e9\u0445\u0446\u04e9\u043b \u0431\u0438\u0435\u043b\u044d\u0433\u0434\u044d\u0436 \u0431\u0430\u0439\u0445 \u04af\u0435\u0434 \u044f\u043c\u0430\u0440 \u043d\u044d\u0433\u044d\u043d \u04af\u0439\u043b\u0434\u043b\u0438\u0439\u0433 \u0445\u0438\u0439\u0434\u044d\u0433 \u0431\u0430\u0439\u0445\u0430\u0430\u0440 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u0447\u043b\u0430\u0445 \u0431\u043e\u043b \u0425\u042d\u0420\u0412\u042d\u042d \u0431\u0443\u044e\u0443 if \u043a\u043e\u043c\u0430\u043d\u0434\u044b\u0433 \u0430\u0448\u0438\u0433\u043b\u0430\u043d\u0430.<\/p>\n<p>if condition<\/p>\n<p>Bunch<\/p>\n<p>Of<\/p>\n<p>expressions<\/p>\n<p>else<\/p>\n<p>Other<\/p>\n<p>expressions<\/p>\n<p>end<\/p>\n<p>\u0414\u044d\u044d\u0440\u0445 \u0431\u0438\u0447\u0438\u0433\u043b\u044d\u043b\u0438\u0439\u0433 \u043e\u0440\u0447\u0443\u0443\u043b\u0431\u0430\u043b:<\/p>\n<p>\u0425\u042d\u0420\u0412\u042d\u042d \u04e9\u0433\u04e9\u0433\u0434\u0441\u04e9\u043d \u041d\u04e8\u0425\u0426\u04e8\u041b<\/p>\n<p>\u0411\u0418\u0415\u041b\u042d\u0413\u0414\u042d\u0416 \u0431\u0430\u0439\u0433\u0430\u0430 \u0431\u043e\u043b \u043d\u044d\u0433 \u04af\u0439\u043b\u0434\u044d\u043b \u0445\u0438\u0439\u043d\u044d<\/p>\n<p>\u04ae\u0413\u04ae\u0419 \u0431\u043e\u043b \u04e9\u04e9\u0440 \u04af\u0439\u043b\u0434\u044d\u043b \u0445\u0438\u0439\u043d\u044d<\/p>\n<p>\u0414\u0423\u0423\u0421\u0413\u0410<\/p>\n<p>\u0433\u044d\u0441\u044d\u043d \u0431\u0430\u0439\u0434\u043b\u0430\u0430\u0440 \u0431\u0438\u0447\u0438\u0436 \u0431\u043e\u043b\u043e\u0445 \u044e\u043c.<\/p>\n<p>if \u043a\u043e\u043c\u0430\u043d\u0434 \u043d\u044c \u04e9\u0433\u04e9\u0433\u0434\u0441\u04e9\u043d \u043d\u04e9\u0445\u0446\u04e9\u043b\u0438\u0439\u043d \u0431\u0438\u0435\u043b\u044d\u043b\u0442\u044d\u044d\u0441 \u0445\u0430\u043c\u0430\u0430\u0440\u0430\u043d \u0431\u043e\u043b\u043e\u043c\u0436\u0438\u0442 2 \u04af\u0439\u043b\u0434\u043b\u0438\u0439\u043d \u0430\u043b\u044c \u043d\u044d\u0433\u0438\u0439\u0433 \u0441\u043e\u043d\u0433\u043e\u043d \u0445\u0438\u0439\u0445 \u0448\u0430\u0430\u0440\u0434\u043b\u0430\u0433\u0430\u0442\u0430\u0439 \u04af\u0435\u0434 \u0430\u0448\u0438\u0433\u043b\u0430\u0445\u0430\u0434 \u0445\u0430\u043c\u0433\u0438\u0439\u043d \u0442\u043e\u0445\u0438\u0440\u043e\u043c\u0436\u0442\u043e\u0439 \u0431\u0430\u0439\u0434\u0430\u0433 \u043a\u043e\u043c\u0430\u043d\u0434 \u044e\u043c. \u041d\u04e9\u0445\u0446\u04e9\u043b \u0431\u0438\u0435\u043b\u044d\u0433\u0434\u044d\u0436 \u0431\u0430\u0439\u0432\u0430\u043b \u044d\u0445\u043d\u0438\u0439 \u04af\u0439\u043b\u0434\u044d\u043b \u04af\u0433\u04af\u0439 \u0431\u043e\u043b else \u0442\u04af\u043b\u0445\u04af\u04af\u0440 \u04af\u0433\u0438\u0439\u043d \u0434\u0430\u0440\u0430\u0430\u0433\u0430\u0430\u0440 \u0431\u0438\u0447\u0438\u0433\u0434\u044d\u0436 \u0431\u0430\u0439\u0433\u0430\u0430 \u04af\u0439\u043b\u0434\u044d\u043b \u0445\u0438\u0439\u0433\u0434\u0434\u044d\u0433.<\/p>\n<p>\u0416\u0438\u0448\u044d\u044d\u043b\u0431\u044d\u043b:<\/p>\n<p>&gt;&gt; if 3&lt;4<\/p>\n<p>&#8216;3 is less than 4&#8217;<\/p>\n<p>else<\/p>\n<p>&#8216;3 is not less than 4&#8217;<\/p>\n<p>end<\/p>\n<p>&gt;&gt;<\/p>\n<p>\u0414\u044d\u044d\u0440\u0445\u0438\u0439\u0433 \u0443\u043d\u0448\u0432\u0430\u043b: 3 \u043d\u044c 4 -\u0441 \u0431\u0430\u0433\u0430 \u043d\u044c \u04af\u043d\u044d\u043d \u0431\u043e\u043b &#8220;3, 4-\u0441 \u0431\u0430\u0433\u0430&#8221; \u04af\u0433\u04af\u0439 \u0431\u043e\u043b\u00a0&#8220;3, 4-\u0441 \u0431\u0430\u0433\u0430 \u0431\u0438\u0448&#8221; \u0433\u044d\u0441\u044d\u043d \u0442\u0430\u0439\u043b\u0431\u0430\u0440\u044b\u0433 \u0434\u044d\u043b\u0433\u044d\u0446 \u0434\u044d\u044d\u0440 \u0433\u0430\u0440\u0433\u0430\u043d\u0430. \u00a0\u0414\u0430\u0440\u0430\u0430\u0445 \u043a\u043e\u043c\u0430\u043d\u0434\u0443\u0443\u0434\u044b\u0433 \u0444\u0430\u0439\u043b \u04af\u04af\u0441\u0433\u044d\u043d \u0431\u0438\u0447\u044d\u044d \u0430\u0436\u0438\u043b\u043b\u0443\u0443\u043b\u0436 \u04af\u0437\u044d\u044d\u0440\u044d\u0439\u00a0(<a href=\"http:\/\/ocw.mit.edu\/ans7870\/resources\/farjoun\/if_example.m\" rel=\"nofollow\">if_example.m<\/a>)<\/p>\n<p>x = rand;<\/p>\n<p>s = &#8216;The number %g is %s than 0.5\\n&#8217;;<\/p>\n<p>if x&lt;0.5<\/p>\n<p>fprintf(s,x,&#8217;less&#8217;)<\/p>\n<p>else<\/p>\n<p>fprintf(s,x,&#8217;more&#8217;)<\/p>\n<p>end<\/p>\n<p>\u04ae\u04af\u0441\u0433\u044d\u0441\u044d\u043d \u0444\u0430\u0439\u043b\u0430\u0430 \u0445\u0430\u0434\u0433\u0430\u043b\u0430\u0430\u0434 \u0430\u0436\u0438\u043b\u043b\u0443\u0443\u043b\u043d\u0430.<\/p>\n<p>rand \u043d\u044c \u0441\u0430\u043d\u0430\u043c\u0441\u0430\u0440\u0433\u04af\u0439 \u0442\u043e\u043e \u0433\u0430\u0440\u0433\u0430\u0433\u0447 \u0444\u0443\u043d\u043a\u0446 \u044e\u043c.<\/p>\n<p><span style=\"font-family: arial, sans-serif;\">fprintf\u00a0<\/span>\u0444\u0443\u043d\u043a\u0446 \u043d\u044c<span style=\"font-family: arial, sans-serif;\">\u00a0sprintf\u00a0<\/span>\u0444\u0443\u043d\u043a\u0446\u0442\u044d\u0439 \u0438\u0436\u0438\u043b \u04af\u0439\u043b\u0434\u044d\u043b \u0445\u0438\u0439\u043d\u044d. \u042f\u043b\u0433\u0430\u0430 \u043d\u044c \u04af\u0440 \u0434\u04af\u043d\u0433 \u0434\u044d\u043b\u0433\u044d\u0446 \u0440\u04af\u04af \u0433\u0430\u0440\u0433\u0430\u0445\u0433\u04af\u0439 \u0445\u0430\u0440\u0438\u043d \u0444\u0430\u0439\u043b \u0440\u0443\u0443 \u0431\u0438\u0447\u043d\u044d:<\/p>\n<p>&gt;&gt; a=sprintf(&#8220;Hello!&#8221;)<\/p>\n<p>&gt;&gt; b=fprintf(&#8220;Hello!&#8221;)<\/p>\n<p>&#8216;\\n&#8217; \u0442\u044d\u043c\u0434\u044d\u0433\u043b\u044d\u044d \u0445\u04e9\u0442\u043b\u04e9\u0433\u0447\u0438\u0439\u0433 \u043d\u044d\u0433 \u0448\u0438\u043d\u044d \u043c\u04e9\u0440\u04e9\u043d\u0434 \u0448\u0438\u043b\u0436\u04af\u04af\u043b\u043d\u044d.<\/p>\n<h3>\u04ae\u043d\u044d\u043d \u0433\u044d\u0436 \u044e\u0443 \u0432\u044d?<\/h3>\n<p>\u0425\u0443\u0434\u0430\u043b \u04af\u043d\u044d\u043d\u0438\u0439 \u0442\u0430\u043b\u0430\u0430\u0440 \u0441\u044d\u0442\u0433\u044d\u043b \u0437\u04af\u0439\u043d \u0430\u0441\u0443\u0443\u0434\u0430\u043b \u044d\u043d\u0434 \u044f\u0440\u0438\u0445 \u0433\u044d\u0441\u044d\u043d\u0433\u04af\u0439. Matlab \u043f\u0440\u043e\u0433\u0440\u0430\u043c \u04af\u043d\u044d\u043d \u0445\u0443\u0434\u0430\u043b \u0433\u044d\u0434\u044d\u0433\u0442 \u044e\u0443\u0433 \u043e\u0439\u043b\u0433\u043e\u0434\u043e\u0433 \u0432\u044d? \u0433\u044d\u0434\u0433\u0438\u0439\u0433 \u043f\u0440\u043e\u0433\u0440\u0430\u043c \u0431\u0438\u0447\u0438\u0436 \u0431\u0430\u0439\u0433\u0430\u0430 \u0445\u04af\u043d \u04e9\u04e9\u0440\u04e9\u04e9 \u0441\u0430\u0439\u043d \u043e\u0439\u043b\u0433\u043e\u0436 \u0431\u0430\u0439\u0445 \u0451\u0441\u0442\u043e\u0439 \u0443\u0447\u0438\u0440 \u044d\u043d\u044d \u0445\u044d\u0441\u0433\u0438\u0439\u0433 \u043e\u0440\u0443\u0443\u043b\u043b\u0430\u0430. \u04ae\u04af\u043d\u0438\u0439 \u0442\u0443\u043b\u0434\u00a0(oracle.m) \u0433\u044d\u0441\u044d\u043d \u0448\u0438\u043d\u044d \u0444\u0430\u0439\u043b \u04af\u04af\u0441\u0433\u044d\u043d\u044d.<\/p>\n<p>if x<\/p>\n<p>&#8216;True&#8217;<\/p>\n<p>else<\/p>\n<p>&#8216;False&#8217;<\/p>\n<p>end<\/p>\n<p>\u0423\u043b\u043c\u0430\u0430\u0440 \u04af\u04af\u0441\u0433\u044d\u0441\u044d\u043d \u0444\u0430\u0439\u043b\u0430\u0430 \u0430\u0436\u0438\u043b\u043b\u0443\u0443\u043b\u0436 \u0434\u0430\u0440\u0430\u0430\u0445 \u04e9\u0433\u04e9\u0433\u0434\u043b\u04af\u04af\u0434\u0438\u0439\u0433 \u04e9\u0433\u0447 \u0433\u0430\u0440\u0430\u0445 \u04af\u0440 \u0434\u04af\u043d\u0433 \u0430\u0436\u0438\u0433\u043b\u0430\u0430\u0440\u0430\u0439.<\/p>\n<p>x=(1==4)<\/p>\n<p>x=4<\/p>\n<p>x=0<\/p>\n<p>x=[0 0]<\/p>\n<p>x=[]<\/p>\n<p>x=[0 1]<\/p>\n<p>x=[1 2 3]<\/p>\n<p>x=&#8217;hello&#8217;<\/p>\n<p>x=&#8221;<\/p>\n<p>x=&#8217; &#8216;<\/p>\n<p>x=-1<\/p>\n<p>x=i<\/p>\n<h2>\u0414\u0430\u0432\u0442\u0430\u043b\u0442<\/h2>\n<p>Loops are good when we want to do stuff over and over again. For example, if we didn&#8217;t know how to sum up the elements of a vector using sum or linear algebra commands, we could use a loop to do it (a bad idea&#8230;much slower than the alternative, about 100 times slower!!! check out\u00a0<a href=\"http:\/\/ocw.mit.edu\/ans7870\/resources\/farjoun\/loop_timer.m\" rel=\"nofollow\">loop_timer.m<\/a>\u00a0if you care.) However, slow they may be, but we might still need them&#8230;.The syntax is as follows:<\/p>\n<p>for variable = vector<\/p>\n<p>a bunch<\/p>\n<p>of<\/p>\n<p>expressions<\/p>\n<p>here variable is an<\/p>\n<p>ELEMENT of vector<\/p>\n<p>end<\/p>\n<p>The way it works is that for every element in vector, matlab runs through all the commands in the block. during each such execution, the value of variable is set to that element&#8230;Let see a simple example:<\/p>\n<p>for v = 1:10<\/p>\n<p>v<\/p>\n<p>v^2<\/p>\n<p>end<\/p>\n<p>Notice that\u00a0 for every number from 1 to 10 we have two printouts (since there were two commands in the block) one simply the number and the other is the number squared&#8230;just as we asked.<\/p>\n<p>One can have any legal variable name instead of the variable, but note that it gets overwritten. Also the vector, can be any vector&#8230;<\/p>\n<p>for v = &#8216;hello&#8217;<\/p>\n<p>v<\/p>\n<p>end<\/p>\n<p>One can &#8220;break out&#8221; of a loop before it is done by issuing a break command:<\/p>\n<p>for v = &#8216;hello and goodbye&#8217;<\/p>\n<p>v<\/p>\n<p>if v==&#8217;d&#8217;<\/p>\n<p>break<\/p>\n<p>end<\/p>\n<p>end<\/p>\n<p>OK, enough new material..lets exercise!<\/p>\n<h3>Exercises<\/h3>\n<ul>\n<li>Find the sum of a vector\u00a0x=[3 5 12 42 67]\u00a0without using the old tricks&#8230;that is, use afor\u00a0loop and don&#8217;t use\u00a0sum.<\/li>\n<li>Write a little program that checks if\u00a0x=73\u00a0is prime. Do not use\u00a0isprime.\u00a0But you might find\u00a0mod\u00a0or\u00a0rem\u00a0useful.<\/li>\n<li>Modify your program to find the first 20 primes<\/li>\n<li>Write a program that find the error in the calculation from the top of the page for\u00a0N = 1, 10, 100,&#8230;10^6\u00a0and plots the results in a meaningful way. (you might want to consider using a log-plot, with\u00a0loglog,\u00a0or manually, taking the\u00a0log\u00a0from both axes before plotting)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u0425\u0438\u0447\u044d\u044d\u043b \u2116 4<\/p>\n<p> \u0414\u0430\u0441\u0433\u0430\u043b: n \u0445\u0443\u0432\u044c\u0441\u0430\u0433\u0447 1-\u0441 10 \u0431\u0430\u0439\u0445 \u0445\u043e\u043e\u0440\u043e\u043d\u0434\u00a0-1^(n+1) \/ (2n-1) \u0438\u043b\u044d\u0440\u0445\u0438\u0439\u043b\u043b\u0438\u0439\u0433 \u0431\u043e\u0434. N=100 \u0431\u0430\u0439\u043d\u0430. \u041d\u0438\u0439\u043b\u0431\u044d\u0440 \u043d\u044c \u00a0pi\/4 \u0431\u0430\u0439\u0445\u00a0N \u0442\u043e\u043e\u043d\u044b \u0443\u0442\u0433\u044b\u0433 \u043e\u043b. (\u044d\u043d\u0434 &lt; 10^-6 \u0431\u0430\u0439\u043d\u0430) \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u0447\u043b\u0430\u043b\u044b\u043d \u043a\u043e\u043c\u0430\u043d\u0434\u0443\u0443\u0434 <\/p>\n<p>Matlab \u043f\u0440\u043e\u0433\u0440\u0430\u043c \u043d\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u0447\u043b\u0430\u0445 \u0431\u043e\u043b\u043e\u043c\u0436\u0438\u0439\u0433 \u0445\u044d\u0440\u044d\u0433\u043b\u044d\u0433\u0447\u0438\u0434 \u043e\u043b\u0433\u043e\u0434\u043e\u0433. \u0417\u0430\u0440\u0438\u043c \u04af\u043d\u0434\u0441\u044d\u043d \u043a\u043e\u043c\u0430\u043d\u0434\u0443\u0443\u0434\u044b\u0433 \u04af\u0437\u044d\u0446\u0433\u044d\u044d\u0435.<\/p>\n<p> if \u041a\u043e\u043c\u0430\u043d\u0434 <\/p>\n<p>\u0422\u043e\u0434\u043e\u0440\u0445\u043e\u0439 \u043d\u04e9\u0445\u0446\u04e9\u043b \u0431\u0438\u0435\u043b\u044d\u0433\u0434\u044d\u0436 \u0431\u0430\u0439\u0445 \u04af\u0435\u0434 \u044f\u043c\u0430\u0440 \u043d\u044d\u0433\u044d\u043d \u04af\u0439\u043b\u0434\u043b\u0438\u0439\u0433 \u0445\u0438\u0439\u0434\u044d\u0433 \u0431\u0430\u0439\u0445\u0430\u0430\u0440 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u0447\u043b\u0430\u0445 \u0431\u043e\u043b <span style=\"color:#777\"> . . . &rarr; Read More: <a href=\"https:\/\/uranchimeg.com\/Education\/?p=4026\">MIT. Matlab \u0445\u0438\u0447\u044d\u044d\u043b \u2116 4<\/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\/4026"}],"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=4026"}],"version-history":[{"count":0,"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=\/wp\/v2\/posts\/4026\/revisions"}],"wp:attachment":[{"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/uranchimeg.com\/Education\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}