MATLAB tutorial Jixing Yao June 22, 2010Outline • Variables and Operations • Variables and Operations • Some Basic Matrices • Plot & Graphics • Plot • Labeling • Complex Numbers • Basic Operations • FOR loop • For loop • Help • Help in MATLABVariables and OperationsMatrix and Variables • To create a variable >> x=5; >> y=sin(pi/3); >> z=exp(-.3); >> k=1j;Matrix and Variables • To enter a vector A • >>A=[1 2 3 4 5]; • >>A=[1,2,3,4,5]; • >>AA=[1;2;3;4;5]; • To pick some elements of a vector • >> AAA=A(2:5); • >> AAA=A([1, 3:end]); • To check the number of elements in a vector, use length >> length(A) ans = 5Matrix and Variables • To enter a matrix B=3 16 4 • >> B=[3 1; 6 4]; • >>B=[3,1;6,4]; • >> C=[A ; A]; • To check dimension of a matrix: • >> size(B) • ans= • 2 2 • Determinant and inverse • >>det(B) • ans= • 6 • >> inv(A) • ans = • 0.6667 -0.1667 • -1.0000 0.5000Basic Operations • Transpose • >>a=A’ • a = • 1 • 2 • 3 • 4 • 5 • >>a=A(:); • >> b=B’ • b = • 3 6 • 1 4Basic Operations • Addition • >> C=B+b • C = • 6 7 • 7 8 • Subtraction • >>D=B-b • D = • 0 -5 • 5 0Basic Operations • Multiplication • Matrix Multiplication • >> E=B*b • E = • 10 22 • 22 52 • Element-wise Multiplication • >>E= B.*b • E = • 9 6 • 6 16Basic Operations • Division • Element-wise Division • >>E= B./b • E = • 1.0000 0.1667 • 6.0000 1.0000 • bB-1 • >>F=b/B • F = • -4.0000 2.5000 • -3.3333 1.8333 • >>F=b*inv(B)Generating a Vector of Arithmetic Sequence • v = start:increment:end • >> v = 0:2:10 • v = • 0 2 4 6 8 10 • >> v = 0:10 • v = • 0 1 2 3 4 5 6 7 8 9 10Some Basic Matrices • All zero matrix • >> z=zeros(2,3) • z = • 0 0 0 • 0 0 0 • Matrix with ones • >> w = ones(2,3) • w = • 1 1 1 • 1 1 1 • Identity matrix • >> I =eye(3) • I = • 1 0 0 • 0 1 0 • 0 0 1Plot & GraphicsPlot • Plot • x = [x(1), ..., x(N)] • y = [y(1), ..., y(N)] • plot(x,y) gives the plot with straight line connecting between the data points {(x(1), y(1)), ...(x(N), y(N))} • x = [0:5] • y = [1 7 5 3 2 1] • plot(x,y) • stem(x,y) suitable if consider a discrete signalSubplot • subplot(r,c,p) • r is number of rows • c is number of columns • p is the position of that plot • >> subplot(2,2,1) • >> plot(x,y) • >> subplot(2,2,2) • >> stem(x,y) • >> subplot(223) • >> plot(x,y) • >> subplot(224) • >> stem(x,y)Label the Plot • Title title(’.......’) • X axis xlabel(’.......’) • Y axis ylabel(’.......’)Plot ToolsComplex NumbersBasic Operations • Real part and imaginary part of z = 1 + j2 • >> real(z) • ans = • 1 • >> imag(z) • ans = • 2 • Find the magnitude and the phase(in radian) • >> abs(z) • ans = • 2.2361 • >> angle(z) • ans = • 1.1071 • Conjugate of z = 1 + j2 • >> conj(z) • ans = • 1.0000 - 2.0000iFOR LoopFOR loop • FOR loop • for i = start:increment:end • <do P(i)> • end • Example: Find the summation 1+2+...+10 • >> summ=0; • >> for i =1:10 • summ = summ+i; • end • >> summ • summ = • 55Need
View Full Document