{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fibonacci Sequence\n", "$F_1 = 1$, $F_2 = 1$ and $F_i = F_{i-1} + F_{i-2}$, $i = 3, 4, ...$" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "F =\n", "\n", " Columns 1 through 11:\n", "\n", " 1 1 2 3 5 8 13 21 34 55 89\n", "\n", " Columns 12 through 20:\n", "\n", " 144 233 377 610 987 1597 2584 4181 6765\n", "\n" ] } ], "source": [ "n=20;\n", "F(1)=1;\n", "F(2)=1;\n", "for i=3:n\n", " F(i)=F(i-1)+F(i-2);\n", "end\n", "F\n" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "F =\n", "\n", " 1 1 2 3 5 8 13 21 34 55\n", "\n" ] } ], "source": [ "clear all\n", "F(1)=1;\n", "F(2)=1;\n", "n=3;\n", "F(3) = F(1) + F(2);\n", "while n<=10\n", " F(n)=F(n-1)+F(n-2);\n", " n=n+1;\n", "end\n", "F" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A simple while loop" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = 1\n", "x = 2\n", "x = 4\n", "x = 8\n", "x = 16\n" ] } ], "source": [ "x=1\n", "while x<=10\n", " x=2*x\n", "end" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum of two matrices without loop\n", "ans =\n", "\n", " 6 8 10\n", " 12 14 6\n", " 10 12 14\n", "\n", "sum of two matrices using for loop\n", "C =\n", "\n", " 6 8 10\n", " 12 14 6\n", " 10 12 14\n", "\n" ] } ], "source": [ "A=[1 2 3; 4 5 6; 7 8 9];\n", "B=[5 6 7;8 9 0;3 4 5];\n", "disp('sum of two matrices without loop')\n", "A+B\n", "for i=1:3\n", " for j=1:3\n", " C(i,j)=A(i,j)+B(i,j);\n", " end\n", "end\n", "disp('sum of two matrices using for loop')\n", "C" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix multiplication" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix multiplication without loop\n", " 42 36 30\n", " 96 81 66\n", " 150 126 102\n", "Matrix multiplication using for loop\n", " 42 36 30\n", " 96 81 66\n", " 150 126 102\n" ] } ], "source": [ "A=[1 2 3; 4 5 6; 7 8 9];\n", "B= [3 2 1; 6 5 4; 9 8 7];\n", "C=zeros(3);\n", "disp('Matrix multiplication without loop')\n", "disp(A*B)\n", "for i=1:3\n", " for j=1:3\n", " % C(i,j)=0;\n", " for k=1:3\n", " C(i,j)=C(i,j)+A(i,k)*B(k,j);\n", " end\n", " end\n", "end\n", "disp('Matrix multiplication using for loop')\n", "disp(C)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find factorial $n! = 1. 2. 3. ... (n-1)n$" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "using for loop\n", " 120\n", "using while loop\n", " 120\n" ] } ], "source": [ "n=5;\n", "f=1;\n", "i = 1;\n", "for i=1:n\n", " f=f*i;\n", " i=i+1;\n", "end\n", "disp('using for loop')\n", "disp(f)\n", "\n", "f1=1;\n", "i=1;\n", "while i<=n\n", " f1=f1*i;\n", " i=i+1;\n", "end\n", "disp('using while loop')\n", "disp(f1)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "function f = fact(n)\n", " f = prod(1:n);\n", "endfunction" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 120\r\n" ] } ], "source": [ "fact(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fixed point iteration,\n", "$x = \\phi(x)$, $x_n =\\phi(x_{n-1})$. \n", "For example $x = cosh(x/4)$" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Fixed point iteration\n", "x = 1.0336\n", "k = 7\n" ] } ], "source": [ "disp('Fixed point iteration');\n", "phi = @(x) cosh(x/4);\n", "x=1;\n", "er = 1;\n", "k=0;\n", "while ((er > 1e-8) && (k < 100))\n", " xnew = phi(x);\n", " er = abs(xnew-x);\n", " x = xnew;\n", " k = k+1;\n", "end\n", "x\n", "k" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Octave", "language": "octave", "name": "octave" }, "language_info": { "file_extension": ".m", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-octave", "name": "octave", "version": "0.16.1" } }, "nbformat": 4, "nbformat_minor": 1 }