General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009
Lecture Overview Review Using Matlab remotely Conditions ◦ If statement Loops ◦ For loops
Review Computer Science ◦ Data ◦ Operations Matlab functions
Emacs To start emacs ◦ emacs Graphical version ◦ emacs –nw Text version To open a file ◦ emacs ◦ emacs … then Ctrl-x Ctrl-f ◦ Menu: File then “Open File…” To save file ◦ Ctrl-x Ctrl-s ◦ Menu: File then “Save (current buffer)” Exit ◦ Ctrl-x Ctrl-c
Unix Commands When you log into a UNIX terminal ◦ You are in your home directory. ◦ To see the files in your directory. ls ◦ To make an new folder/directory. mkdir exampledir ◦ To change directories. cd exampledir ◦ To go back one directory. cd.. ◦ To go back to your home directory. cd
Handling files cp file1 file2 ◦ copy file1 and call it file2 mv file1 file2 ◦ move or rename file1 to file2 rm file ◦ remove a file rmdir exampledir ◦ remove a directory cat file ◦ display contents of a file less file ◦ display a file a page at a time
Quiz!
Using Matlab Remotely Mac users: ◦ You already have an X-Windows environment PC users: ◦ You must setup Cygwin-X ◦ Download Cygwin setup.exe from: ◦ Run setup.exe and select additional packages for: xauth xinit openssh
Running Matlab Remotely Once the X-Windows environment is setup, open a terminal window and login to strauss: ◦ ssh -Y Now start Matlab: ◦ matlab & What does the & do?
Conditions When you want to make a decision based on data “If traffic light is red, stop”
Conditions When you want to make a decision based on data “If traffic light is red, stop” ◦ Involves some comparison operator between data and a known value ◦ known value = red ◦ data = current state of the traffic light ◦ comparison operator is “equals”
Conditions When you want to make a decision based on data “If traffic light is red, stop” ◦ Involves some comparison operator between data and a known value ◦ known value = red ◦ data = current state of the traffic light ◦ comparison operator is “equals” “If current state of traffic light equals red, stop”
Conditions If statement Many others (will talk about later)
IF Statements in Matlab IF statements allow program to make choices whether a condition is met or not Basic if statement if expression1 statements1 elseif expression2 statements2 else statements3 end
IF Statements in Matlab IF statements can be used with or without the ELSEIF and ELSE parts If you type: > x = 5 > if (x == 5) 200 end It will return: > 200
IF Statements in Matlab > x = 8 > if (x == 5) 200 elseif (x == 4) 300 else 400 end Will return: > 400
Condition operators equals ◦ == not equals ◦ ~= greater than ◦ > ◦ >= less than ◦ < ◦ <=
Matlab Loops Loops execute blocks of code repeatedly There are two types of loops ◦ For loops ◦ While loops (later in the course)
For Loops Used when you know how many times code is to be executed. Syntax for = : :
For Loops Used when you know how many times code is to be executed. Syntax for = : : Variable is initially the start value At end of iteration variable changes by increment If value is not greater than end the loop runs again. is optional, if not provided will be 1
Example Problem I want to find the average # of widgets sold in 4 days Day# of widgets sold Widget(1) = 15 Widget(2) = 22 Widget(3) = 20 Widget(4) = 18 Avg = (Widget(1) + Widget(2) + Widget(3) + Widget(4)) / 4
Example Problem I want to find the average # of widgets sold in 4 days Day# of widgets sold Widget(1) = 15 Widget(2) = 22 Widget(3) = 20 Widget(4) = 18 Avg = (Widget(1) + Widget(2) + Widget(3) + Widget(4)) / 4 ◦ This is easy for a small number of days. ◦ What if we had a 1000 days? ◦ We can use a for loop!
Example Problem total = 0; for i = 1:1:1000 loop starts at 1 total = total+widget (i);loop increments by 1 end loop ends at 1000 avg = total / i;
A Loop Analogy The mail man/woman executes a loop. If they know the number of deliveries For loop for delivery = start : next_delivery : end deliver_mail(delivery) end
Putting For loops and Ifs together for j = 1:10 if (j > 5) disp(j); end Will print what?
Group Exercise Create a for loop that finds all prime numbers between 1 and 100 ◦ pseudo-code ◦ hint: remember the math function modulus which gives the remainder of a division operation In Matlab you can do mod(x,y) mod(5,3) == 2
Lab02 Practice some unix commands Matlab script file sumIntsTest.m ◦ an example of a “unit test” Create new functions using loops/condition statements