Вы находитесь на странице: 1из 9

University of Engineering & Technology Peshawar, (Mardan Campus).

Course: Control Systems


Supervised by: Engr. Aftab Ahmad Khan (M.Sc Wireless Communications)

MATLAB Tutorial

Getting Started
Starting MATLAB Click the MATLAB icon on Windows to start MATLAB command window, which gives an interactive interface. The top menu bar can be used to open or write a M-file, which will be addressed later. Once started, MATLAB will provide some introductory remarks and pop up the MATLAB prompt >>. Help Facility By typing help, help <topics>, you can get on-line help. help HELP topics: matlab\general matlab\ops matlab\lang matlab\elmat matlab\elfun matlab\specfun matlab\matfun matlab\datafun matlab\polyfun matlab\funfun matlab\sparfun help ode23 ODE23 Solve non-stiff differential equations, low order method. [T,Y] = ODE23('F',TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the system of differential equations y' = F(t,y) from time T0 to TFINAL with initial conditions Y0. 'F' is a string containing the name of an ODE file. Function F(T,Y) must return a column vector. Each row in solution array Y corresponds to a time returned in column vector T. To obtain solutions at specific times T0, T1, ..., TFINAL (all increasing or all decreasing), use TSPAN = [T0 T1 ... TFINAL]. Saving working space Terminating a MATLAB session deletes the variables in the workspace. Before quitting, you can save the workspace for later use by typing save, which saves the workspace as matlab.mat. You can save with other filenames or to save only selected variables. save temp x y - General purpose commands. - Operators and special characters. - Programming language constructs. - Elementary matrices and matrix manipulation. - Elementary math functions. - Specialized math functions. - Matrix functions - numerical linear algebra. - Data analysis and Fourier transforms. - Interpolation and polynomials. - Function functions and ODE solvers. - Sparse matrices.

which saves the current variable x, y into temp.mat. To retrieve all the variables from the file named temp.mat, type load temp

Exit Exit MATLAB by typing quit or exit

Fundamental Expressions/Operations
MATLAB uses conventional decimal notion, builds expressions with the usual arithmetic operators and precedence rules: x = 3.421 x= 3.4210 y = x+8.2i y= 3.4210 + 8.2000i z = sqrt(y) z= 2.4805 + 1.6529i p = sin(pi/2) p= 1 Matrix Operations Matrix operations are fundamental to MATLAB. Within a matrix, columns are separated by space, and the rows are separated by semicolon;. For example

A = [1 2 3; 4 5 6; 7 8 9] A= 1 4 7 2 5 8 3 6 9

B = ones(3,3) B= 1 1 1 A+B ans = 2 5 8 A' ans = 1 2 3 4 5 6 7 8 9 3 4 6 7 9 10 1 1 1 1 1 1

The following matrix operations are available in MATLAB: + * ^ \ / addition subtraction matrix multiplication power transpose left division right division

The operations, , ^, \, and /, can be made to operate entry-wise by preceding them by a period. Building Matrix Convenient matrix building functions are

eye identity matrix zeros matrix of zeros ones matrix of ones diag diagonal matrix triu upper triangular part of a matrix tril lower triangular part of a matrix rand randomly generated matrix For example, A = eye(3) A= 1 0 0 0 1 0 0 0 1

B = zeros(3,2) B= 0 0 0 0 0 0

C = rand(3,1) C= 0.9501 0.2311 0.6068 Matrices can be built from blocks. For example, D = [A B C D] D= 1.0000 0 0 0 1.0000 0 0 0 1.0000 0 0 0 0 0.9501 0 0.2311 0 0.6068

Retrieving part or an element of a matrix

D(:,6) ans = 0.9501 0.2311 0.6068 D(1, :) ans = 1.0000 D(1,6) ans = 0.9501 Other functions for colon notation: E = 0.3:0.4:1.5 E= 0.3000 0.7000 1.1000 1.5000 Vector Functions Certain functions in MATLAB are vector functions, i.e., they operate essentially on a vector (row or column). For example, the maximum entry in a matrix D is given by max(max(D)). max(D) ans = 1.0000 1.0000 1.0000 max(max(D)) ans = 1 A few of these functions are: max, min, sort, sum, prod, mean, std, any, all. 0 0 0.9501 0 0 0 0 0.9501

Matrix functions The most useful matrix functions are eig svd inv det size norm cond rank For example F = rand(3,3) F= 0.4860 0.4565 0.4447 0.8913 0.0185 0.6154 0.7621 0.8214 0.7919 eig(F) ans = 1.7651 0.0310 -0.4997 rank(F) ans = 3 cond(F) ans = 69.1503 inv(F) ans = eigenvalues and eigenvectors singular value decomposition inverse determinant size 1-norm, 2-norm, F-norm condition number in the 2-norm rank

17.9446 -0.1385 -9.9689 8.6579 -1.6802 -3.5560 -26.2485 1.8760 14.5444

Plotting Figures
Creating a Figure If y is a vector, plot (y) produces a linear graph of the elements of y versus the index of the elements of y. If you specify two vectors as arguments, plot(x,y) produces a graph of y versus x. t = 0:pi/100:2*pi; x = sin(t); y1 = sin(t+0.25); y2 = cos(t-0.25); figure(1) % open a figure and make it current, not necessarily plot(x,y1,'r-',x,y2, 'g--') title('sin-cos plots') xlabel('x=sin(t)') ylabel('y')
1 sin-cos plots

0.5

-0.5

-1 -1

-0.5

0 x=sin(t)

0.5

By plotting multiple figures on the graph, one alternative way is to use hold on and hold off commands, plot(x,y1, 'r-') hold on

plot(x,y2, 'g--') hold off Other types of plots: loglog plot using logarithmic scales for both axes semilogx plot using a logarithmic scales for x-axis and linear scale for the y-axis semilogy figure out yourself Line styles, markers, and color Symbol y m c r g b w k Color yellow magenta cyan red green blue white black Symbol . o x + * : -. -Linestyle point circle x-mark plus star solid dotted dashdot dashed

Exporting a Figure 1) Cut and paste: click Edit at the top menu bar of the figure window, then click Copy Figure, paste the figure wherever you want. 2) Save as a file: print tiff deps (print as a postscript file). Type help print to find out more options.

M-files
MATLAB can execute a sequence of statements in a file. Such files are called M-files because they must have the file type of .m as the last part of their filename. There are two types of M-files: script files and function files. Script files A script file consists of a sequence of normal MATLAB statements. For example, type all the commands for generating the figure into a single script file and save as sineplot.m. Then the MATLAB command sineplot will cause the statements in the file to be executed. Try it. Function files

Вам также может понравиться