matlab and simulinklecture 11 to days outline introduction matlab desktop basic features ...

41
MATLAB and Simul ink Lecture 1 1 To days Outline Introduction MATLAB Desktop Basic Features Branching Statements Loops Script file / Commando file Exercises on this days topics

Upload: ralph-johnston

Post on 26-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

MATLAB and Simulink

Lecture 1 1

To days Outline

Introduction MATLAB Desktop Basic Features Branching Statements Loops Script file / Commando file Exercises on this days topics

MATLAB and Simulink

Lecture 1 2

Introduction

Course schedule W35-W41

Tuesdays 10-12 S319 Theory

Tuesdays 13-17 L337/338

Free Exercises

Thursdays 10-12L337/338 Exercises

Thursdays 13-17 L337/338 Laboratory work

W 42-43 (44) Project Work

MATLAB and Simulink

Lecture 1 3

Introduction No written examination

Grades U – G Laboratory Works

Solutions posted by mail, comment the code! Minor Project

You should come up with an idea for the project

Use your knowledge of MATLAB and Simulink in the project.

Presentation of the project.

MATLAB and Simulink

Lecture 1 4

Introduction Litterature

Stephen J Chapman(2005).

MATLAB Programming for Engineers

ISBN: 0-534-42417-1

Dabney J.B and Harman T.L (2004).

Mastering Simulink

ISBN: 0-13-142477-7

Additional reading, see course information

MATLAB and Simulink

Lecture 1 5

Introduction

MATLAB is used for calculation and visualizing is programmable contains various commands and

functions can interact with other program

languages as: C, FORTRAN, JAVA etc. uses arrays as the basic data type

MATLAB and Simulink

Lecture 1 6

MATLAB Desktop

Command Window Present results Enter commands

Editor/Debug Window Create M-files Debug M-files

MATLAB and Simulink

Lecture 1 7

MATLAB Desktop

Command History View previous

commands

MATLAB and Simulink

Lecture 1 8

MATLAB Desktop

Current Directory File Manipulation Files in current directory

MATLAB and Simulink

Lecture 1 9

MATLAB Desktop

Workspace Shows variables defined

in workspace

MATLAB and Simulink

Lecture 1 10

MATLAB Desktop

Help Demos Tutorials Documentations

MATLAB and Simulink

Lecture 1 11

MATLAB Desktop

Profiler Improve code writing

MATLAB and Simulink

Lecture 1 12

Basic Features

Execute commands either by Write the code in command window Write code in a script file

Execute script file by typing the file name in command window.

Write code in a function file Execute function file by typing the file

name in command window.

MATLAB and Simulink

Lecture 1 13

Basic Features

Addition + Subtraction - Multiplication * Division / Store results in variables

Case sensitive Maximum of 31 characters Must start with a letter

MATLAB and Simulink

Lecture 1 14

Basic Features Example

In command window

>> 5+5ans = 10

>> a=5+5a = 10

MATLAB and Simulink

Lecture 1 15

Basic Features

No need to declare variables Any numerical value assign to a

variable will be of class double Any character or sting assign to a

variable will be of class character

MATLAB and Simulink

Lecture 1 16

Basic Features

The fundamental unit of data in MATLAB is the array

Row 1

Row 2

Row 3

Col 4Col 3Col 2Col 1

MATLAB and Simulink

Lecture 1 17

Basic Features

Initializing variables Assign data in assignment statements

var1 = 5+5; Input data from keyboard

var1 = input(‘Enter a value’) Read data from file

var1 = fread(fid, 5, 'uint8=>char')'

MATLAB and Simulink

Lecture 1 18

Basic Features

How to create arrays in MATLAB The ‘[]’ operator

var1 = [1,2,3,4] % Row vector var1 = [1,2;3,4] % 2x2 array

operator ‘;’ equal to new row

Shortcut expression var1 = first:incr:last

var1 = 1:1:4

MATLAB and Simulink

Lecture 1 19

Basic Features Built-in functions

var1 = zeros(2,4) var1 = ones(2,4)

Useful MATLAB functions eye(n)

Creates an nxn unity matrix size(var1)

Returns the number of rows and columns in variable var1

length(var1) Returns the length of a vector var1, or the longest

dimension if var1 is a 2-D array

MATLAB and Simulink

Lecture 1 20

Basic Features Accessing elements in an array

var1(2,2)= 5;

Row 1

Row 2

Row 3

Col 4Col 3Col 2Col 1

MATLAB and Simulink

Lecture 1 21

Basic Features

Displaying output data The disp function

disp(str) disp(‘This is displayed’)

The fprintf function fprintf(format,data)

fprintf(‘This the value of pi %f \n’,pi)

MATLAB and Simulink

Lecture 1 22

Basic Features Scalar and Array

Operations Remember the

algebraic rules! Dot operator

performs element by element operation.

Operation MATLAB Form

Array Addition A+B

Array Subtraction A-B

Array Multiplication A*B

Array Division A/B = A*inv(B)

Element by Element A./B , A.*B, A.^B

MATLAB and Simulink

Lecture 1 23

Example 2.1

10

21b

2

3c

cad

bac

bab

baa

*)(

*)(

*.)(

)(

dah

dag

daf

cae

*)(

*.)(

)(

)(

12

01a 5d

What is the result of each expression

MATLAB and Simulink

Lecture 1 24

Basic Features Introduction to plotting

Plot the function y(x) for values of x between -5 and 5

>>X=-5:0.1:5>>y=5*x.^2-2*x+5>>plot(x,y)

5225)( xxxy

MATLAB and Simulink

Lecture 1 25

Basic Features Plot figur

MATLAB and Simulink

Lecture 1 26

Basic Features

Multiple plots>>X=-5:0.1:5>>y1=sin(x)>>y2=cos(x)>>subplot(2,1,1),plot(x,y1)>>subplot(2,1,2),plot(x,y2)

MATLAB and Simulink

Lecture 1 27

Basic Features

MATLAB and Simulink

Lecture 1 28

Basic Features Line style, Line color, marker style and

legend plot(x,y,s)

s is a character string defining color, line style, marker style.

See help plot for more information

g=green . = point -- = dashed

k=black o = circle - = solid

MATLAB and Simulink

Lecture 1 29

Example 2.3

Design a MATLAB program that reads an input temperature in degrees Fahrenheit, convert it to an absolute temperature in Kelvin, and write the result.

MATLAB and Simulink

Lecture 1 30

Example 2.4 The figure shows a voltage source V=120 V

with an internal resistance Rs=50Ω supplying a load RL.

Find the value of RL that will result in maximum power being supplied by the source.

Plot the power supplied to the load as a function of the resistance RL

+-

Rs RL

V

MATLAB and Simulink

Lecture 1 31

Example 2.4

0 10 20 30 40 50 60 70 80 90 1000

10

20

30

40

50

60

70

80Plot power versus load resistance

Load Resistance [Ohm]

Pow

er

[watt

s]

MATLAB and Simulink

Lecture 1 32

Branching Statements Logic and Relational OperatorsLogical OPERATORS Relational Operators

Operator Operation Operator Operation

== Equal to & Logical AND

~= Not Equal To | Logical OR

> Greater Than xor Logical Exclusive OR

< Less Then ~ Logical NOT

<= / >= Less/Greater Then or Equal to

MATLAB and Simulink

Lecture 1 33

Branching Statements

The if Construct if control_expr_1

Statement elseif control_expr_2

Statement else control_expr_3

Statement end

MATLAB and Simulink

Lecture 1 34

Example 3.4 Suppose that we are writing a

program which reads in a numerical grade and assigns a letter grade to it according to:

grade > 95 A95 ≥ grade 86 >B86 ≥ grade 76 >C76 ≥ grade 66 >D66 ≥ grade 0 >F

MATLAB and Simulink

Lecture 1 35

Branching Statements The switch Construct

switch (switch_expr)case {csde_expr_1, csde_expr_2,…},

Statementcase {csde_expr_3, csde_expr_4,…},

Statementotherwise,

Statementend

MATLAB and Simulink

Lecture 1 36

Example Determine whether an integer between

1 and 10 is even or odd, and print out an appropriate message.

MATLAB and Simulink

Lecture 1 37

Loops

The While loop

while expression…….code block…….

end

MATLAB and Simulink

Lecture 1 38

Loops

The for loop

for index = exprStatement1Statement2….

end

MATLAB and Simulink

Lecture 1 39

Loops

Comparing Loops and Vectorization If possible use vectors. Loops slows down execution speed

Calculate the square of every integer from 1 to 10000. Using a for loop can take up to 3 sec Using vectors takes 0.0014 sec

MATLAB and Simulink

Lecture 1 40

Script file / Commando file Script file / Commando file

When a script file executes the same result is made if all commands would be written in the command window

A collection of statements All variables created will be stored in the

same workspace. Have access to all variables in the workspace No input argument Returns no result Communication between script files through

data stored in the workspace

MATLAB and Simulink

Lecture 1 41

Suitable Exercises on this days topics

2.1, 2.4, 2.6, 2.10, 2.14, 2.16, 2.18 3.3(2), 3.4(3), 3.14(11), 3.15(12),

3.16(13) 4.24(19), 4.26(21), 4.29(22)

The exercise numbers are referring to the book MATLAB Programming for engineers, third edition (second edition).