730 likes | 753 Views
Computer programming. Dr. Ivan A. Hashim. MATLAB.
E N D
Computer programming Dr. Ivan A. Hashim
MATLAB MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include: • Math and computation • Algorithm development • Data acquisition • Modeling, simulation, and prototyping • Data analysis, exploration, and visualization • Scientific and engineering graphics • Application development, including graphical user interface building
Matrices and Arrays • Entering Matrices • Separate the elements of a row with blanks or commas. • Use a semicolon (;) to indicate the end of each row. • Surround the entire list of elements with square brackets, [ ]. A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
Matrices and Arrays >> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] A = 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
Matrices and Arrays • Subscripts >> A(1,3)+A(2,2)+A(3,4)+A(4,1) ans = 28
Matrices and Arrays • The Colon Operator (:) >> 1:10 1 2 3 4 5 6 7 8 9 10 >> 1:2:10 1 3 5 7 9 >> 100:-7:50 100 93 86 79 72 65 58 51
Matrices and Arrays • The Colon Operator (:) >> A(2,:) >> A(:,3) 5 10 11 8 ans = 2 11 7 14
Matrices and Arrays • The Colon Operator (:) >> A(1:2,1:3) ans = 16 3 2 5 10 11
Matrices and Arrays • The Colon Operator (:) >> A(1:2,2:end) ans = 3 2 13 10 11 8
Matrices and Arrays • The Colon Operator (:) >> A(2:3,3:4) ans = 11 8 7 12
Matrices and Arrays • The Colon Operator (:) >> A(1:3,2:4) ans = 3 2 13 10 11 8 6 7 12
Matrices and Arrays • The Colon Operator (:) >> A([1 3],:) ans = 16 3 2 13 9 6 7 12
Matrices and Arrays • The Colon Operator (:) >> A(:,[1 4]) ans = 16 13 5 8 9 12 4 1
Matrices and Arrays • The Colon Operator (:) >> A([1 3],[1 4]) ans = 16 13 9 12
Matrices and Arrays Functions >> det(A) >> inv(A) ans = 1.0871e-12 ans = 1.0e+15 * 0.1251 0.3753 -0.3753 -0.1251 -0.3753 -1.1259 1.1259 0.3753 0.3753 1.1259 -1.1259 -0.3753 -0.1251 -0.3753 0.3753 0.1251
Matrices and Arrays Functions >> diag(A) ans = 16 10 7 1
Matrices and Arrays Functions >> length(A) >> length(B) ans = 4 ans = 8
Matrices and Arrays Functions >> numel(A) >> numel(B) ans = 16 ans = 8
Matrices and Arrays Functions >> size(A) >> size(B) ans = 4 4 ans = 1 8
Matrices and Arrays Functions >> max(A) >> max(B) ans = 16 15 14 13 ans = 9 >> max(max(A)) ans = 16
Matrices and Arrays Functions >> min(A) >> min(B) ans = 4 3 2 1 ans = 0 >> min(min(A)) ans = 1
Matrices and Arrays Functions >> sort(B) >> sort(B,'ascend') ans = 0 1 2 3 5 6 8 9 >> sort(B,'descend') ans = 9 8 6 5 3 2 1 0
Matrices and Arrays Functions >> sort(A) or sort(A,1) >> sort(A,2) ans = 4 3 2 1 5 6 7 8 9 10 11 12 16 15 14 13 ans = 2 3 13 16 5 8 10 11 6 7 9 12 1 4 14 15
Matrices and Arrays Functions >> sum(A) >> sum(B) ans = 34 343434 ans = 34 >> sum(sum(A)) ans = 134
Matrices and Arrays Functions >> tril(A) >> triu(A) ans = 16 0 0 0 5 10 0 0 9 6 7 0 4 15 14 1 ans = 16 3 2 13 0 10 11 8 0 0 7 12 0 0 0 1
Matrices and Arrays Functions >> zeros(3) >> magic(3) ans = 0 0 0 0 0 0 0 0 0 ans = 8 1 6 3 5 7 4 9 2 >> ones(3) >> zeros(3,2) ans = 1 1 1 1 1 1 1 1 1 ans = 0 0 0 0 0 0
Matrices and Arrays Functions >> rand(2,3) >> rand(2) ans = 0.6324 0.2785 0.9575 0.0975 0.5469 0.9649 ans = 0.8147 0.1270 0.9058 0.9134 >> randn(3) ans = -1.3499 -0.0631 -0.1241 3.0349 0.7147 1.4897 0.7254 -0.2050 1.4090
Matrices and Arrays Functions >> mean(A) >> mean(B) ans = 8.5000 8.5000 8.5000 8.5000 ans = 4.2500
Matrices and Arrays Functions >> B' ans = 5 6 8 9 1 3 2 0
Matrices and Arrays Functions >> A' ans = 16 5 9 4 3 10 6 15 2 11 7 14 13 8 12 1
Matrices and Arrays Functions >> A+D >> A*D ans = 18 5 4 15 7 12 13 10 11 8 9 14 6 17 16 3 ans = 68 686868 68 686868 68 686868 68 686868
Matrices and Arrays Functions >> A+5 >> A*3 ans = 21 8 7 18 10 15 16 13 14 11 12 17 9 20 19 6 ans = 48 9 6 39 15 30 33 24 27 18 21 36 12 45 42 3
Matrices and Arrays Functions >> A^2 >> A.^2 ans = 341 285 261 269 261 301 309 285 285 309 301 261 269 261 285 341 ans = 256 9 4 169 25 100 121 64 81 36 49 144 16 225 196 1
Matrices and Arrays Functions >> A.*D >> A./D ans = 32 6 4 26 10 20 22 16 18 12 14 24 8 30 28 2 ans = 8.0 1.5 1.0 6.5 2.5 5.0 5.5 4.0 4.5 3.0 3.5 6.0 2.0 7.5 7.0 0.5
Matrices and Arrays Functions >> A.^D >> sqrt(A) ans = 256 9 4 169 25 100 121 64 81 36 49 144 16 225 196 1 ans = 4.0000 1.7321 1.4142 3.6056 2.2361 3.1623 3.3166 2.8284 3.0000 2.4495 2.6458 3.4641 2.0000 3.8730 3.7417 1.0000
Matrices and Arrays Functions >> flip(A) >> flip(B) ans = 4 15 14 1 9 6 7 12 5 10 11 8 16 3 2 13 ans = 0 2 3 1 9 8 6 5
Matrices and Arrays Functions >> fliplr(A) >> fliplr(B) ans = 13 2 3 16 8 11 10 5 12 7 6 9 1 14 15 4 ans = 0 2 3 1 9 8 6 5
Variables • MATLAB does not require any type declarations or dimension statements • When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage • If the variable already exists, MATLAB changes its contents num_students = 25 • Variable names consist of a letter, followed by any number of letters, digits, or underscores • MATLAB uses only the first 31 characters of a variable name. MATLAB is case sensitive
Numbers • MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. Some examples of legal numbers are 3 -99 0.0001 9.6397238 1.60210e-20 6.02252e23 1i -3.14159j 3e5i • All numbers are stored internally using the long format specified by the IEEE floating-point standard. Floating-point numbers have a finite precision of roughly 16 significant decimal digits and a finite rangeof roughly • 10-308 to 10+308.
Basic Plotting Functions • Creating a Plot x = 0:pi/100:2*pi; y = sin(x); plot(x,y)
Basic Plotting Functions • xlabel('x = 0:2\pi') • ylabel('Sine of x') • title('Plot of the Sine Function','FontSize',12)
Basic Plotting Functions • text(1,-1/3,'{\it Sinwave Example.}')
Basic Plotting Functions • Setting Axis Limits • axis([xminxmaxyminymax]) • axis([-1 7 -2 2])