1 / 30

Introduction to MATLAB

Introduction to MATLAB. MATLAB is all of the following: Computational environment Plotting software Programming language. Typical applications: Calculations Engineering/Scientific programs Audio processing Image processing. % Scalars. x = 1.23; y = pi; disp(x) format long disp(y)

talor
Download Presentation

Introduction to MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to MATLAB MATLAB is all of the following: Computational environment Plotting software Programming language Typical applications: Calculations Engineering/Scientific programs Audio processing 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

  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

  4. Exercise 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

  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

  7. Exercise 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 1 2 3 4 5 6

  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

  10. Exercise

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

  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

  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 3 1 1

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

  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

  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

  18. Exercise 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

  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. Exercise Create a plot of the squaring function (). 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])

  23. Color Specifiers

  24. Line Style Specifiers

  25. Exercise Create a plot of the common logarithm (base 10 logarithm), . 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])

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

  30. Exercise

More Related