230 likes | 455 Views
Introduction to MATLAB. Dr. Lei Zheng Department of Biomedical, Industrial and Human Factors Engineering Wright State University. What is Matlab?. Matlab ( Mat rix lab oratory) An interactive software system for numerical computations and graphics
E N D
Introduction to MATLAB Dr. Lei Zheng Department of Biomedical, Industrial and Human Factors Engineering Wright State University
What is Matlab? • Matlab (Matrix laboratory) • An interactive software system for numerical computations and graphics • especially designed for matrix computations (solving systems of linear equations, computing eigenvalues and eigenvectors, factoring matrices, etc. ) • BASIC-like syntax • basic data type: 2- dimensional floating-point matrix • comprehensive toolboxes for easy access to standard algorithms from many fields: • popular for experimental/rapid-prototype number crunching • widely used as a visualization and teaching tool
What Matlab is NOT? • not a strong general purpose programming language • limited support for other data structures • few software-engineering features; typical MATLAB programs are only a few lines long • not suited for teaching OOP • not a high-performance language (but fast matrix operators) • not freely available
MATLAB matrices (1) • Generate a “magic square” with equal row/column/diagonal sums and assign the resulting 3 × 3 matrix to variable a: >> a = magic(3) a = 8 1 6 3 5 7 4 9 2 Assignments and subroutine calls normally end with a semicolon. Without, MATLAB will print each result. Useful for debugging! Results from functions not called inside an expression are assigned to the default variable ans. Type help magic for the manual page of this library function.
MATLAB matrices (2) • zeros( ) --- Zeros matrix zeros(N) is an N-by-N matrix of zeros. zeros(M,N) is an M-by-N matrix of zeros.: >> b = zeros(3,2) • Eye( ) ----Identity matrix. eye(N) is the N-by-N identity matrix. eye(M,N) is an M-by-N matrix with 1's on the diagonal and zeros elsewhere. >> b = eye(3,2) • Ones( ) ---Ones array. ones(N) is an N-by-N matrix of ones. ones(M,N) is an M-by-N matrix of ones.. >> ones(3,2)
MATLAB matrices (3) • Rand( ) --- Uniformly distributed random numbers. rand(N) is an N-by-N matrix with random entries, chosen from a uniform distribution on the interval (0.0,1.0). rand(M,N) is M-by-N matrices with random entries. >> b = rand(1) • Randn( ) --- Normally distributed random numbers. randn(N) is an N-by-N matrix with random entries, chosen from a normal distribution with mean zero, variance one and standard deviation one. randn(M,N) is M-by-N matrices with random >> b = randn(2,3)
An Example of Matrix Computation • Given linear equations: 3x+2y+5z=7 2x+8y+7z=9 7x+3y+6z=10 >> s =inv([3, 2, 5; 2, 8, 7; 7, 3, 6])*[7, 9, 10]’
Some Amazing Functions (1) • limit( ) ----limit of an expression. limit(F,x,a) takes the limit of the symbolic expression F as x -> a. limit(F,x,a,'right') or limit(F,x,a,'left') specify the direction of a one-sided limit. Examples: (1) >> limit(sin(x)/x, x, 0) %% returns 1 (2) >> syms x h; limit((sin(x+h)-sin(x))/h,h,0) %%returns cos(x)
Some Amazing Functions (2) • int( ) ---- Integrate. int(S,v) is the indefinite integral of S with respect to v. int(S,v,a,b) is the definite integral of S with respect to v from a to b. Examples: (1) >>syms x; int(1/(1+x^2), x) %%returns atan(x) (2) >> syms x1; int(x1*log(1+x1),0,1) %%returns 1/4
Plotting (1) • >> x=-50:0.1:50; y=x.^2; plot(x,y); title('y=x^2');
Plotting (2) • >> fplot('sin(3*x)', [0,2*pi])
Functions and m-files To define a new function, write into a file fname.m the lines function v= fname(x) Only the function that has the same name as the m-file in which it is defined can be called from outside the file; all other functions are only visible inside the file. The function keyword sets the variable whose value will be returned and lists the parameter variables. The m-file must be in the current directory (cd) or MATLAB’s search path (path) to become accessible. M-files can also contain just sequences of statements instead of a func- tion definition. These are called simply by typing their name.
References • http://www.mathworks.com/access/helpdesk/help/techdoc/helptoc.html • Introduction to MATLAB, www.cl.cam.ac.uk/teaching/2006/UnixTools/matlab-slides.pdf