introduction to matlab
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to Matlab](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/1.jpg)
Introduction to Matlab
Tutorial for CS4MN3/SE3X03Wen Yu
McMaster University
![Page 2: Introduction to Matlab](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/6.jpg)
Control Statement
• for i = m:k:n statements
end• while relation
statementsend
• if relationstatements
end
![Page 7: Introduction to Matlab](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/16.jpg)
Line- and Mark-types
• Linetypes: solid (-), dashed (--). dotted (:), dashdot (-.)
• Marktypes: point (.), plus (+), star (*), circle (o), x-mark (x)
![Page 17: Introduction to Matlab](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/17.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062305/56814aee550346895db7fd07/html5/thumbnails/18.jpg)
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