introduction to matlab tutorial for cs4mn3/se3x03 wen yu mcmaster university

18
Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Post on 21-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Introduction to Matlab

Tutorial for CS4MN3/SE3X03Wen Yu

McMaster University

Page 2: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Matlab Overview

• Matlab is a commercial "Matrix Laboratory" package which operates as an interactive programming environment

• Matlab is installed in Moore- ssh moore- change to working directory- matlab- quit or exit

Page 3: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Entering matrices

• On the interactive window A = [1 2 3 A = [1,2,3; 4,5,6; 7,8,9] 4 5 6

7 8 9 ]

• From Files- data files: load data.ext- script files: data.m

• Built-in statements and functions: rand(2,3)• From blocks: B = [A, zeros(3,2); zeros(2,3), eye(2)]

Page 4: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Operations

• Matrix operations: +, -, *, ^, ‘, \, /- x = A\b is the solution of A x = b- x = b/A is the solution of x A = b- b/A = (A’\b’)’

• Array operations (entry-wise): .*, .^, .\, ./- [1,2,3,4].*[1,2,3,4] = [1,2,3,4].^2 = [1,4,9,16]

Page 5: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Statements, Expressions, and Variables

• Matlab is an expression language• variable = expression, or simply expression• Expressions are assigned to default variable

ans• A statement is terminated with (;), (,) or

carriage return• A statement can be placed in several lines• Several statements can be placed in one line

Page 6: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Control Statement

• for i = m:k:n statements

end• while relation

statementsend

• if relationstatements

end

Page 7: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Relations

• Relational operators: <, >, <=, >=, ==, ~=• Logical operators: &, |, ~• A relation between matrices is true if the

relation of each entry is true• any(any(A ~= B)) returns true if any entry of A

and B is not equal

Page 8: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Submatrices and Colon Notation

• 1:5 gives [1 2 3 4 5]• 0.2:0.2:1.2 gives [0.2, 0.4, 0.6, 0.8, 1.0, 1.2]• 5:-1:1 gives [5 4 3 2 1]• B(1:4,3) first four entries of the third column• B(:,3) the third column• B(1:4,:) first four rows• C=B, B(:,[2 4 5]) = C(:,1:3)• B(:,[2,4]) = B(:,[2,4])*[1 2;3 4]

Page 9: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

M-files

• A script file consists of a sequence of normal MATLAB statements

• Variables in a script file are global• Function files provide extensibility to MATLAB• Variables in a function file are by default local• function [out1, out2, …] = fname(in1, in2, …)• A comment line begins with the % symbol

Page 10: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Text

• s = 'This is a string‘• disp(‘This message is hereby displayed')• error('Sorry, the matrix must be symmetric')• iter = input('Enter the number of iterations: ')• fprintf(1,‘This is a string, too.’)

Page 11: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Output Format.

• format short fixed point with 4 decimal places (the default)

• format long fixed point with 14 decimal places• format short e scientific notation with 4 decimal places• format long e scientific notation with 15 decimal

places• format rat approximation by ratio of small integers• format hex hexadecimal format• format bank fixed dollars and cents• Format compact suppress most blank lines

Page 12: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Graphics

Polar plot: t=0:.01:2*pi; polar(t,abs(sin(2*t).*cos(2*t))); Line plot:

x=0:0.05:5;,y=sin(x.^2);,plot(x,y);

Stem plot: x = 0:0.1:4;, y = sin(x.^2).*exp(-x); stem(x,y)

Page 13: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Graphics – cond.

Mesh plot:z=peaks(25);, mesh(z);

Surface plot: z=peaks(25);, surf(z);, colormap(jet);

Contour plot: z=peaks(25);,contour(z,16); Quiver plot:

Page 14: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Titles, Axes labeled, and Text

• title graph title• xlabel x-axis label• ylabel y-axis label• text position text at specified coordinates

Page 15: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Axis

• axis([xmin,xmax,ymin,ymax]) set axis scaling to prescribed limits

• axis(axis) freezes scaling for subsequent graphs• axis auto returns to auto-scaling• v = axis returns vector v showing current scaling• axis square same scale on both axes• axis equal same scale and tic marks on both axes• axis off turns off axis scaling and tic marks• axis on turns on axis scaling and tic marks

Page 16: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Line- and Mark-types

• Linetypes: solid (-), dashed (--). dotted (:), dashdot (-.)

• Marktypes: point (.), plus (+), star (*), circle (o), x-mark (x)

Page 17: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

Other Useful Command for Graphics

• figure creates an empty figure window• hold on freezes the current graphics screen• hold off releases the hold• print -deps -f3 filename saves to filename.eps

the graphics figure 3• saveas(gcf, ‘plot2.eps’) save current figure as

graphics file.

Page 18: Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University

References

• MATLAB Primer, Kermit Sigmon, Department of Mathematics, University of Florida

• Introduction to Matlab, Joanna Waniek • Matlab Tutorial, Prof. Matthias Hein• Introduction to MATLAB, Markus Kuhn,

University of Cambridge