{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Commands" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Commands Table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Command|Result|\n", "|---|\n", "|matlab|loads the program matlab into your workspace|\n", "|quit|quits matlab, returning you to the operating system|\n", "|exit|same as quit|\n", "|who|lists all of the variables in your matlab workspace|\n", "|whos|list the variables and describes their matrix size|\n", "|clear|deletes all matrices from active workspace|\n", "|clear *x*|deletes the matrix x from active workspace|\n", "|...|the ellipsis defining a line continuation is three successive periods|\n", "|save|saves all the matrices defined in the current session into the file, matlab.mat|\n", "|load|loads contents of matlab.mat into current workspace|\n", "|save *filename*|saves the contents of workspace into filename.mat|\n", "|save *filename x y z*|saves the matrices x, y and z into the file titled filename.mat|\n", "|load *filename*|loads the contents of filename into current workspace; the file can be a binary (.mat) file or an ASCII file|\n", "|!|the ! preceding any unix command causes the unix command to be executed from matlab|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Examples" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% single-line comment\n", "\n", "%{\n", " multiple-line\n", " comments\n", "%}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 6\n" ] } ], "source": [ "% the semicolon suppresses output\n", "a = 4;\n", "b = 2;\n", "ans = a + b\n", "% note that only the result for ans is shown below" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**help** — use *help* to lookup command usage, similar to 'man' in terminal" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CLC Clear command window.\n", " CLC clears the command window and homes the cursor.\n", " \n", " See also HOME.\n", "\n", " Reference page in Doc Center\n", " doc clc\n" ] } ], "source": [ "% help example\n", "help clc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**doc** — use *doc* to open a verbose help-document that includes examples" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% doc example\n", "doc clc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**clc** — clears the entire command window; workspace and command history remain intact" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% clc example\n", "clc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**clear** — clears selected variables/answers (or all) from the workspace" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% clear example\n", "clear x % clears the variable x\n", "clear all % clears entire workspace " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* to clear Command History window, click down-arrow and select *Clear Command History*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**home** — moves insertion point to top of command window while retianing previous commands" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% home example\n", "home" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**who** — displays the current variables in the workspace" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "% who example\n", "who" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**whos** — verbose display of variables" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ "% whos example\n", "whos" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a =\n", "\n", " 4\n", "\n", "\n", "b =\n", "\n", " 2\n", "\n", "\n", "ans =\n", "\n", " 6\n" ] } ], "source": [ "% output is 'loose' by default, meaning there is liberal line-spacing\n", "a = 4\n", "b= 2\n", "a + b" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a =\n", " 4\n", "b =\n", " 2\n", "ans =\n", " 6\n" ] } ], "source": [ "% to change output to a tighter format\n", "format compact\n", "a\n", "b\n", "a+b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Common Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Operator|Result|\n", "|---|\n", "|+|addition|\n", "|-|subtraction|\n", "|\\*|scalar/matrix multiplication|\n", "|.\\*|array multiplication|\n", "|/|right division (a/b means a $\\div$ b)|\n", "|\\|left division (a\\b means b $\\div$ a)|\n", "|.\\|array left division|\n", "|./|array right division|\n", "|^|scalar/matrix exponentiation|\n", "|.^|array exponentiation|\n", "|:|generates regularly spaced elements; represents an entire row/column|\n", "|()|enclose function args and array indices; override precedence|\n", "|[]|enclose array elements|\n", "|,|separate statements and elements in a row|\n", "|;|separate columns and suppress output|\n", "|%|comment|\n", "|'|transpose|\n", "|=|assignment|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Order of Precedence" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. parentheses\n", "2. exponentiation, left to right\n", "3. multiplication and division, left right\n", "4. addition and subtraction, left right" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running Scripts\n", "There are 2 ways to do this. Given a script named *test_script.m*:\n", "1. Double-click the script in the file browser on left-hand side\n", " * a separate script editor window appears, where it can be edited and run\n", "2. Call the script directly from the command window\n", " * the entire contents of the script is executed in command window" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x =\n", " 2\n", "y =\n", " 4\n" ] } ], "source": [ "% running script in command window\n", "test_script" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 2\n", "ans =\n", " 0.5000\n" ] } ], "source": [ "% differences in forward and back slash with division\n", "a = 4;\n", "b = 2;\n", "a/b % a is divided by b\n", "a\\b % b is divided by a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with Symbolics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**sym(**expression**)** — the expression maintains its symbolic representation" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 0.5000\n", "ans =\n", "1/2\n" ] } ], "source": [ "% normal division\n", "2/4\n", "\n", "% sym() example\n", "sym(2/4)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "9/10\n" ] } ], "source": [ "% casting just ONE expression will cause the result to be symbolic\n", "sym(1/2) + 2/5" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "9/10\n" ] } ], "source": [ "% conversely, the entire computation can be put in\n", "sym(1/2 + 2/5)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 0.9000\n" ] } ], "source": [ "% a symbolic result can be casted back to double\n", "double(ans)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables can be assigned as symbolics:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x =\n", "x\n" ] } ], "source": [ "x = sym('x')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x =\n", "1/2\n" ] } ], "source": [ "x = sym(1/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**syms** — declare variable(s) as symbolic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*NOTE: the syms command is currently not working in Jupyter...*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Symbolic Calculations" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "area =\n", "(9*pi)/16\n" ] } ], "source": [ "% NOTE: the syms a b c method is not working in Jupyter...\n", "% using syms, we would write: syms x\n", "% the alternative is to write: x = sym('x')\n", "r = sym(3/4);\n", "area = sym('area');\n", "area = pi * r^2" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9 pi\n", "----\n", " 16\n" ] } ], "source": [ "% we can make the output better looking by using pretty command\n", "pretty(area)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "(x^5 - z^3 + 742*y)/(a^2 + b^2 - c^2)\n" ] } ], "source": [ "% a more elaborate scenario:\n", "a=sym('a');b=sym('b');c=sym('c');x=sym('x');y=sym('y');z=sym('z');\n", "sym(x^5+742*y-z^(3))/sym(a^2+b^2-c^2)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 3\n", "x - z + 742 y\n", "---------------\n", " 2 2 2\n", " a + b - c\n" ] } ], "source": [ "% this is a little better...\n", "pretty(ans)" ] } ], "metadata": { "kernelspec": { "display_name": "Matlab", "language": "matlab", "name": "matlab" }, "language_info": { "codemirror_mode": "octave", "file_extension": ".m", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-matlab", "name": "matlab", "version": "0.8.0" } }, "nbformat": 4, "nbformat_minor": 0 }