Introduction to MATLAB MATLAB is all of the following: 1. Computational environment 2. Plotting software 3. Programming language Typical applications: 1. 2. 3. 4. Calculations Engineering/Scientific programs Audio processing Image processing 1 % Scalars x = 1.23; y = pi; disp(x) format long disp(y) format short disp(y) 1.2300 3.141592653589793 3.1416 2 % Scalar arithmetic x = 3; y = 2; disp(x+y) disp(x-y) disp(x*y) disp(x/y) disp(x^y) % % % % % addition: subtraction: multiplication: division: power: + * / ^ 5 1 6 1.5000 9 3 Exercise 1. Calculate 4 times 5. 2. Raise 2 to the power 10. Hints: * (multiplication) ^ (raising to a power) 4 % vectors x = [1,2,3]; % row vector x = [1 2 3]; % commas can be replaced by spaces disp(x) disp(length(x)) disp(size(x)) y = [1;2;3]; % column vector disp(y) disp(size(y)) 1 2 3 3 1 3 1 2 3 3 1 5 % Transpose x = [1 2 3]; y = x'; % transpose of real vector x disp(y) z = y'; disp(z) 1 2 3 1 2 3 6 Exercise 1. 2. Create a row vector consisting of the whole numbers (integers) from 0 to 5. Display it. Have MATLAB identify the size of this “matrix.” 3. Convert your row vector into a column vector. Display it. 4. Have MATLAB identify the size of this new “matrix.” Hints: size() ' (transpose) 7 % Concatenation of row vectors x = [1 2 3]; y = [4 5 6]; z = [x y]; % concatenate row vectors disp(z) z = cat(2,x,y); % concatenate by adding columns % this accomplishes the same thing as above disp(z) 1 2 3 4 5 6 1 2 3 4 5 6 8 % Concatenation of column vectors x = [1; 2]; y = [3; 4]; z = [x; y]; % concatenate column vectors disp(z) z = cat(1,x,y); % concatenate by adding rows % this accomplishes the same thing as above disp(z) 1 2 3 4 1 2 3 4 9 Exercise 1. Create a row vector consisting of the whole numbers (integers) from 1 to 5 and a second row vector consisting of the whole numbers 6 to 8. 2. Concatenate these two row vectors into one long row vector using two different methods: square brackets the function cat 3. Create the transpose of each of the two component row vectors that you created in step 1. Concatenate these two column vectors into one long column vector using two different methods: square brackets the function cat 4. 10 MATLAB Documentation for sum sum Sum of array elements Syntax B = sum(A) B = sum(A,dim) My comments: A is a matrix, and dim is either 1 or 2. If A is a vector, then a scalar (the sum of all elements) results. If A is a matrix (with at least 2 rows and at least 2 columns), then either a row (dim = 1) or a column (dim = 2) vector results. 11 % sum function with vector argument x = [1 2 3 4]; % row vector disp(x) disp(sum(x)) y = [1; 2; 3]; % column vector disp(y) disp(sum(y)) 1 2 3 4 10 1 2 3 6 12 % The functions maximum and minimum x = [1 2 3]; % The functions maximum and minimum work equally well for % row and column vectors. disp(max(x)) disp(max(x')) disp(min(x)) disp(min(x')) 3 3 1 1 13 Exercise Create a column vector consisting of the whole numbers 0 to 10. Have MATLAB find the length of this vector, the sum of its elements, the maximum and minimum elements. Hints: length() sum() max() min() 14 % Create vectors with the functions zeros and ones x = zeros(1,5); disp(x) y = zeros(2,1); disp(y) y = ones(1,4); disp(y) 0 0 0 0 1 1 1 0 0 0 1 15 % Create row vectors with the colon operator a = 0:2:10; % start at 0, increment by 2, end at 10 disp(a) b = 0:5; % by default, increment by 1 disp(b) c = 0:-1:-5; disp(c) 0 2 4 6 8 10 0 1 2 3 4 5 0 -1 -2 -3 -4 -5 16 % Create row vectors with linspace and logspace x = linspace(0,1,5); % 5 values: 0 through 1 disp(x) y = logspace(0,4,5); % 5 values: 10^0 through 10^4 disp(y) 0 0.2500 1 0.5000 10 0.7500 100 1.0000 1000 10000 17 Exercise 1. Create a column vector of length 6, with each element a 0. 2. Create a row vector consisting of the odd integers 1 through 11, using the colon operator. 3. Create a row vector of 21 equally-spaced values between 0 and 10. Create a row vector of 7 logarithmically-spaced values between 1 and 1,000,000. 4. Hints: zeros() linspace() logspace() 18 % Vector input to built-in mathematical function x = linspace(0,pi,5); disp(x) y = sin(x); % because x is a vector, sin produces a vector disp(y) 0 0.7854 1.5708 2.3562 3.1416 0 0.7071 1.0000 0.7071 0.0000 19 % Basic plot x = linspace(0,4,41); y = sqrt(x); figure(1) plot(x,y,'-b') axis([0 4 0 2]) saveas(1,'basic','png') % Bigger font size and thicker lines x = linspace(0,4,41); y = sqrt(x); figure(2); plot(x,y,'-b') axis([0 4 0 2]) set(gca,'FontSize',24) set(findobj(2,'LineWidth',0.5),'LineWidth',2) % thick lines saveas(2,'better','png') Exercise Create a plot of the squaring function (𝑦 = 𝑥 2 ). Experiment with different ranges for the axes. For example, you could start with: axis([0 2 0 4]) For this exercise, you needn’t worry about appearance. In other words, you don’t have to set font size or line thickness. Hint. Recall that we plotted square-root like this: x = linspace(0,4,41); y = sqrt(x); figure(2); plot(x,y,'-b') axis([0 4 0 2]) 22 Color Specifiers r g b c m y k w red green blue cyan magenta yellow black ‘white’ 23 Line Style Specifiers ‘-’ solid line ‘--’ dashed line ‘:’ dotted line ‘-.’ dash-dot line 24 Exercise Create a plot of the common logarithm (base 10 logarithm), 𝑦 = log(𝑥). In MATLAB the common logarithm function is log10. Experiment with using different line style specifiers and different colors. Hint. Recall that we plotted square-root like this: x = linspace(0,4,41); y = sqrt(x); figure(2); plot(x,y,'-b') axis([0 4 0 2]) 25 % Two curves x = linspace(0,4,41); y = sqrt(x); z = x./2; figure(5); plot(x,y,'-k',x,z,'--b') % 2 curves on same axes axis([0 4 0 2]) set(gca,'FontSize',24) set(findobj(5,'LineWidth',0.5),'LineWidth',2) saveas(5,'curves','png') Using an Index to Address Elements of an Array In the C/C++ programming language, an index starts at 0 and elements of an array are addressed with square brackets [∙]: 8 2 -3 7 -1 ↑ ↑ ↑ ↑ ↑ x[0] x[1] x[2] x[3] x[4] In MATLAB, an index starts at 1 and elements of an array are addressed with parentheses (∙): 8 2 -3 7 -1 ↑ ↑ ↑ ↑ ↑ x(3) x(4) x(5) x(1) x(2) 27 Square Brackets in MATLAB In MATLAB, square brackets [∙] are used for building arrays, such as vectors, matrices, and three-dimensional arrays. For example, x = [1 2 3]; y = [4 5 6]; z = [x y]; disp(z) 1 2 % concatenate row vectors 3 4 5 6 28 % Indices (spelling lesson: 1 index, 2 or more indices) x = zeros(1,5); x(1) = 8; % change element with index 1 (first element) disp(x) x(2:5) = 7; % change elements having indices 2 through 5 disp(x) y = 1:4; x(2:5) = y; disp(x) 8 0 0 0 0 8 7 7 7 7 8 1 2 3 4 29 Exercise 1. Create a row vector of length 8, with each element a 0. 2. Create a row vector consisting of the whole numbers 1 to 4. 3. In the row vector of step 1, replace the last 4 elements with the vector of step 2. 30