%-------------------------------------% % Lab 2 : MATLAB Statements % % Date: 9/10/2002 % % % % Gun Srijuntongsiri % % gs61 % % ?????? % %-------------------------------------% %------------------------------------------------------------------------------ % CS99 Lab 2 % 9/10/2002 % MATLAB Statements %------------------------------------------------------------------------------ % (0) Read these objectives of today's lab: % % + practice with MATLAB tokens (see matlabtokens.txt) % + learn about general MATLAB statements % + learn about input/output (I/O) statements %------------------------------------------------------------------------------ % (1) What to hand in: % % + Create a file called lab2.m. % + Write a comment block in the required format at the top % (see Syllabus) % + Complete the *entire* document in your file. % + Run each command in the command window to learn about it's behavior. % - for commands that are deliberate errors, comment out the % erroneous command after testing it % + When you're done, this file will be a runnable MATLAB file! % + Due: submit by e-mail to Gun no later than noon on Fri 9/13. % % Handy things to set up! clear all % clear all variable assignments more off % turn off pager clc % clear screen %------------------------------------------------------------------------------ % (2) Statements % % Tasks: % % Complete these comments and write inside lab2.m: % % A written language uses an __alphabet__ to create its words. % A computer language uses a __character__ for its words. % A computer language's "words" are really called __tokens__ . % To communicate in a written language, you combine words to % form __sentences__. These __sentences__ are called % __statements__ in a computer language. % Each statement is essentially a command or instruction that you give % to the computer. %------------------------------------------------------------------------------ % (3) Empty statements % % + An EMPTY STATEMENT is the simplest form of a command. % + MATLAB automatically succeeds at an empty statement. % % Tasks: Show two examples (pressing Return and a semicolon): % Return: % Semicolon: ; %------------------------------------------------------------------------------ % (2) Expression % % + MATLAB evaluates all arithmetic/logic operations % + MATLAB reports the result as output % + The result us automatically stored in variable $ans$ if you do not % specify something else % + You can suppress output with semicolon (;) % + Want list of arithmetic operators? see $help arith$ % + Want list of logic operators? see $help relop$ % % Tasks: % Add 1 and 1 and show output: 1+1 % Multiply 1 and 2 and suppress output: 1*2; % + operator precedence: % - some operators come before others: % 1+2*3 means 1+(2*3) automatically % - can change behavior with parentheses % (1+2)*3 means just that! % + operator associativity % - operators have a "direction" when confronting themselves.... % 1-2-3 means (1-2)-3 because minus associates left % % Tasks: % Show how 1/(2+3) differs from 1+2/3: 1/(2+3) 1+2/3 % alternatively, 1/(2+3)==1+2/3 % will output 1 if equal, 0 if not. % Show how 1*(2*3) is identical to (1*2)*3: 1*(2*3) (1*2)*3 % alternatively, 1*(2*3)==(1*2)*3 % will output 1 if equal, 0 if not. %------------------------------------------------------------------------------ % (3) Declaration % % + in other languages, need to tell MATLAB about the data types % + MATLAB is weakly typed! % + everything is an array % - will demonstrate later... % - if curious, try this: x = 11:15, x(3), x + 1, x(1:5) + 1 % % Tasks: % Show how entering an unassigned variable causes an error: % k %------------------------------------------------------------------------------ % (4) Assignment % % + you *will* want to reuse values % + store results of expressions in variables % + syntax: $var=value$ % + think in terms of "variable gets value" % or pseudocode: variable<-value % + to see a variable's value, enter variable at the prompt % + cannot use a variable until it's assigned % + variables assigned in the command window are GLOBAL % (known until you reassign or clear) % + examples) % clear % removes all assignments % x % causes an error because x is not assigned yet % y+1 % same problem: y isn't assigned yet! % x = 1 % x gets the value 1 % x+2 % MATLAB knows that x means 1, so x+1 produces 3 % clear x % remove the value in x % x % uh oh: cannot use x now because value is removed % % Tasks: % Assign the value 10 to variable y: y = 10; % Clear variable y: clear y % Demonstrate the variable y is cleared: % y % which yields "Undefined..." error. % alternatively, who % lists all existing variables. So you shouldn't see y in the list % or, who y % lists all variables named y, which are none in this case. % Clear all variables: clear % Demonstrate "weakly typed" by assigning a string, an integer, % a floating-point number, and an array to the variable x: x = 'cs99' x = 12 x = 3.14 x = [1 3 5 6 -2] %------------------------------------------------------------------------------ % (5) Function call % % + exectute a programmed procedure % + functions are M-Files that you can "pass" values % - you may access builtin functions or write your own! % - good for commonly needed evaluations % + syntax: name(arguments) % - name is the name of the function % - arguments are values that you pass to the function % - sometimes functions have no arguments % (and thus do not need the parentheses) % + examples) % % sqrt(4) % rand % % + some functions generate values called RETURN VALUES % + the return values replace the function's value in the expression % + example) % % x = 1 + sqrt(4) % produces 3 % % Tasks: % Find the square root of 10 and multiply the result by 2: sqrt(10)*2 % Generate a random number: rand % Find the square root of a randomly generated number. Hint: NEST your % functions by making the result one function the input of the other: sqrt(rand) % Find the floor (integer portion) of the number 7.7. Hint: See $help floor$: floor(7.7) % Find the floor of a randomly generated number: floor(rand) % Find a random number between 1 and 10: rand*9+1 % Find a random INTEGER between 0 and 2: floor(rand*3) % say x = rand*3. So if x is in (0,1), floor(x)=0, % if x is in (1,2), floor(x)=1, % if x is in (2,3), floor(x)=2. % Find a random INTEGER between 1 and 2: floor(rand*2)+1 % Find a random INTEGER between two integers (MIN and MAX). Give MIN and % MAX initial values to test your code: min = -4 max = 12 floor(rand*(max-min+1)+min) % Note that min is inside floor, to correct % cases when min < 0. %------------------------------------------------------------------------------ % (6) I/O statements (examples of functions) % % + input: % - keyboard: the program prompts the user % - file: the program "sucks in" a file with needed information % + output: % - keyboard, file, printer, etc... % % Simple input: % + see $help input$ % + example session: % % >> input('Enter anything: ') % Enter anything: 3 % ans = % 3 % % Notes on $input$: % + $input$ requires a value! % + a correctly entered value is stored in special variable $ans$ % ($ans$ is most recently entered value) % + To store the result of an input, use an assignment with this syntax: % var = input('message: '); % % Tasks: % Prompt the user to enter their name as a string: input('What is your name? ','s'); % Prompt the user to enter their age: input('How old are you? '); % Prompt the user to enter their age and store the result in variable $age$: age = input('How old are you? '); % Simple output: % + see $help disp$ % + $disp$ requires a single string or array of strings % - an array is a collection of values % - for now, use syntax [element,element,element,...] % - example) x = ['s','abc','qwerty'] % - to print a number with $disp$, you need to convert it to a string % with $num2str$ % - example) x = 1; disp(num2str(x)); % % + $disp$ examples: % % >> disp('hi!') % >> x = 1; disp(['The value of x is: ',num2str(x)] % % Tasks: % Prompt the user for their favorite color and output the result: color = input('What is your favorite color? ','s'); disp(['Your favorite color is ',color,'.']); % Prompt the user for their age and output the result in this format: % Your age is 82. age = input('How old are you? '); disp(['Your age is ', num2str(age), '.']); % Done! %------------------------------------------------------------------------------