Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering 1
Introduction 2
Chapter l Starting with MATLAB This chapter describes: MATLAB windows Command Window in detail How to do basic arithmetic Create basic variables Introductory script files 3
Current Folder Window Starting MATLAB, MATLAB Windows Workspace Window Command History Window Command Window 4
MATLAB windows Window Command Window Figure Window Purpose Main window, enters variables, runs programs. Contains output from graphic commands. Editor Window Help Window Command History Window Workspace Window Current Folder Window Creates and debugs script and function files. Provides help information. Logs commands entered in the Command Window. Provides information about the variables that are stored. Shows the files in the current folder. 5
Command Window: The Command Window is MATLAB 's main window and opens when MATLAB is started. Figure Window opens automatically after any command that draws a graph. 6
Editor Window: Use Editor Window to write and debug MATLAB scripts. Open with edit command. 7
Help Window: Get Help Window by clicking on Help icon (question mark) in tool strip 8
Command Window is MATLAB s main window. Use it to: Execute commands Open other windows Run programs that you ve written 9
Basic procedure 1. At prompt (>>), type in MATLAB command 2. Press ENTER key 3. MATLAB displays result in Command Window, followed by a prompt 4. Repeat from step 1 10
Notes on Command Window To start a command, make sure cursor is next to prompt MATLAB won t respond until you press ENTER It then executes only last command Commands before last one may still be visible, but MATLAB doesn t execute them Can type several commands in same line by putting a comma between commands Hard to read, so don t do this often If command too long to fit on line, can continue to next line by typing ellipsis (3 periods, i.e., ) and then pressing ENTER 11
Notes on Command Window When cursor is in bottom command line: key moves cursor one character to left key moves cursor one character to right key recalls preceding command key recalls command that follows one being displayed. 12
Notes on Command Window PAGE-UP key moves up to previous commands in a window-size at a time. PAGE-DOWN key moves down to previous commands in a window-size at a time. BACKSPACE key deletes character to left of cursor. DELETE key deletes character to right of cursor. 13
Notes on Command Window To quickly execute a previous command but with minor changes. 1. Recall command with up- and down-arrow keys. 2. Use left- and right-arrow keys to move to characters to be altered. 3. Use BACKSPACE or DELETE to remove old character, then type new character. 4. Press ENTER to execute modified command. 14
Semicolon (;) Notes on Command Window When typed at end of command, suppresses output. (Only prompt displayed at next line) Useful for preventing display of large outputs Percent sign(%) When typed at beginning of line, MATLAB treats line as a comment and doesn t execute line. clc command Clears Command Window display Up and down arrows still bring back previous commands 15
Command History Window Command History Window Shows previous commands, including ones from previous MATLAB sessions Double-clicking on command puts it in Command Window and executes it Can drag command to Command Window, make changes in command, then execute it To clear one or more commands, select the lines to delete, right click, choose Delete Selection To clear entire history, right click, select Clear Command History 16
Arithmetic in MATLAB Symbols for arithmetic are: Operation Symbol Example Addition + 5 + 3 Subtraction 5 3 Multiplication * 5 * 3 Right division / 5 / 3 Left division \ 5 \ 3 = 3 / 5 Exponentiation ^ 5 ^ 3 (means 5 3 = 125) 17
Order in which MATLAB does arithmetic Precedence First Second Third Fourth Mathematical Operation Parentheses. For nested parentheses, the innermost are executed first. Exponentiation. Multiplication, division (equal precedence). Addition and subtraction. 18
Using Matlab as Calculator 19
Using Matlab as Calculator 20
1.4 display Formats Can control display of numbers with format command Once enter command, format stays the same until another format command. Default format is fixed point with four digits to right of decimal point (called short) Format only affects display of numbers. MATLAB always computes and saves numbers in full precision 21
Display formats 22
Display formats 23
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS 24
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS 25
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS 26
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS 27
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS 28
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS 29
1.6 DEFINING SCALAR VARIABLES A variable is a name made of a letter or a combination of several letters (and digits) that is assigned a numerical value. Once a variable is assigned a numerical value, it can be used in mathematical expressions, in functions, and in any MATLAB statements and commands. A variable is actually a name of a memory location. The Assignment Operator In MATLAB the = sign is called the assignment operator. The assignment operator assigns a value to a variable. Variable_ name = A numerical value, or a computable expression 30
1.6 DEFINING SCALAR VARIABLES 31
1.6 DEFINING SCALAR VARIABLES 32
1.6 DEFINING SCALAR VARIABLES 33
1.6 DEFINING SCALAR VARIABLES A variable that already exists can be reassigned a new value: 34
1.6 DEFINING SCALAR VARIABLES Once a variable is defined it can be used as an argument in functions. For example: 35
1.8 SCRIPT FILES So far all the commands were typed in the Command Window and were executed when the Enter key was pressed. A different (better) way of executing commands with MA TLAB is first to create a file with a list of commands (program), save it, and then run (execute) the file. When the file runs, the commands it contains are executed in the order that they are listed. Files that are used for this purpose are called script files. A script file is a sequence ofmatlab commands, also called a program. Using a script file is convenient because it can be edited (corrected or otherwise changed) and executed many times. Script files are also called M-files because the extension.m is used when they are saved. 36
1.8 SCRIPT FILES 37
1.9 EXAMPLES OF MATLAB APPLICATIONS Sample Problem 1-1: Trigonometric identity 38
1.9 EXAMPLES OF MATLAB APPLICATIONS Sample Problem 1-2: Geometry and trigonometry Four circles are placed as shown in the figure. At each point where two circles are in contact, they are tangent to each other. Determine the distance between the centers C2 and C4. The radii of the circles are: R1 = 16mm, R2 = 6.5mm, R3 = 12 mm, and R4 = 9.5mm. 39
1.9 EXAMPLES OF MATLAB APPLICATIONS Sample Problem 1-2: Geometry and trigonometry 40
1.9 EXAMPLES OF MATLAB APPLICATIONS Sample Problem 1-2: Geometry and trigonometry 41
1.9 EXAMPLES OF MATLAB APPLICATIONS Sample Problem 1-2: Geometry and trigonometry 42