250 likes | 326 Views
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).
E N D
General Computer Science for EngineersCISC 106Lecture 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) • ssh yourusername@strauss.udel.edu • requires an ssh program such as PuTTY • see course website for installation details • at prompt type: matlab -nodesktop
Using Matlab Remotely (GUI) • 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
Running Matlab Remotely • Once the X-Windows environment is setup, open a terminal window and login to strauss: • ssh -c arcfour,blowfish-cbc -YC yourusername@strauss.udel.edu • Now start Matlab: • matlab & • What does the & do?
Emacs • To 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
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
Function documentation • Contract • Description • Examples
Sample 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;
Sample function circleArea.m Contract %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;
Write one for function ringArea.m • Contract • Description • Examples
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 • 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
Traffic Light Example function out = trafficLight(currentColor) if (currentColor == 'R') out = 'stop'; elseif (currentColor == 'Y') out = 'slow'; else out = 'go'; end
Condition operators • equals • == • not equals • ~= • greater than • > • >= • less than • < • <=
Simple Input/Output fav = input(‘Enter your favorite number\n’); if (fav >= 0) disp(‘You like positive numbers’); else disp(‘You like negative numbers’); end fprintf(‘Your favorite number is %d’, fav);
Lab01 • Practice some unix commands • Matlab file sumIntsTest.m • an example of a “unit test” • Create new functions