MATLAB Basics

advertisement

MATLAB Basics

What is MATLAB? MATrix LABoratory

Desktop: three parts: a)Workspace b)Command History: c)Current directory/Workspace variables

Tips in workspace: up arrows, escape, Control + C, tic toc

Input data: Import data wizard, built-in function, manually create variables

Matrix and array construction: row vector, e.g. [1 2 3 -1] or [1,2,3,-1] column vector, e.g. [5 ; 6 ; 7 ; 8 ; -1] empty matrix = [] colon, e.g. 1:3:13 = [1 4 7 11] transpose, symbol A’ matrix indexing: usual M(i,j) or linear indexing. concatenation e.g. x=[7 8 9], y=[3 4 5 6] , z= [y x]=[3 4 5 6 7 8 9] scalar expansion, e.g. A=[2 3; -1 0], A+2 = [4 5; 1 2]

Array operations vs matrix operations:

Use .* or ./ for array operation, e.g. d= [-1 0 2], then d.^2= [1 0 4] slash operator e.g. to solve AX=B, use X= A\B to solve XA=B, use X=B/A vectorization: key feature of MATLAB

A = ones(3); x = [2;3;1]; y = [0; 0; -1];

A(:, 1:2)= [x y]

Then A= [2 0 1;3 0 1;1 -1 1] symbolic toolbox polynomials: in MATLAB, they are represented simply by their coefficients listed as a row vector in decreasing degree order, e.g. x^3 – x +5 = [1 0 -1 5] polyder: take polynomial derivative polyint: take polynomial antiderivative with constant C=0; polyval: evaluate polynomial at x

Online resources

Matrix inverses: inv(A) involves a lot of computational time, memory and it is very sensitive to round off error.

The Moore-Penrose inverse or pseudo-inverse pinv(A) coincides with inv when the latter exists, but it is more reliable computationally, it extends to non-square matrices and provides either a particular or least-square solution to a rank-deficient system.

Recall = inv(V)*A= V\A and A*inv(V)=A/V in MATLAB.

Format long, short, rat (show fractions)

Back to linear systems

For A singular square matrix (also called rank-deficient matrix):

Ax=b has either infinite solution or no solution at all.

The command pinv(A)*b provides a particular solution in the first case –the one with the least norm- and a least-square solution in the second case. rref(A): reduced row echelon form of A

Eigenvalues: [V,D] = eig(A) returns in the matrix V the matrix of normalized eigenvectors and matrix D is the diagonal matrix whose diagonal contains the eigenvalues of A. If A is diagonalizable, then A = V*D*inv(V) or A=V*D/V.

Eigshow

Jordan form: useful theoretical tool, but not very reliable computationally.

Matrices as colormaps

M-files, scripts and functions, % sign

Data structures: cells, structures

Download