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

## Presentation on theme: "Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University."— Presentation transcript:

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

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)]

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]

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

Control Statement for i = m:k:n statements end while relation statements end if relation statements end

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

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]

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

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.’)

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

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)

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:

Titles, Axes labeled, and Text title graph title xlabel x-axis label ylabel y-axis label text position text at specified coordinates

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

Line- and Mark-types Linetypes: solid (-), dashed (--). dotted (:), dashdot (-.) Marktypes: point (.), plus (+), star (*), circle (o), x-mark (x)

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.

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

Download ppt "Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University."

Similar presentations