Sunteți pe pagina 1din 32

Linear algebra

Vectorization of code

X=[1 2 3 4 5] Y=zeros(size(X)); For I=1 : numel(X) Y(i)=X(I)^2 End

X=[1 2 3 4 5] Y=x.^2

Graph- Functions of one variable

To make a graph of y = sin(t) on the interval


t = 0 to t = 10 we do the following: t = 0:.3:10; y = sin(t); plot(t,y)

Functions of two variables Here is how we graph the function z(x,y) = x exp( x^2 - y^2): [x,y] = meshgrid(-2:.2:2, -2:.2:2); % Generate X and Y arrays for three-dimensional plots. The first command creates a matrix whose entries are the points of a grid in the square -2 <= x <= 2, -2 <= y <= 2. The small squares which make up the grid are 0.2 units wide and 0.2 unit tall. z = x .* exp(-x.^2 - y.^2); The second command creates a matrix whose entries are the values of the function z(x,y) at the grid points mesh(z) ;% create wireframe parametric surfaces specified by X, Y, and Z

Programming Constructs: for ,while, if


If(condition statement) (matlab commands) end
Ex1 :a = 2; b = 3; if (a<b) j = -1; end Ex2: a = 4; b = 4; if (a<b) j = -1; else if (a>b) j = 2; else j = 3 end

In this example the differential equation. dy/dx=x|y|, y(0)=1, is approximated using Euler's Method:

h = 0.001;x = [0:h:2]; y = 0*x;y(1) = 1; i = 1;size(x); max(size(x)) while(i<max(size(x))) y(i+1) = y(i) + h*(x(i)-abs(y(i))); i = i + 1; end plot(x,y,'go') Hold ;plot(x,y)

For(iteration) (iterative statements) end


for j=1:4, j end

For loop

A = [ [1 2 3]' [3 2 1]' [2 1 3]'] B = A; for j=2:3, A(j,:) = A(j,:) - A(j-1,:) end

Ex: B = [ [1 2 3]' [3 2 1]' [2 1 3]'] for j=2:3, for i=j:3, B(i,:) = B(i,:) - B(j-1,:)*B(i,j-1)/B(j-1,j-1) end end

Another example where loops come in


handy is the approximation of differential equations. The following example approximates the differential equation y'=x^2-y^2, y(0)=1, using Euler's Method. First, the step size, h, is defined. Once done, the grid points are found, and an approximation is found. The approximation is simply a vector, y, in which the entry y(j) is the approximation at x(j).

Eulers method-dy/dx=x^2-y^2,y(0)=1

h = 0.1; x = [0:h:2]; y = 0*x; y(1) = 1; size(x) for i=2:21, y(i) = y(i-1) + h*(x(i-1)^2 - y(i-1)^2); end plot(x,y) plot(x,y,'go') plot(x,y,'go',x,y)

Built in functions

Diag End Eye Find Istrue,Isequal Ndims,Reshape Prod Sort,Sum Tril,Triu

Matrix functions

Linspace Fliplr Flipud Repmat B = repmat(A,m,n) creates a large matrix B consisting of an m-by-n tiling of copies of A. The statement repmat(A,n) creates an n-by-n tiling. Rot90 B = rot90(A,k) rotates matrix A counterclockwise by k*90 degrees, where k is an integer. Squeeze B = squeeze(A) returns an array B with the same elements as A, but with all singleton dimensions removed. A singleton dimension is any dimension for which size(A,dim) = 1.

2-by-1-by-3 array- Y = rand(2,1,3) Squeeze(Y)

X=[1 2 3 4 5] x=size(X) size(x) Ndim-length(size(x)) Numel: prod(size(X))

Reshape a 3-by-4 matrix into a 2-by-6


matrix:A = 1 4 7 10 2 5 8 11 3 6 9 12 B = reshape(A,2,6)

Vectors, matrices and arrays


MAGIC(N) is an N-by-N matrix constructed from
the integers 1 through N^2 with equal row, column, and diagonal sums. Column or row matrix as a vector 2D matrix as an array repmat(magic(2),2,3) v=repmat(NaN,size(X)) Linspace(upper,lower,n) X=linspace(1,10,10)

rotate
a=[1 2 3 ;4 5 6;] b=rot90(a,1) or b=a(:,3:-1:1).' c=rot90(a,2)

a = magic(3); b = pascal(3); c = cat(4,a,b)

Arithmetic and plotting



A=[ 2 3 5 7 9 6] B=A+2 Plot(A) hold plot(B) ;grid on Bar(B)

B=[1 4 7 ;9 4 3 ;2 5 6] A=B C=A*B D=A.*B X=inv(A) I=Inv(A)*A Y=eig(A) p=round(poly(ans))-roots of characteristic equation

Roots(p)is equal to Eig(A) Sqrt(-1) Log(0) Humps and peaks Fplot and Gplot TIC Start a stopwatch timer. TOC Read the stopwatch timer. ETIME Elapsed time CLOCK = [year month day hour minute seconds]

U=[10 9 8] V=[1 2 3] U+V,U-V U*V,U.*V U/V-Slash or right division. U/V is the matrix division of U into V, which is roughly the same as U*INV(V) U\V-Backslash or left division. U\V is the matrix division of U into V, which is roughly the same as INV(U)*V

Inner and outer products


U*V(inner product) U*V(outer product) Find(u<4)

Dot and cross



Concatenate-cat b=[2 2;4 5] A=[1 2 3; 4 5 6] [A b] Dot product and cross product A=[1; 4 ;3; 5] B=[ 7;9;5;0] Dot[A,B]=dot[B,A] Length=sqrt(dot(A,A)) Norm cosU=v.w/(length(v)*length(w))

U=[1;2;3] V=[7;8;9] Cross(U,V) Dot(cross ans and primary vectors is zero)

v1=1:3;v2=4:6;v3=7:9; N=[v1;v2;v3] tril(N) triu(N) sum(N) diag(N) Sum(diag(N)) prod(N) fliplr(N) load m.txt

Linear equations
Now consider a linear equation
ax + by = p cx + dy = q We can write this more compactly as AX = B coefficient matrix A is ab cd If A is invertible, X = (1/A)B, or, using Matlab notation, X = A\B

Solving linear equations


Numerical solution of simultaneous linear
equations using the matrix backslash (\) method MATLAB uses Gaussian elimination to solve systems of linear equations via the backslash matrix command (\). As an example, consider the following three equations: 2x-3y+4z=5 y+4z+x=10 -2z+3x+4y=0

clear, C = [2,-3,4; 1,1,4; 3,4,-2], B = [5; 10; 0] b is a column vector. Solve for x, y and z using A = C\B, x = A(1), y = A(2), z = A(3) Verify by calculating C*A B (Note that these are matrix operations; you MUST NOT use a dot (.) before * or \)

Polynomials

F(x)=x^3-3x-2 Entering this polynomial P=[1 0 2 5] Polyval(P,2) K=roots(P)

Fitting a curve

Data to be fit x=[1 2 3 4 5] y=[23,78.9,367 57.99 82] h=polyfit(x,y,2)(change order) x2=1:0.5:5;y2=polyval(h,x2) plot(x,y,'o',x2,y2);hold ;grid on

Use in Control Theory


Num=[1 0 0] Den=[ 1 3 2] tf(Num,Den)

Applications
Data fitting Solving linear equations Visualization of rotation

S-ar putea să vă placă și