1 introduction to matlab morris law jan 5, 2013. 2 outline matlab software entering matrix matrix...

25
1 Introduction to MATLAB Morris Law Jan 5, 2013

Upload: toby-wade

Post on 04-Jan-2016

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

1

Introduction to MATLAB

Morris Law

Jan 5, 2013

Page 2: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

2

Outline

Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming: writing M-file and

functions Function input & output Control flow

Page 3: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

3

MATLAB software

Multi-platforms (Windows, Linux, MacOSX) Technical computing Script language Variables are all matrices Expandable by adding toolboxes

Page 4: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

4

Entering matrix

Explicit listing>> A = [1, 2, 3; 4, 5, 6; 7 8 9]

M-fileStored the above statement in ex1.m. Run the

M-file, i.e. >> ex1 External data files

>> load A.dat Built-in statements and functions

>> B = eye(3)

Page 5: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

5

Matrix function and manipulation

ones(3), zeros(3), eyes(3) magic(4), hilb(5), rand(4,3) size(A), eig(A), det(A), inv(A), diag(A) sum(A), prod(A), mean(A), max(A), std(A)

Page 6: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

6

Exercise on Matrix functions

Try A = [1 2 3; 4 5 6; 7 0 9] x = ones(3) y = eye(3) z = zeros(3) d = diag(A) det(A) inv(A) rank(A) eig(A)

Page 7: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

7

Exercise on Matrix functions

m = magic(4) sum(m) sum(m’) sum(diag(m)) r = rand(4,3) size(r)

Page 8: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

8

Matrix elements

>> x = [-1.3, sqrt(3), (1+2+3)*4/5 ];>> x(1) x(1) =

-1.3000>> x(5)= abs(x(1))x =

-1.3000 1.7321 4.8000 0 1.3000>> y = 1:5y =

1 2 3 4 5xy = [x;y]xy =

-1.3000 1.7321 4.8000 0 1.3000 1.0000 2.0000 3.0000 4.0000 5.0000

Page 9: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

9

Matrix elements (cont)

>> A = [1,2,3; 4,5,6; 7,8,9]; >> r = [10,11,12];>> B = [A;r]; >> C = [A,zeros(size(A));ones(size(A)),A]C =

1 2 3 0 0 04 5 6 0 0 07 8 9 0 0 01 1 1 1 2 31 1 1 4 5 61 1 1 7 8 9

>> D = C(1:3,:)>> D = C(:,3:4)

Page 10: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

10

Matrix Arithmetic

Additions (+) Subtraction (-) Multiplication (*) Right division (/)

Solve A = xb by x = A/b

Left division (\) Solve Ax = b by x = A\b

Power (^)

Page 11: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

11

Element-by-Element Operation

Using Period (.)

x = [1,2,3]; y = [4 5 6];>> x .* y

ans

4 10 18

>> x ./ y

ans

0.25 0.4 0.5

>> x .\ y

ans

4 2.5 2

>> x .^ yans

1 32 729

>> x .^ 2ans

1 4 9

>> 2 .^ [x y]ans

2 4 8 16 32 64

Page 12: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

12

MATLAB graphics

If y is a vector, plot(y) produced a linear graph plot(x,y) produces a graph of y versus x

t = 0: pi/10: 2*pi;

x = sin(t)

Y1 = sin(t+.25);

Y2 = sin(t+.50);

plot(x,y1,’r-’,x,y2,’g--’)

Title(‘Phase Shift’)

xlabel(‘x=sin(t)’);

ylabel(‘y1=sin(t+)’);

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1Phase Shift

x=sin(t)

y=si

n(t+

)

Page 13: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

13

MATLAB graphics – 3D mesh

The following commands draw a meshx = [-pi:0.1:pi]; y=x;

[X,Y]=meshgrid(x,y);

Z = sin(X).* sin(Y) + cos(X).*cos(Y);

mesh(Z);

020

4060

80

0

20

40

60

80-1

-0.5

0

0.5

1

Page 14: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

14

MATLAB programming

Two kinds of MATLAB program 1. Script or M-file

sequence of Matlab statements without input and output variables are global

2. Functions with input and output defined as

function [out1, out2, … outN]= funcname(in1, in2, … inN)

variables are local

Page 15: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

15

Writing scripts / M-files

Use Matlab M-file editor, save the following as fibo.m

% An M-file to calculate Fibonacci numbers

f = [1 1]; i=1;while f(i) + f(i+1) < 1000 f(i+2) = f(i) + f(i+1); i=i+1;endplot(f);

At the Matlab command prompt, type the following to run

>> fibo

Page 16: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

16

Writing script – another example looptest.m

% Fill b with square roots of 1 to 1000 using a for loopclear;tic;for i= 1:1000 b(i) = sqrt(i);endt=toc;disp([‘Time taken for loop method is ‘, num2str(t)]);

% Fill b with square roots of 1 to 1000 using a vectorclear;tic;a = 1:1000;b = sqrt(a);t=toc;disp([‘Time taken for vector method is ‘, num2str(t)]);

Run the program with>> looptest

Page 17: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

17

MATLAB function

MATLAB functions allow input and output variablesfunction y = stat(x)% mean average [ m n ] = size(x);if m == 1 m = n;endy = sum(x) / m;

If z is a vector, z = 1:99, type>> mean(z)ans

50

Page 18: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

18

Control flow

For Loopsfor v = expression

statements

end

While Loopswhile expression

statements

end

If and break statementif expression, break, end

orif expression

statements

[elseif expression

statements]

[else expression

statements]

end

Page 19: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

19

General Purpose commands (1/5)

Managing commands and functionsdemo run demo

help on line documentation

Info information about MATLAB and MathWorks

lookfor keyword search through the help entries

path control MATLAB's search path

type list M-file

what directory listing of M-, MAT- and MEX-files

which locate functions and files

Page 20: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

20

General Purpose commands (2/5)

Managing variables and the workspace clear clear variables and functions from memory

disp display matrix or text

length length of vector

load retrieve variables from disk

pack consolidate workspace memory

save save workspace variables to disk

size size of matrix

who list current variables

whos list current variables, long form

Page 21: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

21

General Purpose commands (3/5)

Working with files and the operating system cd change current working directory

delete delete file

diary save text of MATLAB session

dir directory listing

getenv get environment value

! execute operating system command

Page 22: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

22

General Purpose commands (4/5)

Controlling the command window clc clear command window

echo echo commands inside script files

format set output format

home send cursor home

more control paged output in command window

Page 23: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

23

General Purpose commands (5/5)

Starting and quitting from MATLAB matlabrc master startup M-file

quit terminate MATLAB

startup M-file executed when MATLAB is invoked

Page 24: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

24

Search for documentation

helpdesk http://www.mathworks.com

Page 25: 1 Introduction to MATLAB Morris Law Jan 5, 2013. 2 Outline Matlab software Entering matrix Matrix function and manipulation Matlab graphics Matlab programming:

25

Reference

Booklet on Introduction to MATLAB MATLAB primer (version 3)