%--------------------------------------------------------------------- % CS 100, Summer 2001 % Thursday, 7/5 % Lecture 7 % Author: David Schwartz, dis@cs.cornell.edu %--------------------------------------------------------------------- % This file will run in MATLAB! % To run it, do: % >> addpath % >> matlab1 %---------------------------------------------------------------------- % Notation: % + The percent symbol (%) means that MATLAB treats everything that % follows as a comment (ignored). % + Dollar signs ($) around a word means something you type to MATLAB % + menus: "->" means selecting a submenu % example) File->Set Path means "click on File menu then click % on Set Path submenu" % + you might wish to uncomment one of the following two lines: % more on % turns pager on % more off % turns pager off %--------------------------------------------------------------------- % Programming in MATLAB (Matrix Laboratory) % + two places to put MATLAB statements: % - Command Window (workspace) % - Edit Window (M-File) % % Command Window % + MATLAB's "main" window % - enter commands at prompt (>>) % - type a command and then press Return or Enter % - this is the "brain" % + carves up memory for programs % + acts upon commands and lines of code % + The "brain" is called the MATLAB workspace or base workspace: % - processes and evaluates commands % - remembers variables and commands you have entered % + key strokes for short cuts: % - recalling commands: up and down arrow keys % - editing commands: left and right arrow keys, Backspace, % Del, Home % - interrupting evaluation: Control-C (hold Ctr key and press c) %--------------------------------------------------------------------- % + "Help" commands % helpinfo % very general help (help on ways to get help) % help % get help on a command % helpwin % window contain help % helpdesk % more extensive HTML help browser % lookfor % search for keword % which % look for commands and files % demo % MATLAB demonstration % + Command options % - enter $help$ where is the name of a command % - many commands have options that modify the command's behavior % - example) more more on more off echo on echo off % - most commands here have options not shown! pause clc %---------------------------------------------------------------------- % + Handy commands: % more on % turn on pager (spacebar) % clc % clear screen % clear % clear variable assignments % who % check current vars (see also File->Show Workspace Menu) % whos % long form of $who$ % format % set output prefs (see also File->Preferences menu) % computer % type of computer % version % version of MATLAB currently used % ver % more explicit than $version$ % diary % save results of current session %--------------------------------------------------------------------- %Language elements: pause clc echo on % Examples of tokens and expressions %============% % Separators % %============% % blank spaces % you may supply as many as you wish between language elements % do not separate within a command: example) he lp % example that works: % help help % lots of spaces inserted and no change in behavior pause % special clc %==========% % Comments % %==========% % MATLAB ignores anything following a % sign on the SAME line pause %=========% % Numbers % %=========% 1 % integer (whole number) 1.1 % floating point number (decimal) 1e2 % floating point number (scientific notation) 27i % imaginary 27j % imaginary pause %===========% % Operators % %===========% 1+2 % add 1-2 % subtract 1*2 % times 1/2 % divide 1^2 % power pause % Operator precedence % Operator associativity % ---------------------- % mostly the same rules % More information % ---------------- % help arith % help ops % help slash % help relop %===========% % Variables % %===========% % token that stores another quantity in memory % acts like a placeholder for a value % example) $x = 1$ stores the value of $1$ in variable $x$ % rules: % + case-sensitive (for example, $A$ does not mean $a$!) % + only 31 characters (can go more, but extras are ignored) % + start with a letter % + characters may include digits (0-9) and the underscore (_) % examples) x, x1, x_1 pause % assignments % ----------- % To store a value in a variable, use an assignment statement % Syntax: $var = expression$ % MATLAB evaluates the expression, carves out a portion of memory, and % stores the value of the expression in $var$. % Other features: % + You may not use unassigned varibles. % + The equals sign (=) means ASSIGN in MATLAB, not equals! % + When talking of algorithms, write an assignment with an arrow. % example) "x <- 1" in English means $x = 1$ in MATLAB. pause % More examples of assignments: % x % $x$ is not assigned a value, so MATLAB cannot evaluate x = 1 % assign $1$ to $x$ x = 2 % you change a variable's assignment x % once assigned, you may check a variable's assignment pause % recalling variable values % ------------------------- % You may use assigned variables in expressions. % Until you clear variables, restart MATLAB, or assign new values, % variable values remain in memory as long as your current MATLAB session % keeps running. % example) pause x = 1 % assign 1 to $x$ y = 2 % assign 2 to $y$ z = x+y % evaluate $x + y$ and store the result (3) in $z$ x = 4 % assign $x$ to a new value z % $z$ is still 3 and NOT 4+2 because $z$ % was already assigned BEFORE $x$ changed to 4 % predefined variables % -------------------- % MATLAB comes with useful value already built-in for your convenience! ans % default name for evaluated expressions without assignments pi % numerical pi eps % smallest number that can be added to 1 to make number bigger % than 1 inf % positive infinity NaN % not-a-number (ex: 0/0) nan % not-a-number (ex: 0/0) pause i % imaginary number (square root of -1) j % imaginary number (square root of -1) realmin % smallest usable positive number in MATLAB realmax % smallest usable negative number in MATLAB % You may actually change these values (usually with $eps$) eps=0.1 % Assign $eps$ to a new value (0.1) % To restore a predefined variable's value, see $clear$ below. pause % clearing variables % ------------------ % Sometimes you might wish to clear an assigned variable's value. % You may restart MATLAB with $clear$ clear % you may clear all variables of assignments % To clear just one variable, enter $clear var$: x = 1 % assign 1 to $x$ clear x % unassign $x$ % x % test value of $x$ % Use $clear$ to restore predefined variable values. % You may also enter $clear var$ to clear a specific variable: eps =0.1 % change value of $eps$ clear eps % restore $eps$ eps % see default value (2.2204e-16) pause %=============% % Punctuation % %=============% % comments: % % suppress output: ; % separate statments: , % For a full list, see % help punct pause % examples) 1+1 % Hi! % evaluate 1+1 and ignore the comment a = 1; % assign 1 to $a$ and suppress output a % check the value of $a$ (MATLAB will say 1) a=1, b=2 % you may use a comma to separate statements % on the same input line pause %==========% % Keywords % %==========% % also called reserved words % part of the language % cannot change meaning using assignments % if, end, while, for (and more..) pause %=========% % Strings % %=========% % Strings are collections of individual characters (usually ASCII) % They are not names and not keywords. % Strings are used for processing text files and providing labels. % To create a string surround characters with single forward quotes: 'hello, I am a string!' % enter a string % You may also assign a string to a variable: x = 'blah, blah, blah' % assign a string to $x$ pause % Characters inside strings have numerical ASCII codes. % To find out a string's numerical codes, use $double(string)$: x = 'abc'; % assign a string to $x$ double(x) % determine the string's numerical character codes % Much more to say on this subject also.... pause %===========% % Functions % %===========% % A function is an expression with the syntax $name(args)$, % where $args$ is a sequence of variables separated by commas. % Entering a function tells MATLAB to run another program that performs % the tasks the function represents. % example) sqrt(4) % take the square root of 4 % MATLAB reports that the answer is 2 % You may include a function in an expression. % MATLAB performs the desired tasks in the function. If the function % produces a value (known as returning a value), MATLAB substitutes the % returned value for the function. % example) 1 + sqrt(4) % add 1 to square root of 4 % MATLAB will replace sqrt(4) with 2 and then add 2 to 1 % Some functions in MATLAB take may no arguments: % help % Much more to say on functions.... %-------------------------------------------------------------------