Computer Applications Lab Computer Applications Lab Lab 1 Lab 1 - - PowerPoint PPT Presentation

computer applications lab computer applications lab lab 1
SMART_READER_LITE
LIVE PREVIEW

Computer Applications Lab Computer Applications Lab Lab 1 Lab 1 - - PowerPoint PPT Presentation

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 What is Matlab ? What


slide-1
SLIDE 1

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

slide-2
SLIDE 2

What is Matlab ? What is Matlab ?

  • A Computer programming language and software

environment to manage data interactively

  • Originally developed in 1970s for applications

involving matrices, linear algebra and numerical analysis analysis

  • Maintained and sold by the MathWorks, Inc.
  • Functionality: manage

variables, import/export data, calculations, generate plots, and more ……

  • T
  • olboxes such as image processing, statistics,

control, financial analysis, and more …

2

slide-3
SLIDE 3

Matlab Desktop Matlab Desktop

Menus

3

Toolbar Current Directory Command Window Command History Workspace

slide-4
SLIDE 4

Matlab as interactive calculator Matlab 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

slide-5
SLIDE 5

Matlab as interactive calculator Matlab as interactive calculator

  • Matlab retains your previous keystrokes.
  • Use the up-arrow key to scroll back through

the 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. Edit

a line using the left-and right-arrow keys the Backspace key, and the Delete key.

  • Press

the Enter key to execute the command.

5

slide-6
SLIDE 6

Scalar Arithmetic Operations Scalar 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

slide-7
SLIDE 7

Order of Precedence of Order of Precedence of Arithmetic Operators Arithmetic Operators

Note: if precedence is

Parentheses Exponentiation

7

precedence is equal, evaluation is performed from left to right.

Exponentiation Multiplication & division Addition and subtraction

slide-8
SLIDE 8

Examples of Precedence Examples of Precedence

>> 8 + 3*5 ans= 23 >> 8 + (3*5) ans= 23 >> (8 + 3)*5 ans= 55 >> 4^2-12- 8/4*2 ans= >> 4^2-12- 8/(4*2) ans= 3

8

slide-9
SLIDE 9

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

>> 3*4^2 + 5 ans= 53 >> (3*4)^2 + 5 ans= 149 149 >> 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

slide-10
SLIDE 10

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

  • Typing x = 3 assigns the value 3 to the variable

x.

  • We can then type x = x + 2. This assigns the

value 3 + 2 = 5 to x. But in algebra this implies that 0 = 2. that 0 = 2.

  • In algebra we can write x + 2 = 20, but in

Matlab 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

slide-11
SLIDE 11

Variables in Matlab Variables in Matlab

No declaration or dimension statements are required to

define variables in Matlab.

To define a new variable, simply write the variable name

followed by the assignment operator and the value to be stored 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 the

command prompt and hit Enter.

Variable names consist of a letter, followed by any number

  • f letters, digits, or underscores.

11

slide-12
SLIDE 12

Special Variables and Constants Special Variables and Constants

Variable Description ans Temporary variable containing the most recent answer

12

i and j The imaginary unit √−1 inf Infinity NaN Indicates an undefined numerical result pi The number π

slide-13
SLIDE 13

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)

slide-14
SLIDE 14

Complex Number Operations Complex Number Operations

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

c1 = 1-2i

  • An asterisk is not needed between i or j

and a number, although it is required with and a number, although it is required with a 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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Numeric Display Formats Numeric Display Formats

Format Operation Example short Four decimal digits (the default format); 13.6745 long 16 decimal digits; 17.27484029463547

16

long 16 decimal digits; 17.27484029463547 short e Five digits (four decimals) plus exponent 6.3792e+03 long e 16 digits (15 decimals) plus exponent; 6.379243784781294e–04

slide-17
SLIDE 17

Command Resolving in Matlab Command Resolving in Matlab

What happens when you type problem1 in the command 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 own

commands, and executes it if it is.

  • 3. If not, Matlab then looks in the current directory for a file

named problem1.mand executes problem1if it finds it.

  • 4. If not, Matlab then searches the directories in its search

path, in order, for problem1.m and then executes it if found.

17

slide-18
SLIDE 18

The Help Navigator The 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

slide-19
SLIDE 19

Help Functions Help Functions

  • help funcname: Displays in the Command

window a description of the specified function funcname.

  • lookfor topic: Displays in the Command

window a brief description for all functions window a brief description for all functions whose description includes the specified key word topic.

  • doc funcname: Opens the Help Browser to

the reference page for the specified function funcname, providing a description, additional remarks, and examples.

19