1 / 71

Review of MATLAB

Review of MATLAB. The basics of MATLAB Manipulating matrices Built-in functions Plotting Functions in Matlab Mathematical operations and system of eqs . NOTE: Covers Chapts . 1-6, but you should be pretty good if you mainly focus on the first 4 chapters

tegan
Télécharger la présentation

Review of 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. Review of MATLAB • The basics of MATLAB • Manipulating matrices • Built-in functions • Plotting • Functions in Matlab • Mathematical operations and system of eqs. • NOTE: Covers Chapts. 1-6, but you should be pretty good if you mainly focus on the first 4 chapters • Slides most important: 1-48, 53-57, and 59

  2. The Basic Data Type in MATLAB: Matrices • Group of numbers arranged into rows and columns • Single Value (Scalar) • Matrix with one row and one column • Vector (One dimensional matrix) • One row or one column • Matrix (Two dimensional)

  3. To create a row vector, enclose a list of values in brackets

  4. You may use either a space or a comma as a “delimiter” in a row vector

  5. Use a semicolon as a delimiter to create a new row

  6. Shortcuts • While a complicated matrix might have to be entered by hand, evenly spaced matrices can be entered much more readily. The command  • b= 1:5 • or the command • b = [1:5] • both return a row matrix 

  7. The default increment is 1, but if you want to use a different increment put it between the first and final values

  8. To calculate spacing between elements use linspace number of elements in the array initial value in the array final value in the array

  9. “What’s the output” >>A = [1 2 3; 3 5 6] This statement stores the matrix: 1. A= 2. A= 3. A= 4. A=

  10. What’s the output? >>A = [0:0.1:0.5] This statement stores the matrix: 1. A= 2. A= A = 3. A=

  11. What’s the output? >>A = 5*[0:0.1:0.5] This statement stores the matrix: 1. A= 2. A= A = 3. A=

  12. What’s the output? >>A = linspace(0,10,3) This statement stores the matrix: 1. A= 2. A= A = 3. A=

  13. Manipulating Matrices • Defining matrices • A matrix can be defined by typing in a list of numbers enclosed in square brackets. • The numbers can be separated by spaces or commas. • New rows are indicated with a semicolon. A = [ 3.5 ]; B = [1.5, 3.1]; or B =[1.5 3.1]; C = [-1, 0, 0; 1, 1, 0; 0, 0, 2];

  14. The element value on row 2 and column 2 of matrix T Count down column 1, then down column 2, and finally down column 2 to the correct element of matrix T. Manipulating Matrices • Defining matrices • Define a matrix by using another matrix that has already been defined. • Reference an element in a matrix • Both row and column indices start from 1. S = [3.0, 1.5, 3.1]; T = [1 2 3; S] B = [1.5, 3.1]; S = [3.0, B] S = 3.0 1.5 3.1 T = 1 2 3 3.0 1.5 3.1 S(2) T(2, 2) T(4)

  15. Manipulating Matrices • Change values in a matrix • S(2) = -1.0; • changes the 2nd value in the matrix S • from 1.5 to -1.0 • Extend a matrix by defining new elements. S(4) = 5.5; Extend the matrix S to four elements instead of three. S(8) = 9.5; Matrix S will have eight values, and the values of S(5), S(6), S(7) will be set to 0. T(3, 3) = 10; T(4, 5) = 20;

  16. Manipulating Matrices • Using the colon operator • Define an evenly spaced matrix • H = 1:8 --- The default spacing is 1 • time = 0.0:0.5:3.0 --- The middle value becomes the spacing. • Extract data from matrices • x = M( :, 1) --- extract column 1 from matrix M • y = M( :, 4) --- extract column 4 from matrix M • z = M(2, : ) --- extract row 2 from matrix M • a = M(2:3, : ) --- extract rows 2 and 3 from matrix M • b = M( :, 2:4) --- extract column 2 to column 4 from matrix M • c = M(2:3, 3:5) --- extract not whole rows or whole columns • from matrix M • Converts a two dimensional matrix to a single column • M( : ) M = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7

  17. What’s does B store? >>A = [5 6 ; 7 8] >>B = A(:,2) This statement stores the matrix: 2. B= 7 8 B= 6 8 4. B= 7 8 3. B= 5 7

  18. NEXT TOPIC: MATLAB BUILT-IN FUNCTIONS

  19. MATLAB uses function names consistent with most major programming languages For example • sqrt • sin • cos • log

  20. Function Input can be either scalars or matrices

  21. Trigonometric Functions sin(x) sine cos(x) cosine tan(x) tangent asin(x) inverse sine sinh(x) hyperbolic sine asinh(x) inverse hyperbolic sine sind(x) sine with degree input asind(x) inverse sin with degree output

  22. Data Analysis max(x) min(x) mean(x) median(x) sum(x) prod(x) sort(x)

  23. When x is a matrix, the max is found for each column

  24. max value element number where the max value occurs Returns both the smallest value in a vector x and its location in vector x.

  25. Vector of maximums Vector of row numbers Returns a row vector containing the minimum element from each column of a matrix x, and returns a row vector of the location of the minimum in each column of matrix x.

  26. What does B store? >>A = [1 2 3; 3 5 6;1 2 1] >>B = max(A) 3 6 2 a) 6 3 5 6 6 5 6

  27. What does C store? >>A = [1 2 3; 3 5 6;1 2 1] >>B = max(A) >>C=max(B) 3 6 2 a) 6 3 5 6 6 5 6

  28. What does B and C store? >>A = [1 2 3; 3 5 6;1 2 1] >>[B,C] = max(A) B = 3 6 2 C = 3 3 2 B= 6 C = 4 B = 3 5 6 C = 2 2 2 B = 6 5 6 C = 1 1 1

  29. What does D and E store? >>A = [1 2 3; 3 5 6;1 2 1] >>[B,C] = max(A) >>[D,E] = max(B) D = 6 E = 3 D= 6 E = 4 D = 3 5 6 E = 3 D = 6 E = 2

  30. What is B? >>A = [4 4 4 1 3 1 1] >>B = median(A) B = 3 B = 4 B = 1 B = 7

  31. Will this piece of code work? If not, why? >>A = [1 2 3; 3 5 6] >>B = [2 4 6; 6 10 18] >>C = B*A This code will not work because ‘*’ is used for matrix multiplication and in matrix multiplication you need the same number of columns in B as rows in A.

  32. Will this piece of code work? If not, why? >>A = [1 2 3; 3 5 6] >>B = [2 4 6; 6 10 18] >>C = B.*A This code will work because ‘.*’ means multiply each element of B with the same element of A. In order to use ‘.*’, A and B need to be the same dimensions.

  33. What does C store? >>A = [1 2 3; 3 5 6;1 2 1] >>B = [2 4 ; 6 1] >>C = B.*A(2:3,1:2) 2 8 18 5 a) 6 20 6 2 10 24 12 1 4 12 30 6

  34. PLOTTING IN MATLAB

  35. Define x and y and call the plot function

  36. Engineers always add … • Title title(‘y = cos(x)’) • X axis label, complete with units xlabel(‘x-axis’) • Y axis label, complete with units ylabel(‘y-axix’) • Often it is useful to add a grid grid on Single quotes are used.

  37. Line, Color and Mark Style • You can change the appearance of your plots by selecting user defined • line styles • mark styles • color • Try usinghelp plotfor a list of available styles

  38. Specify your choices in a string • For example plot(x, y, ‘:ok') • strings are identified with single quotes • the : means use a dotted line • the o means use a circle to mark each point • the letter k indicates that the graph should be drawn in black • (b indicates blue)

  39. Available choices

  40. specify the drawing parameters for each line after the ordered pairs that define the line

  41. Subplots • The subplot command allows you to subdivide the graphing window into a grid of m rows and n columns • subplot(m,n,p) rows columns location

  42. subplot(2,2,1) 2 columns 2 1 2 rows 3 4

  43. 2 rows and 1 column

  44. Other Types of 2-D Plots • Polar Plots • Log/semi-log (semilogx,semilogy,etc.) • Bar Graphs • Pie Charts • Histograms • X-Y graphs with 2 y axes • Function Plots

  45. CREATING FUNCTIONS IN MATLAB

  46. A simple function (poly2) Save the file as poly2.m (same as the name of the function)

  47. The function is available from the command window or from other M-file programs

  48. Comments • You should comment functions, just as you would any computer code • The comment lines immediately after the first line are returned when you query the help function

  49. Using find and logical operators in Matlab

  50. Logical Operators & and ~ not | or

More Related