34
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction P. 1 Agenda MATLAB Overview The Command Window Simple Script Files

ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

Embed Size (px)

Citation preview

Page 1: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

College of EngineeringEngineering Education Innovation Center

First-Year Engineering Program

MAT - Introduction

P. 1

AgendaMATLAB Overview

The Command WindowSimple Script Files

Page 2: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

MATLAB Quizzes• Note MATLAB quizzes are assigned in

the daily assignment list

• Material covered on the quizzes will include the most recent preparation materials plus any previous lectures.

• It is ok to reference the book or slides while taking the quiz.

P. 2

Page 3: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

Slide Format

• Note the upper right corner indicate pages from the MATLAB book which correspond with the material covered on the slide.

P. 3

IntroductionTextbook

• It is very important to review the chapters in the MATLAB book prior to class. They contain complementary information and valuable examples.

Page 4: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 4

MATLAB is a powerful program for numerical computations, plotting and programming.

Use it as a calculator.

Define variables and use them in calculations.

Use built-in functions.

Plot graphs.

Write and run computer programs.

Perform symbolic mathematics.

1-3

Page 5: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 5

MATLAB’s Default Windows

Page 6: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 6

(The Command Window opens when MATLAB is started)Command window

Command prompt

8-9

Page 7: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 7

(The Figure Window opens automatically by the plot() command)

7

Figure window

Page 8: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 8

(The Editor Window is opened from the file menu in the Command Window)

7

Editor window

Page 9: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 9

(The Help Window can be opened from the Help menu of any of MATLAB windows)

8

Help window

Page 10: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 10

WORKING IN THE COMMAND WINDOW

8-9

•Place the cursor at the command prompt (>>) to enter a command

•Press the ‘Enter’ key to execute the command (Line-at-a-time-execution)

•To correct a previously typed command press the up arrow key ( ) to recall the desired command

Page 11: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

MATLAB’s Working Directory

P. 11

When you first start MATLAB it is a good practice to changethe working directory to your N: drive or USB device.

You can use the browse icon to locate the directory you want to use

Page 12: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 12

ARITHMETIC OPERATIONS WITH SCALARS

10

Operation

Addition

Subtraction

Multiplication

Right Division

Left Division

Exponentiation

Symbol

+

-

*

/

\

^

Example

5 + 3

5 - 3

5 * 3

5 / 3

5 \ 3 = 3 / 5

5 ^ 3

Page 13: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 13

ORDER OF PRECEDENCE

•Higher-precedence operations are executed before lower-precedence operations (like Excel or programmable calculators).

•If two operations have the same precedence, then the expression is executed from left to right.

PRECEDENCE OPERATION

First Parentheses (innermost pair first)

Second Exponentiation.

Third Multiplication and division

Fourth Addition and subtraction

10

Page 14: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 14

MATLAB AS A CALCULATORUsing numbers:

8 / 2 is executed first

>> 7 + 8 / 2

ans =

11

Type and press Enter

Computer response

7 + 8 is executed first

>> (7 + 8) / 2

ans =

7.5000

Type and press Enter

Computer response

27 ^ 1 and 32 ^ 0.2 are executed first/ is executed next+ is executed last

>> 27 ^ 1 / 3 + 32 ^ 0.2

ans =

11

>> 27 ^ (1 / 3) + 32 ^ 0.2

ans =

5

1 / 3 is executed first^ is executed next+ is executed last

11

Page 15: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 15

DEFINING VARIABLESA variable becomes defined when a value is assigned to itusing the assignment operator, = .

Once a variable is defined, the computer remembers and stores its value. The variable can then be

used in further calculations, used to create new variables, passed into functions.

The value of a variable can be modified at any time.

>> a = 8

a =

8

>> B = 12

B =

12

Type and press Enter

Computer response

16-17

>> a + B

ans =

20

>> a / B

ans =

0.6667

>> d = a * B

d =

96

>> sqrt(d)

ans =

9.7980

Page 16: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 16

THE ASSIGNMENT OPERATOR

>> x = 3

x =

3

Note: In algebra this expression (x=x+5) is invalid since it implies: 0 = 5.

>> x = x + 5

x =

8

MATLAB assigns a new value to x, which is the old value (3) plus 5.

15-18

The statement: ‘x + 4 = 30’Is not valid. Matlab does not solve for x

>> x + 4 =30

??? x + 4 = 30

But, the statement ‘x = 30 – 4’ is valid(the number 26 is assigned to x)

>> x = 30 - 4

x =

26

Assigns the value 3 to variable x.

Page 17: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 17

RULES ABOUT VARIABLES NAMES Can be up to 63 characters long.

Must begin with a letter.

Can contain letters, digits, and the underscore character (no spaces).

MATLAB is case sensitive; it distinguishes between UPPERCASE and lowercase letters.

For example, 'number‘, ‘NUMBER’, and 'Number' are NOT the same variable.

It is good practice to make the variable name descriptive

For example, ‘avg_score’ or ‘avgScore’ instead of ‘a’

18

Page 18: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 18

RULES ABOUT VARIABLES NAMES

Avoid naming variables names that are usedby MATLAB

Names of MATLAB functions:

exp, sin, cos, sqrt, max, min, sum, etc.

Names of MATLAB-defined variables:

π (accurate to 2.2 * 10 ^ -16)

(the solution from the most recent calculation)

Etc.

Note that e is not a MATLAB-defined variable (use exp)

18

pi

ans

Page 19: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

P. 19

MATLAB BUILT-IN MATH FUNCTIONS

13-15

exp(x) exponential (ex)

log(x) natural logarithm (log base “e”, written ln in math expressions)

log10(x) base 10 logarithm (log in math expressions)

sqrt(x) square root

abs(x) absolute value

NOTE: For trigonometric functions, x is in radianssin(x), cos(x), asin(x), acos(x), tan(x), cot(x)

For each trig function there is an equivalent, e.g. sind(x) where x is in degrees

Page 20: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

Built-in Functions

• Here are some examples of using MATLAB built-in functions:

P. 20

>> sin(pi / 4)

ans =

0.7071

>> sqrt(169)

ans =

13

>> log10(10000)

ans =

4

MATLAB has hundreds of built-in functions (this will be discussed in future lectures). More functions are listed in section 1.5 of the text.

Page 21: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

Example• Calculate the value of this function for x=30

degrees:

cos2x sinx + tan(x/2) / √3x

In the command window:

>>x=30 (Assigns the value 30 to x)

>>x=x*pi/180 (Converts degrees to radians)

>>cos(x)^2*sin(x)+tan(x/2)/sqrt(3*x)

P. 21

Page 22: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

Script files

• What if you wanted do a lengthy calculation many times. What’s the chance of a typo causing an error?

• MATLAB allows you to save any series of commands in a file and will execute the commands sequentially as if they were typed directly into the command window.

• These files are called script files.

Page 23: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

Example with Script file

• Suppose we created a script file called trig_calc which contained the following:

Line 1: x=x*pi/180

Line 2: cos(x)^2*sin(x)+tan(x/2)/sqrt(3*x)

• Then we would only have to type the following into the command window:

>>x=30

>>trig_calc

The result would be displayed in the command window

Page 24: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

SCRIPT FILE

Using a script file is convenient because it can be…

edited (corrected and/or changed)

executed many times with reliable results

saved, then opened at a later date

sent to others to use

Script files are also called M-files because the extension .m is used when they are saved.

77-78

Page 25: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

THE M-FILE EDITOR/DEBUGGER WINDOW

78-79

The commands of the script file are typed line-by-line.

The lines are numbered automatically

A new line starts when the Enter key is pressed.

The Run Icon

Page 26: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

EXAMPLE OF A SCRIPT FILE

% Example of a script file

% This file calculates the square of x

x = sin(pi / 4)

y = x ^ 2Define x

Store result in y

Typing ‘%' at the beginning of a line designates

the line as a comment

comments are not executed

79

Page 27: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

SAVING A SCRIPT FILE

Once the script file is completed, it must be saved. In our class use Save As to your N:\ or USB drive. This should be the same as the working directory you specified when you started MATLAB.

The name of the script file follows the rules for names of variables in MATLAB Can be up to 63 characters long. Must begin with a letter. Can only contain letters, digits, and the

underscore character (no spaces). Case sensitive.

78

Page 28: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

RUNNING A SCRIPT FILE

A script file can be executed from the Editor Window by clicking on the Run icon (this saves the m-file before running).

A script file can also be executed from the Command Window by typing its name and pressing Enter.

79-81

Refer pp. 79-81 in the book about other ways to change the Current Directory or the Search Path.

Page 29: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

EXAMPLE OF RUNNING A SCRIPT FILE

80-81

% Example of a script file

% This file calculates the square of x

x = sin(pi / 4)

y = x ^ 2

Script file:

>> Test1

x = 0.7071

y = 0.5000

>>

Command window:Enter script file name

MATLAB response

Save script file as Test1

Page 30: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

SOME USEFUL COMMANDS

10,19

Clears the command window

Clears all variables from memory

Clears only variables x, y and z

Careful, this command only clears x

>> clc

>> clear

>> clear x y z

>> clear x, y, z

>> x = 5

x =

5

>> x = 5;

>>

Typing a semi-colon (;) at the end

of a line of input prevents the

output from being displayed in the

command window

Page 31: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

OUTPUT FROM A SCRIPT FILE

When a script file runs, output that is generated is

displayed in the Command Window

Output is displayed automatically if a statement does

not end with a semicolon. Many times you don’t want all

outputs to be displayed in the command window.

Output can also be displayed intentionally by using the

disp() command

84-87

Page 32: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

THE disp() COMMAND

disp(A)

disp(‘text’)

string

disp() adds a ‘line feed’ to the end of it’s input. So, any following output appears on a new line.

Displays the value of the variable A.

Displays the text (string) that is

enclosed within the single quotes.

84-86

Page 33: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering Program

DISPLAY FORMATSThe format command controls how output numbers appear on the screen. Input numbers can be typed in any format.>> format short (default)

41.4286 Fixed-point with 4 decimal digits

>> format long41.428571428571431 Fixed-point with 15 decimal digits

>> format short e 4.1429e+001 Scientific with 4 decimal digits

>> format bank41.43 Two decimal digits (like money)

Note: These commands affect the output only they do not affect the precision of the number in memory

12-13

Other formatting options are discussed in section 1.4 of the text

>> format short

>> format long

>> format short e

>> format bank

Page 34: ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction

• ENG 1181 First-Year Engineering ProgramHomework Example

clc

disp ('Student, Joe')

disp ('EG167, Seat 99')

disp ('Assignment 1')

disp ('Problem 4')

prob4a = cos(5*pi …

Script File

Student, Joe

EG167, Seat 99

Assignment 1

Problem 4

prob4a =

0.2846

CommandWindow

Submit a printout of both the Script File and the Command Window