computer applications lab lab 1 introduction to matlab · computer applications lab lab 1...

19
Computer Applications Lab Computer Applications Lab Lab 1 Lab 1 Introduction to Matlab Introduction to Matlab Chapter 1 Chapter 1 Sections 1,2,3,5 Sections 1,2,3,5 Dr. Iyad Jafar Adapted from the publisher slides

Upload: others

Post on 23-Mar-2020

44 views

Category:

Documents


2 download

TRANSCRIPT

Computer Applications LabComputer Applications LabLab 1Lab 1

Introduction to MatlabIntroduction to Matlab

Chapter 1Chapter 1Sections 1,2,3,5Sections 1,2,3,5

Dr. Iyad Jafar

Adapted from the publisher slides

What is Matlab ?What is Matlab ?

• A Computer programming language and softwareenvironment to manage data interactively

• Originally developed in 1970s for applicationsinvolving matrices, linear algebra and numericalanalysisanalysis

• Maintained and sold by the MathWorks, Inc.

• Functionality: manage variables, import/exportdata, calculations, generate plots, and more ……

• Toolboxes such as image processing, statistics,control, financial analysis, and more …

2

Matlab DesktopMatlab Desktop

Menus

3

Toolbar

Current Directory

Command Window

Command History

Workspace

Matlab as interactive calculatorMatlab as interactive calculator

>> 8/10

ans=

0.8000

>> 5*ans

ans=

4

NOTES• ans is a built-in special variable to store the result of the last computation. Can be reused by 4

>> r=8/10

r =

0.8000

>> r

r =

0.8000

>> s=20*r

s =

16 4

result of the last computation. Can be reused by typing ans• variables defined by the assignment operator ‘=‘ are stored in memory and can be used again using their names

Matlab as interactive calculatorMatlab as interactive calculator

• Matlab retains your previous keystrokes.

• Use the up-arrow key to scroll back throughthe commands.

• Press the key once to see the previous entry,• Press the key once to see the previous entry,and so on.

• Use the down-arrow key to scroll forward. Edita line using the left-and right-arrow keys theBackspace key, and the Delete key.

• Press the Enter key to execute thecommand.

5

Scalar Arithmetic OperationsScalar Arithmetic Operations

Symbol Operation Mathematical Syntax

Matlab Syntax

^ Exponentiation ab a^b

* Multiplication ab a*b

6

/ Forward Division a/b a/b

\ Backward Division a\b a\b

+ Addition a+b a+b

- Subtraction a-b a-b

Order of Precedence of Order of Precedence of Arithmetic OperatorsArithmetic Operators

Note: if precedence is

Parentheses

Exponentiation

7

precedence is equal, evaluation is performed from left

to right.

Exponentiation

Multiplication & division

Addition and subtraction

Examples of PrecedenceExamples of Precedence>> 8 + 3*5

ans=

23

>> 8 + (3*5)

ans=

23

>> (8 + 3)*5

ans=

55

>> 4^2-12- 8/4*2

ans=

0

>> 4^2-12- 8/(4*2)

ans=

3 8

Examples of Precedence Examples of Precedence (continued)(continued)

>> 3*4^2 + 5

ans=

53

>> (3*4)^2 + 5

ans=

149149

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

ans=

5

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

ans=

5

>> 27^1/3 + 32^0.2

ans=

11 9

The Assignment Operator ‘=’ The Assignment Operator ‘=’

• Typing x = 3 assigns the value 3 to the variablex.

• We can then type x = x + 2. This assigns thevalue 3 + 2 = 5 to x. But in algebra this impliesthat 0 = 2.that 0 = 2.

• In algebra we can write x + 2 = 20, but inMatlab we cannot.

• In Matlab the left side of the assignment operator‘=‘ must be a single variable.

• The right side must be a computable value.10

Variables in MatlabVariables in Matlab

� No declaration or dimension statements are required todefine variables in Matlab.

� To define a new variable, simply write the variable namefollowed by the assignment operator and the value to bestored in the variable.

num_students = 25 num_students = 25

� If the variable already exists, Matlab changes its contents and,if necessary, allocates new storage.

� To view the value of the variable, type its name on thecommand prompt and hit Enter.

� Variable names consist of a letter, followed by any numberof letters, digits, or underscores.

11

Special Variables and Constants Special Variables and Constants

Variable Description

ansTemporary variable containing

the most recent answer

i and j The imaginary unit √ 1

12

i and j The imaginary unit √−1

inf Infinity

NaN Indicates an undefined numerical result

pi The number π

Commonly Used Mathematical Commonly Used Mathematical Functions Functions

Function Matlab Syntax

ex exp(x)

√x sqrt(x)

ln x log(x)

log10 x log10(x)

cos x cos(x)

sin x sin(x)

13

NOTES• Trigonometric functions in Matlab use radian measure• cos2(x) is written (cos(x))^2 in Matlab

sin x sin(x)

tan x tan(x)

cos−1 x acos(x)

sin −1 x asin(x)

tan −1 x atan(x)

|x| abs(x)

Complex Number OperationsComplex Number Operations

• The number c1= 1 –2i is entered as follows:

c1 = 1-2i

• An asterisk is not needed between i or jand a number, although it is required withand a number, although it is required witha variable, such as c2 = 5 - i*c1.

• Be careful ! The expressions y = 7/2*i and x = 7/2i give two different results

y = (7/2)i = 3.5i

x = 7/(2i) = –3.5i

14

Commands for managing the Commands for managing the work session work session

Command Operation

clc Clears the command window

clear Clears all the variables from the workspace

clear v1 v2 Clears variables v1 and v2 only

exist(‘var’) Check if the variable or function exist

15

exist(‘var’) Check if the variable or function exist

quit Exits Matlab session

who List the names of the variables defined in the work space

whos List the names of the variables defined in the work space and their details

Semicolon (;) Suppresses the display of results in the command window

Ellipsis (…) Line continuation

Numeric Display Formats Numeric Display Formats

Format Operation Example

shortFour decimal digits (the default

format); 13.6745

long 16 decimal digits; 17.27484029463547

16

long 16 decimal digits; 17.27484029463547

short eFive digits (four decimals) plus

exponent6.3792e+03

long e16 digits (15 decimals) plus

exponent; 6.379243784781294e–04

Command Resolving in MatlabCommand Resolving in Matlab

What happens when you type problem1 in thecommand prompt ?

1. Matlab first checks to see if problem1 is a variable and if so,displays its value.displays its value.

2. If not, Matlabthen checks to see if problem1 is one of its owncommands, and executes it if it is.

3. If not, Matlab then looks in the current directory for a filenamed problem1.mand executes problem1if it finds it.

4. If not, Matlab then searches the directories in its searchpath, in order, for problem1.m and then executes it if found.

17

The Help NavigatorThe Help Navigator

� Available through the help menu

� Contents◦ Contents: contents listing tab◦ Index : a global index tab,◦ Index : a global index tab,◦ Search: a search tab having a find function and

full text search features◦ Demos: a bookmarking tab to start built-in

demonstrations.

18

Help FunctionsHelp Functions

• help funcname: Displays in the Commandwindow a description of the specified functionfuncname.

• lookfor topic: Displays in the Commandwindow a brief description for all functionswindow a brief description for all functionswhose description includes the specified keyword topic.

• doc funcname: Opens the Help Browser tothe reference page for the specified functionfuncname, providing a description, additionalremarks, and examples.

19