Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009.

Similar presentations


Presentation on theme: "General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009."— Presentation transcript:

1 General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009

2 Lecture Overview Review Using Matlab remotely Conditions ◦ If statement Loops ◦ For loops

3 Review Computer Science ◦ Data ◦ Operations Matlab functions

4 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

5 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

6 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

7 Quiz!

8 Using Matlab Remotely Mac users: ◦ You already have an X-Windows environment PC users: ◦ You must setup Cygwin-X ◦ Download Cygwin setup.exe from: http://www.cygwin.com/ ◦ Run setup.exe and select additional packages for:  xauth  xinit  openssh

9 Running Matlab Remotely Once the X-Windows environment is setup, open a terminal window and login to strauss: ◦ ssh -Y yourusername@strauss.udel.edu Now start Matlab: ◦ matlab & What does the & do?

10 Conditions When you want to make a decision based on data “If traffic light is red, stop”

11 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”

12 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”

13 Conditions If statement Many others (will talk about later)

14 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

15 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

16 IF Statements in Matlab > x = 8 > if (x == 5) 200 elseif (x == 4) 300 else 400 end Will return: > 400

17 Condition operators equals ◦ == not equals ◦ ~= greater than ◦ > ◦ >= less than ◦ < ◦ <=

18 Matlab Loops Loops execute blocks of code repeatedly There are two types of loops ◦ For loops ◦ While loops (later in the course)

19 For Loops Used when you know how many times code is to be executed. Syntax for = : :

20 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

21 Example Problem I want to find the average # of widgets sold in 4 days Day# of widgets sold 115 222 320 418 Widget(1) = 15 Widget(2) = 22 Widget(3) = 20 Widget(4) = 18 Avg = (Widget(1) + Widget(2) + Widget(3) + Widget(4)) / 4

22 Example Problem I want to find the average # of widgets sold in 4 days Day# of widgets sold 115 222 320 418 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!

23 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;

24 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

25 Putting For loops and Ifs together for j = 1:10 if (j > 5) disp(j); end Will print what?

26 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

27 Lab02 Practice some unix commands Matlab script file sumIntsTest.m ◦ an example of a “unit test” Create new functions using loops/condition statements


Download ppt "General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009."

Similar presentations


Ads by Google