Computer Programming ECIV 2303 Chapter 1 Starting with...

42
Computer Programming ECIV 2303 Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering Chapter 1 Starting with MATLAB 1 Dr. Talal Skaik 2018

Transcript of Computer Programming ECIV 2303 Chapter 1 Starting with...

Introduction

2Dr. Talal Skaik 2018

Starting with MATLABChapter l

This chapter describes: MATLAB windows Command Window in detail How to do basic arithmetic Create basic variables Introductory script files

3Dr. Talal Skaik 2018

Starting MATLAB, MATLAB WindowsC

urr

ent

Fold

er

Win

do

w

Command Window

Workspace Window

Command History Window

4Dr. Talal Skaik 2018

MATLAB windows

Window Purpose

Command Window Main window, enters variables, runs programs.

Figure Window Contains output from graphic commands.

Editor Window Creates and debugs script and function files.

Help Window Provides help information.

Command History Window Logs commands entered in the Command Window.

Workspace Window Provides information about the variables that are stored.

Current Folder Window Shows the files in the current folder.

5Dr. Talal Skaik 2018

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.

6Dr. Talal Skaik 2018

Editor Window: Use Editor Window to write and debug MATLAB scripts. Open with edit command.

7Dr. Talal Skaik 2018

Help Window: Get Help Window by clicking on Help icon (question mark) in tool strip

8Dr. Talal Skaik 2018

Command Window is MATLAB’s main window. Use it to:

Execute commands

Open other windows

Run programs that you’ve written

9Dr. Talal Skaik 2018

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

Basic procedure

10Dr. Talal Skaik 2018

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

Notes on Command Window

11Dr. Talal Skaik 2018

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.

Notes on Command Window

12Dr. Talal Skaik 2018

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.

Notes on Command Window

13Dr. Talal Skaik 2018

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.

Notes on Command Window

14Dr. Talal Skaik 2018

Semicolon (;)

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

Notes on Command Window

15Dr. Talal Skaik 2018

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

Command History Window

16Dr. Talal Skaik 2018

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 53 = 125)

Arithmetic in MATLAB

17Dr. Talal Skaik 2018

Order in which MATLAB does arithmetic

Precedence Mathematical Operation

First Parentheses. For nested parentheses, the innermost are executed first.

Second Exponentiation.

Third Multiplication, division (equal precedence).

Fourth Addition and subtraction.

18Dr. Talal Skaik 2018

Using Matlab as Calculator

19Dr. Talal Skaik 2018

Using Matlab as Calculator

20Dr. Talal Skaik 2018

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

21Dr. Talal Skaik 2018

Display formats

22Dr. Talal Skaik 2018

Display formats

23Dr. Talal Skaik 2018

1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

24Dr. Talal Skaik 2018

1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

25Dr. Talal Skaik 2018

1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

26Dr. Talal Skaik 2018

1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

27Dr. Talal Skaik 2018

1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

28Dr. Talal Skaik 2018

1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

29Dr. Talal Skaik 2018

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

30Dr. Talal Skaik 2018

1.6 DEFINING SCALAR VARIABLES

31Dr. Talal Skaik 2018

1.6 DEFINING SCALAR VARIABLES

32Dr. Talal Skaik 2018

1.6 DEFINING SCALAR VARIABLES

33Dr. Talal Skaik 2018

1.6 DEFINING SCALAR VARIABLES

A variable that already exists can be reassigned a new value:

34Dr. Talal Skaik 2018

1.6 DEFINING SCALAR VARIABLES

Once a variable is defined it can be used as an argument in functions. For example:

35Dr. Talal Skaik 2018

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.

36Dr. Talal Skaik 2018

1.8 SCRIPT FILES

37Dr. Talal Skaik 2018

1.9 EXAMPLES OF MATLAB APPLICATIONS

Sample Problem 1-1: Trigonometric identity

38Dr. Talal Skaik 2018

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.

39Dr. Talal Skaik 2018

1.9 EXAMPLES OF MATLAB APPLICATIONS

Sample Problem 1-2: Geometry and trigonometry

40Dr. Talal Skaik 2018

1.9 EXAMPLES OF MATLAB APPLICATIONS

Sample Problem 1-2: Geometry and trigonometry

41Dr. Talal Skaik 2018

1.9 EXAMPLES OF MATLAB APPLICATIONS

Sample Problem 1-2: Geometry and trigonometry

42Dr. Talal Skaik 2018