matlab programming for engineers dr. bashir nouri introduction to matlab matlab basics branching...

39
Matlab Programming for Engineers Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Introduction to Matlab Matlab Basics Matlab Basics Branching Statements Branching Statements Loops Loops User Defined Functions User Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)

Upload: ariel-obrien

Post on 02-Jan-2016

255 views

Category:

Documents


4 download

TRANSCRIPT

Matlab Programming for Engineers Matlab Programming for Engineers Matlab Programming for Engineers Matlab Programming for Engineers

Dr. Bashir NOURI

Introduction to MatlabIntroduction to Matlab Matlab BasicsMatlab Basics Branching Statements Branching Statements Loops Loops User Defined Functions User Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)

Introduction to MatlabIntroduction to Matlab Matlab BasicsMatlab Basics Branching Statements Branching Statements Loops Loops User Defined Functions User Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

2

Objectives:Objectives:

Variables and Arrays

Initializing Variables in MATLAB

Multidimensional Arrays

Subarrays

Special Values

Displaying Output Data

Data Files

Scalar and Array Operations

Hierarchy of Operations

Built-in MATLAB Functions

Introduction to Plotting

Debugging MATLAB Programs

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

3

Variables and Arrays Variables and Arrays

The fundamental unit of data in any Matlab program is the Array Any element of the array can

be retrieved by its indexes Arrays could be matrices or

vectors (row or column) Matlab variable is the region

of memory containing an array Matlab variable

letter + (letter, number, _) Variable type:

1. double precession

(real, complex)

2. Character

'How are you?'

a(3,2)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

4

Creating and initializing variables in MatlabCreating and initializing variables in Matlab

1. Initializing variables in assignment statements

var = expression; var = 40i; var2 = var/5; var = [1 2 3 4];

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

5

Creating and initializing variables in MatlabCreating and initializing variables in Matlab

1. Initializing variables in assignment statements

a = [0 1+7]; a = [0 8]

b = [ a(2) 7 a]; b = [8 7 0 8]

c(2,3) = 5; c = 0 0 0

0 0 5

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

6

Creating and initializing variables in MatlabCreating and initializing variables in Matlab

2. Initializing variables with Shortcut Expression

colon operator (:)

first : incr : last

Ex:

x = 1:2:10; x = [1 3 5 7 9]

angles = (0.01:0.01:1)*pi;

f = [1:4]'; if no incr is used default incr=1

(') is the transpose operator.

g = [1:4];

h = [g' g'];

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

7

Creating and initializing variables in MatlabCreating and initializing variables in Matlab

3. Initializing variables with Built-in Function

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

8

Creating and initializing variables in MatlabCreating and initializing variables in Matlab

4. Initializing variables with Keyboard Input

my_val = input('Enter an input value: ');

v1 = input('Enter data: ');

v2 = input('Enter data: ', 's');

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

9

Multidimensional ArraysMultidimensional Arrays

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

10

Multidimensional ArraysMultidimensional Arrays

1. Storing Multidimensional Array in Memory

2. Accessing this array with one dimension index

How

the

arr

ay is

sto

red

in c

ompu

ter

mem

ory Array as introduced

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

11

SubarraysSubarrays

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

12

SubarraysSubarrays

1. The end Function (highest value of a given subscript)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

13

SubarraysSubarrays

3. Assigning a scalar to

a subarray

2. Left-hand side Subarrays

assignment statement

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

14

Special ValuesSpecial Values

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

15

Displaying Output DataDisplaying Output Data

Default Matlab Format == Four Digits After the Decimal Point

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

16

Displaying Output Data (NUMBER FORMAT)Displaying Output Data (NUMBER FORMAT)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

17

Displaying Output Data (disp and fprintf functions)Displaying Output Data (disp and fprintf functions)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

18

Data Files (save)Data Files (save)

save filename var1 var2 var3 *.mat

save filename var1 var2 var3 -ascii text format (ascii)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

19

Data Files (save)Data Files (save)

save filename var1 var2 var3 *.mat

save filename var1 var2 var3 -ascii text format (ascii)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

20

Data Files (load)Data Files (load)

load filename.mat var1 var2 (*.mat file format, loads specific variables)

load filename.* (any other format, loads all the file

data in the file must have array structure)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

21

Scalar and Array Operations Scalar and Array Operations

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

22

Scalar and Array

Operations

Very important

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

23

Scalar and Array Operations Scalar and Array Operations

Matrix left division (a\b)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

24

Hierarchy of Operations Hierarchy of Operations

Distance = 0.5 * acceleration* (time^2)

Distance = (0.5 * acceleration* time)^2

Not the same!

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

25

Class Work Class Work

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

26

Built-in Matlab Functions Built-in Matlab Functions

1 . Optional results

2. Most of functions

accept array inputs

Optional

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

27

Built-in Matlab Functions (Common Functions) Built-in Matlab Functions (Common Functions)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

28

Built-in Matlab Functions (Common Functions) Built-in Matlab Functions (Common Functions)

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

29

Plotting in MatlabPlotting in Matlab

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

30

Printing a Plot / Exporting a Plot as a Graphical Image Printing a Plot / Exporting a Plot as a Graphical Image

>> print <options> <filename>

>> help print (to see print options)

If no filename is specified, plot is printed on the printer

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

31

Printing a Plot / Exporting a Plot as a Graphical Image Printing a Plot / Exporting a Plot as a Graphical Image

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

32

Multiple plots Multiple plots

Plot the following functions on the same figure

f(x) = sin(2x) and its first derivative over x = 0:pi/100:2*pi

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

33

Multiple plots Multiple plots

Plot the following functions on the same figure

f(x) = sin(2x) and its first derivative over x = 0:pi/100:2*pi

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

34

Line Color, Line Style, Marker Style, and Legends Line Color, Line Style, Marker Style, and Legends

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

35

Line Color, Line Style, Marker Style, and Legends Line Color, Line Style, Marker Style, and Legends

Line Color, Line Style, and Marker Style:

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

36

Line Color, Line Style, Marker Style, and Legends Line Color, Line Style, Marker Style, and Legends

Legends:

>> legend off (removes the legend from the plot)

>> legend('string1' , 'string1' , … ,'location', 'value from table')

Be aware of the version of Matlab you are using, see help legend

NW NL NC NR NE

TW TL TC TR TE

MW ML MC MR ME

BW BL BC BR BE

SW SL SC SR SE

Limits of plot axes

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

37

Logarithmic Scale Logarithmic Scale

>> plot

>> semilogx

>> semilogy

>> loglog

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

38

Example Example

CH2: MATLAB BASICS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

39

Home WorkHome Work

Solve the following problems

2. [9, 10 , 11, 16]