Anda di halaman 1dari 2

MATLAB as a calculator Double click on MATLAB icon Type the below commands on MATLAB the command window and

observe the outputs. Command remarks >> 8*sqrt(30)+pi After typing in the expression hit return/enter key. ans = The result of an unassigned expression is saved in the default 46.9594 variable ans. pi is defined as 3.1416 in MATLAB >> x=(cos(pi/4))^2 We can assign the value of an expression to a variable ex. x x= A semicolon at the end suppresses the screen output. We can 0.5000 recall the value of x by simply typing in x >> format short e >> format long The floating point output display >> pi >> pi is controlled by the format ans = ans = command. 3.1416e+000 3.14159265358979 >>(2+3.2i)*exp(pi/2*i) 2+3.2*i as complex only if i i & j are 1 2+3.2i ans = (or j) has not been assigned is always interpreted -3.2000e+000 +2.0000e+000i any local value as complex number Working with arrays (vectors) >> x=[1 2 3] >>y=[1;2;3] x is a row vector with 3 elements; y is a column >> w=[2 4 5]; x= y= vector. We can add or subtract 2 vectors of the >> f=w+x 1 2 3 1 same size but not add a row & column vectors f= 2 >> r =x+y ??? Error using ==> + 3 6 8 3 Matrix dimensions must agree. >> t=linspace(0,5,3) Create a vector t with 3 elements linearly t = 0 2.5000 5.0000 spaced b/w 0 & 5 >> t=logspace(0,5,3) Create a vector t with 3 elements t = 1.0e+005 * logarithmically spaced b/w 100 & 105 0.0000 0.0032 1.0000 >> v=2:5:35 V=initial value: Increment : final value v = 2 7 12 17 22 27 32 >> v=2:7 Default increment is 1 if it is not specified v= 2 3 4 5 6 7 >> u = zeros(1,5) >> u = ones(1,5) Creating vectors of all ones u= 0 0 0 0 0 u= 1 1 1 1 1 & zeros >> a=x.*w a = 2 8 15 >> a=x./w a = 0.5000 0.5000 0.6000 >> b=2*x b= 2 4 6 Multiply or divide the elements of two same sized vectors term by term with the array operator .* or ./ No dot required for scalar multiplication Trigonometric functions-sin,cos,etc & elementary math functions- sqrt,exp,log,etc operate on vectors term by term Y=sin(x); z=sqrt(x).*y;

Exercises: 1.Create a vector t with 10 elements:2,4,6,.. & compute the following quantities: sin(t 2 ) a)x=tsin(t) b) c = 1+ t

Working with Matrices >> A=[1 2 3; 4 5 6; 7 8 9] A= 1 2 3 4 5 6 7 8 9 >> A(2,1) >> B=A(2:3,1:3) ans = 4 B= 4 5 6 7 8 9 B(:,2)=[]-deletes 2nd column A Transpose eye(m,n) zeros(m,n) m by n matrix of zeros help elmat help specmat

Matrices are entered row-wise. Rows are separated by semicolons and columns are separated by spaces or commas Element Aij is accessed as A(i,j) Submatrix of A -by using range specifiers. : as a row or column index specifies all rows or columns of the matrix >> B=A(2:3,:) B=4 5 6 7 8 9

Row or column of a matrix deleted by setting it to a null vector []. diag(A)-diagonal of matrix A as a vector m by n matrix with 1s on the main diagonal ones(m,n) rand(m,n) m by n matrix of ones m by n matrix of random numbers ROT90, FLIPUD, FLIPLR, FLIPDIM, TRIL,TRIU,RESHAPE Hadamard,hankel,hilb,pascal,toeplitz,vander,magic

Working with images Create an image using, say, paintbrush (attributes -> 100 pixels for height & width) & save as eg., pall1.jpeg or pall.bmp. Load the data file using File -> Import Data from MATLAB menu file & click finish. Observe a variable by name pall created in the workspace >> pall(20:23,13:16) ans = 255 255 255 255 255 255 255 255 255 255 128 128 255 255 128 128 > save imagdata pall pall1 >> clear >> load imagdata File watpsnr.m n=[1 2 3 4]; d=[2 3 4 5 8]; bode(n,d) grid rlocus(n,d) Extract a part of this image matrix. >> pall1(20:23,13:16) ans = 249 255 244 249 255 216 245 243 169 239 237 140 253 226 170 138