1 / 18

Introduction to Matlab

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

hua
Download Presentation

Introduction to Matlab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Matlab Overview • Matlabis a commercial "Matrix Laboratory" package which operates as an interactive programming environment • Matlab is installed in Moore - sshmoore - change to working directory - matlab - quit or exit

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

  4. 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]

  5. 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

  6. Control Statement • for i = m:k:n statements end • whilerelation statements end • ifrelation statements end

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

  8. 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]

  9. 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

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

  11. 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

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

  13. Graphics – cond. Surface plot: z=peaks(25);, surf(z);, colormap(jet); Mesh plot: z=peaks(25);, mesh(z); Contour plot: z=peaks(25);,contour(z,16); Quiver plot:

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

  15. 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

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

  17. Other Useful Command for Graphics • figurecreates 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.

  18. 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

More Related