Introduction to MATLAB Part3 By Maha Al Mousa
Introduction to MATLAB Part(3) By: Maha Al. Mousa
Defining Matrices Entering a Matrix MATLAB Format >> A = [2 -3 5; -1 4 5] A= 2 -3 5 -1 4 5
Entering a Row Vector MATLAB Format >> x = [1 4 7] x= 1 4 7
Entering a Column Vector or >> x = [1 4 7]' x= 1 4 7
Some special Matrices
Matrix Addition and Subtraction >> C = A + B >> D = A - B If the matrices have different sizes, the message is ? ? ? Error using ==>
Exaple
Matrix operations array multiplication >> F=A. *B >> F=B. *A
Example >> E = A. *C E= 4 -21 15 -1 -16 6
Matrix Multiplication
Inverse Matrix >> B = inv(A) >> A = [1 2 -1; -1 1 3; 3 2 1] A= 1 2 -1 -1 1 3 3 2 1
Determinant of a Matrix >> a = det(A) >> A = [1 2 -1; -1 1 3; 3 2 1] A= 1 2 -1 -1 1 3 3 2 1 >> a = det(A) a= 20
Solving Linear Systems –
Example Assume that A is still in memory. >> b = [-8; 7; 4] b= -8 7 4 >> x = inv(A)*b x= 2. 0000 -3. 0000 4. 0000
A(: , n) Refers to the elements in all the rows of column n of the matrix A A(n, : ) Refers to the elements in all the columns of row n n of the matrix A A(: , m, n) Refers to the elements in all the rows between columns m and n of the matrix A A(m, n, : ) Refers to the elements in all the columns between rows m and n of the matrix A A(m: n, p: q) Refers to the elements in row m through n and columns p through q of the matrix A
length(A) Returns the number of elements in the vector A >>A=[5 9 2 4]; >>length(A) Ans = 4 size(A) Returna a row vector [m, n], where m and n are the size m*n of the array A >>A=[6 1 4 0; 5 19 6 8] A= 6 1 4 0 5 19 6 8 >>size(A) Ans = 2 4 reshape(A, m, n) Rearrange a matrix A has r rows and s columns to have m rows and n columns. R times s must be equal to m times n. >>A=[5 1 6; 8 0 2] A= 5 1 6 8 0 2 >>B=reshape(A, 3, 2) B= 5 0 8 6 1 2
Plot function >>plot(x, y) x = 0: pi/100: 2*pi; y = sin(x); plot(x, y)
xlabel('x') ylabel('sin(x)') title('Plot of the Sine Function')
By adding a third input argument to the plot function, you can plot the same variables using a red dashed line. plot(x, y, 'r--')
Plot function in 3 D §first create a set of (x, y) points over the domain of the function using meshgrid. [X, Y] = meshgrid(-2: . 2: 2); § Z = X. * exp(-X. ^2 - Y. ^2); §Then, create a surface plot. surf(X, Y, Z)
mesh produces wireframe surfaces that color only the lines connecting the defining points. surf displays both the connecting lines and the faces of the surface in color.
- Slides: 22