Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Matlab

Similar presentations


Presentation on theme: "Introduction to Matlab"— Presentation transcript:

1 Introduction to Matlab
332:202 Discrete Mathematics Spring 2007

2 Contents What is Matlab Matlab basic Advanced usage of Matlab - plots
- loops - working with m files - advanced functions

3 Contents What is Matlab Matlab basic Advanced usage of Matlab

4 What is Matlab Matrix laboratory easy to learn robust
powerful functions

5 Contents What is Matlab Matlab basic Advanced usage of Matlab

6 Get Help Help - help - help function name (help abs)

7 Basic Math Operations To compute a math expression composed of numbers and mathematics operators + addition - subtraction * multiplication / division ^ power Compute (2+3-9)*7^2/4 =-49

8 General Math Expressions
To compute a math expression composed of numbers, variables and mathematics operators Define all variables The others are the same E.g. x=3;y=6;x+y=? It is equivalent to 3+6

9 Exercise in Class(1) 5 minutes x=123;y=234;z=345
Write the command expression and get the result: 1) the sum of x and y 2) subtract y from z 3) the multiplication of x and z Answer 1) x+y=357;2)z-y=111;3)x*z=42435

10 Vectors and Matrices Arrays are represented by some elements within brackets The elements of each row is separated by at least one space The separation of rows is by semicolons E.g. [2 3;1 2]

11 How to Retrieve Elements of Matrix 1
What is the difference between x(2),x(1,2),x(:,2),x(2,:),x(1:2,2)?

12 How to Retrieve Elements of Matrix 2
Index begins from 1, not 0 For matrix x: x(i) is the ith element, counted along the column x(i,j) is the element at the ith row and jth column x(:,j) is all the elements at the jth column x(i,:) is all the elements at the ith row x(i1:i2,j) is those elements at the jth column, begins from index i1 to i2

13 Vector and Matrix Operations
+,-,*,/ are used in operations between matrices .*,./,.^ are used in operations between matrix elements Pay attention to the dimensions of vectors and matrices

14 Exercise in Class(2) 10 mins x=[1 2;3 4];y=[5 6;7 8];
z1=[1 2 3];z2=[4 5 6]; 1)Sum of x and y 2) Product of x and y 3)Bitwise product of x and y 4) Bitwise division of z1 over z2 5) Bitwise division of z1 over z2, but for the first two elements only (both the expression and result) Answer:1) [6 8;10 12]; 2) [19 22;43 50]; 3) [5 12;21 32];4)[ ]; 5) z1(1:2)./z2(1:2)=[ ]

15 Matrix Transpose More operators other than +,-,*,/,^
Matrix transpose:’(apostrophe)

16 Contents What is Matlab Matlab basic Advanced usage of Matlab

17 Plot plot(y) plots the columns of y versus their index
plot(x,y) plots vector y versus vector x plot(x,y,s) plots vector y versus vector x, and s is a character string which states the line properties plot(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by the (X,Y,S) triples, where the X's and Y's are vectors or matrices and the S's are strings.

18 Color Options - Yellow - ‘y’ Magenta - ‘m’ Cyan - ‘c’ Red - ‘r’
Green - ‘g’ Blue - ‘b’ Black - ‘k’

19 Line Styles - solid line (default) -- dashed line : dotted line
-. dash-dot line

20 Line Markings + - plus sign o - circle * - asterisk . - Point
x - cross s - square d - diamond ^ - upward pointing triangle v - downward pointing triangle > - right pointing triangle < - left pointing triangle p - five-pointed star (pentagram) h - six-pointed star (hexagram) Combined use of color, line style, and line marking : ‘r+:’

21 Example 1)t=0:0.1:4*pi; x=sin(t);
plot(x);plot(t,x); plot(t,x,’r’); plot(t,x, 'c+:’); 2)x=1:1:10;y=2*x; plot(x,y,'r-');

22 Exercise in Class 3 15 mins Write a short program which plots 2 lines in a graph. Two cycles of sin wave and cos wave. Useful function: plot, sin, cos t=0:0.1:4*pi; x=sin(t);y=cos(t); plot(t,x,'r+-',t,y,'bo-.');

23 Grid Grid or GRID ON adds major grid lines to the current axes.
GRID OFF removes major and minor grid lines from the current axes.

24 Comment Functions xlabel('text') adds text beside the x-axis on the current axis ylabel('text') adds text beside the y-axis on the current axis title('text') adds text at the top of the current axis legend(string1,string2,string3, ...) puts a legend on the current plot using the specified strings as labels text(x,y,'string') adds the text in the quotes to location (X,Y) on the current axes

25 Axis AXIS Control axis scaling and appearance.
AXIS([XMIN XMAX YMIN YMAX]) sets scaling for the x- and y-axes on the current plot. AXIS OFF turns off all axis labeling, tick marks and background. AXIS ON turns axis labeling, tick marks and background back on.

26 Exercise in Class 4 15 mins Write a short program which plots 2 lines in a graph. Two cycles of sin wave and cos wave. Use as many functions as you can: plot, sin, cos, grid, xlabel, ylabel, grid, title, legend, text, axis t=0:0.1:4*pi; x=sin(t); y=cos(t); plot(t,x,'r+-',t,y,'bo-.'); grid on; xlabel('The Time'); ylabel('The Amplitude'); title('Sin Wave and Cos Wave'); legend('Sin','Cos','Location','Best'); axis([0,4*pi,-2,2]); text(3/4*pi,sin(3/4*pi),'\leftarrow Sin'); text(9/4*pi,cos(9/4*pi),'\leftarrow Cos');

27 Plots and Graphs – Con’t
HOLD ON holds the current plot and all axis properties so that subsequent graphing commands add to the existing graph. HOLD OFF returns to the default mode whereby PLOT commands erase the previous plots and reset all axis properties before drawing new plots. Figure or figure ( );

28 Save Figures Save manually Use Function Print
- print filename directs the output to the PostScript file designated by filename - print -dformat filename exports the figure to the specified file using the specified graphics format, (such as TIFF). e.g. print –dmeta filename print -deps filename

29 Exercise in Class 5 5 mins Generate a figure, and save it as ps file and emf file, respectively

30 Plots and Graphs – Con’t
One example, different views Plot3() x=-1:0.1:1; y=-1:0.1:1; z=x.^2+y.^2; plot3(x,y,z); Mesh(); contour(); meshc(); meshz(); Surf(); [x,y]=meshgrid(-1:0.1:1); mesh(x,y,z); H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for the current plot, and returns the axis handle. The axes are counted along the top row of the Figure window, then the second row, etc.

31 Basic Control Statements-for
for, end for variable=expression statement; end expression: initial value: increment: end value for i=1:5 for j=1:4 A(i,j)=1/(i+j-1); end;

32 Exercise in Class 6 10 mins Write a for program to compute …+50 x=0; for i=0.5:0.5:50 x=x+i; end

33 Basic Control Statements-while
while, end while expression statement; end expression: a logical statement which is connected by ==,<,<=,>,>=,~=. x=1; while x<5 x=x+1;

34 Exercise in Class 7 10 mins Write a while program to compute …+50 i=0.5;x=0; while i<=50 x=x+i; i=i+0.5; end

35 Basic Control Statements-if
if, elseif, end IF expression statements ELSEIF expression ELSE END

36 M Files Put a bunch of commands in an .m file.
Run the file under the command window. The using of ‘;’. Write a function as an .m file. - file name is the function name - the first line defines the function function myresutl=myfunction(a, b); - the comment lines (start with %) follow the first line is shown when asking for help

37 Exercise in Class 10 mins Write a m file to compute 0.5+1+1.5+2+…+50
Run it

38 Useful Links Google


Download ppt "Introduction to Matlab"

Similar presentations


Ads by Google