%------------------------------------------------------------------------------ % 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 on % turn on pager clc % clear screen %------------------------------------------------------------------------------ % (2) Statements % % Tasks: % % Complete these comments and write inside lab2.m: % % A written language uses an __________________ to create its words. % A computer language uses a __________________ for its words. % A computer language's "words" are really called __________ . % To communicate in a written language, you combine words to % form _________________. These ________________ are called % _________________ 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: % Multiply 1 and 2 and suppress output: % + 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: % Show how 1*(2*3) is identical to (1*2)*3: %------------------------------------------------------------------------------ % (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: %------------------------------------------------------------------------------ % (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: % Clear variable y: % Demonstrate the variable y is cleared: % Clear all variables: % Demonstrate "weakly typed" by assigning a string, an integer, % a floating-point number, and an array to the variable x: %------------------------------------------------------------------------------ % (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: % Generate a random number: % Find the square root of a randomly generated number. Hint: NEST your % functions by making the result one function the input of the other: % Find the floor (integer portion) of the number 7.7. Hint: See $help floor$: % Find the floor of a randomly generated number: % Find a random number between 1 and 10: % Find a random INTEGER between 0 and 2: % Find a random INTEGER between 1 and 2: % Find a random INTEGER between two integers (MIN and MAX). Give MIN and % MAX initial values to test your code: %------------------------------------------------------------------------------ % (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: % Prompt the user to enter their age: % Prompt the user to enter their age and store the result in variable $age$: % 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: % Prompt the user for their age and output the result in this format: % Your age is 82. % Done! %------------------------------------------------------------------------------