Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.

Similar presentations


Presentation on theme: "1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations."— Presentation transcript:

1 1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations 2.Engineering/Scientific programs 3.Audio processing 4.Image processing

2 % Scalars x = 1.23; y = pi; disp(x) format long disp(y) format short disp(y) 1.2300 3.141592653589793 3.1416 2

3 % Scalar arithmetic x = 3; y = 2; disp(x+y) % addition: + disp(x-y) % subtraction: - disp(x*y) % multiplication: * disp(x/y) % division: / disp(x^y) % power: ^ 5 1 6 1.5000 9 3

4 4 Exercise 1.Calculate 4 times 5. 2.Raise 2 to the power 10. Hints: * (multiplication) ^ (raising to a power)

5 % 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

6 % 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

7 7 Exercise 1.Create a row vector consisting of the whole numbers (integers) from 0 to 5. Display it. 2.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)

8 % 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 8

9 % 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

10 10 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. 4.Concatenate these two column vectors into one long column vector using two different methods: square brackets the function cat

11 11 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. MATLAB Documentation for sum

12 % 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

13 % 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 1 13

14 14 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()

15 % 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 0 0 1 1 1 1 15

16 % 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

17 % 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 0.5000 0.7500 1.0000 1 10 100 1000 10000 17

18 18 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. 4.Create a row vector of 7 logarithmically-spaced values between 1 and 1,000,000. Hints: zeros() linspace() logspace()

19 % 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

20 % 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')

21 % 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')

22 22 Exercise 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])

23 23 rred ggreen bblue ccyan mmagenta yyellow kblack w‘white’ Color Specifiers

24 24 ‘-’solid line ‘--’dashed line ‘:’dotted line ‘-.’dash-dot line Line Style Specifiers

25 25 Exercise 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])

26 % 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')

27 27 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(1) x(2) x(3) x(4) x(5) ↑↑ ↑ ↑ ↑

28 28 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]; % concatenate row vectors disp(z) 1 2 3 4 5 6

29 % 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

30 30 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.


Download ppt "1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations."

Similar presentations


Ads by Google