25
General Computer Science General Computer Science for Engineers for Engineers CISC 106 CISC 106 Lecture 03 Lecture 03 James Atlas Computer and Information Sciences 9/9/2009

General Computer Science for Engineers CISC 106 Lecture 03

Embed Size (px)

DESCRIPTION

General Computer Science for Engineers CISC 106 Lecture 03. James Atlas Computer and Information Sciences 9/9/2009. Objectives. Use Matlab remotely Use Basic Unix Commands Document Functions Use Conditions If statement Input/Output variables. Using Matlab Remotely (text). - PowerPoint PPT Presentation

Citation preview

Page 1: General Computer Science  for Engineers CISC 106 Lecture 03

General Computer Science General Computer Science for Engineersfor Engineers

CISC 106CISC 106Lecture 03Lecture 03

James AtlasComputer and Information Sciences

9/9/2009

Page 2: General Computer Science  for Engineers CISC 106 Lecture 03

ObjectivesObjectivesUse Matlab remotelyUse Basic Unix CommandsDocument FunctionsUse Conditions

◦If statementInput/Output variables

Page 3: General Computer Science  for Engineers CISC 106 Lecture 03

Using Matlab Remotely (text)ssh [email protected]

◦requires an ssh program such as PuTTY◦see course website for installation details

at prompt type:matlab -nodesktop

Page 4: General Computer Science  for Engineers CISC 106 Lecture 03

Using Matlab Remotely (GUI)Mac users:

◦ You already have an X-Windows environmentPC 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

Page 5: General Computer Science  for Engineers CISC 106 Lecture 03

Running Matlab RemotelyOnce the X-Windows environment is

setup, open a terminal window and login to strauss:◦ssh -c arcfour,blowfish-cbc -YC

[email protected] start Matlab:

◦matlab &

What does the & do?

Page 6: General Computer Science  for Engineers CISC 106 Lecture 03

EmacsEmacsTo start emacs

◦ emacs Graphical version◦ emacs –nw Text version

To open a file◦ emacs <filename>◦ 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

Page 7: General Computer Science  for Engineers CISC 106 Lecture 03

Unix CommandsUnix CommandsWhen 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

Page 8: General Computer Science  for Engineers CISC 106 Lecture 03

Handling filesHandling filescp 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

Page 9: General Computer Science  for Engineers CISC 106 Lecture 03

Function documentationContractDescriptionExamples

Page 10: General Computer Science  for Engineers CISC 106 Lecture 03

Sample function circleArea.mSample function circleArea.m%circleArea(number) -> number

%Authors: James Atlas

%CISC106 Lab Section 45 TA: Scott Ivanka

%Description:

% This function computes the area of a circle

% given the radius.

%Examples:

% circleArea(3) -> 28

% circleArea(5) -> 78

% circleArea(45) -> 6362

function output = circleArea(radius)

output = pi * radius * radius;

Page 11: General Computer Science  for Engineers CISC 106 Lecture 03

Sample function circleArea.mSample function circleArea.m%circleArea(number) -> number

%Authors: James Atlas

%CISC106 Lab Section 45 TA: Scott Ivanka

%Description:

% This function computes the area of a circle

% given the radius.

%Examples:

% circleArea(3) -> 28

% circleArea(5) -> 78

% circleArea(45) -> 6362

function output = circleArea(radius)

output = pi * radius * radius;

Contract

Page 12: General Computer Science  for Engineers CISC 106 Lecture 03

Write one for function Write one for function ringArea.mringArea.mContractDescriptionExamples

Page 13: General Computer Science  for Engineers CISC 106 Lecture 03
Page 14: General Computer Science  for Engineers CISC 106 Lecture 03
Page 15: General Computer Science  for Engineers CISC 106 Lecture 03

ConditionsWhen you want to make a decision

based on data“If traffic light is red, stop”

Page 16: General Computer Science  for Engineers CISC 106 Lecture 03

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

Page 17: General Computer Science  for Engineers CISC 106 Lecture 03

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

Page 18: General Computer Science  for Engineers CISC 106 Lecture 03

ConditionsIf statementOthers (will talk about later)

Page 19: General Computer Science  for Engineers CISC 106 Lecture 03

IF Statements in MatlabIF 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

Page 20: General Computer Science  for Engineers CISC 106 Lecture 03

IF Statements in MatlabIF statements can be used with or

without the ELSEIF and ELSE parts

Page 21: General Computer Science  for Engineers CISC 106 Lecture 03

Traffic Light Examplefunction out = trafficLight(currentColor)

if (currentColor == 'R')

out = 'stop';

elseif (currentColor == 'Y')

out = 'slow';

else

out = 'go';

end

Page 22: General Computer Science  for Engineers CISC 106 Lecture 03

Condition operatorsequals

◦ ==not equals

◦ ~=greater than

◦ >◦ >=

less than◦ <◦ <=

Page 23: General Computer Science  for Engineers CISC 106 Lecture 03
Page 24: General Computer Science  for Engineers CISC 106 Lecture 03

Simple Input/Output

fav = input(‘Enter your favorite number\n’);

if (fav >= 0)disp(‘You like positive numbers’);

elsedisp(‘You like negative numbers’);

endfprintf(‘Your favorite number is %d’, fav);

Page 25: General Computer Science  for Engineers CISC 106 Lecture 03

Lab01Practice some unix commandsMatlab file sumIntsTest.m

◦an example of a “unit test”Create new functions