MATLAB Skill Builder

From EG1004 Lab Manual
Jump to: navigation, search

Objective

In this exercise, you will be introduced to additional software used by technical professionals. You will learn a basic analytical software package, Matlab

Introduction to Matlab

Starting Matlab:

Open Matlab.

Matlab Desktop:

Figure 52: Matlab Desktop

Command Window:  The command window is the window you write commands in to perform desired Matlab functions.

Current Directory: The current directory window shows which directory Matlab is currently using.  It can be replaced by the workspace window.

Workspace: The workspace widow displays the names and the values of the variable you are currently using in the Matlab workspace.

Command History: The command history window displays the commands that you have recently used in the Matlab command window.  This also displays commands from previous Matlab sessions.

Basic Arithmetic:

  1. Open the workspace window, it should be empty.
  2. In the command window type 3 + 5 and press enter. This code adds the numbers 3 and 5.
  3. Type a = 7 + 6;  in the command window, remember to include the semicolon after the expression. The semicolon supresses the output to the screen. You should use this for all your commands after basic trigonometry.
  4. Now type the letter "a" in the command window and press enter. By simply typing the letter "a" you can display the value of "a."
  5. Enter b = 16 – 4 this command subtracts 4 from 16
  6. Enter c = 7 * 6 this command multiplies 7 and 6
  7. Enter d = 25 / 5 this command divides 25 by 5
  8. Enter e = 8 ^ 2 this command raises 8 by the power of 2 or 82
  9. Enter f = sqrt(36) this command returns the square root of 36
  10. Note: Matlab can perform multiple operations on series of numbers, or matrices.  These tools are very useful in advanced mathematics such as Linear Algebra.

     

  11. Enter A = [ 4 8 15 16 23 42 ] this command creates a vector consisting of the numbers 4, 8, 15, 16, 23 and 42
  12. Enter B = [ 13 7 82 5 19 8 ]
  13. Enger C = A + B this command adds the vectors A and B
  14. Enter D = B – A this command subtracts the vector A from B
  15. Enter E = A .* B this command multiplies A and B element-by-element; if the symbol "*" is used the multiplication will not be element-by-element, it will be matrix multiplication
  16. Enter F = B ./ A this command divides B by A element-by-element
  17. Enter G = B .^ 2 this command raises each element of the vector B by the power of 2
  18. Enter H = sqrt(A) this command returns the square root of each element of the vector A
  19. Note: The ":" operator counts between two different numbers. By default the increment value is "1" though this can be changed.

     

  20. Enter I = 0:10 this returns a vector consiting of the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10
  21. Enter J = 0 : 0.5 : 5 the increments between 0 and 5 are 0.5
  22. Enter K = 25 : 5 : 53
  23. Enter L = 100 : -3 : 80
  24. Enter M = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] this creates a matrix
  25. sum(M) this function adds all of rows together by column; it is a good example of how Matlab can perform many functions at the same time

Basic Trigonometry:

  1. In the command window type the word "clear"; this clears the workspace of the variables that you have been using
  2. Note: In Matlab π is represented by the word "pi."

     

  3. Enter x = π/2 and y = sin(x)
  4. Enter y = cos(π)
  5. Enter x = π/4 and y = sin(x)
  6. Enter y = cos(x)

2-D Plotting:

  1. In the command window type the word "clear"
  2. Enter x = 0 : π/8 : 2*π and y = sin(x)
  3. Enter plot(x, y), creates a plot of x against y. Save this plot as "EG sin"
  4. Now plot x against y = cos(x), save this plot as "EG cos"
  5. Enter theta = 0 : π/16 : 2*π;
  6. Enter x = cos(theta); and y = sin(theta);
  7. Again, plot x against y and save this as "EG circle"
  8. Note: exp(x) represents ex

  9. Enter x = 0 : 5; and y = exp(x);
  10. Again, plot x against y and save this plot as "EG e"

3-D Plotting:

  1. In the command window type the word "clear"
  2. sphere(20) this creates a three-dimensional figure of a sphere. Save this as "EG sphere"
  3. Figure 53: Top of figure window
  4. Click onto the rotation tool on the top of the window
  5. Click on the figure and drag the mouse around the screen
  6. Enter z = 0 : 0.1 : 2 and r = sin(3*π*z) + 2; this makes z equal to the space in between zero and two at increments of 0.1
  7. Enter cylinder(r) save this as "EG cylinder"
  8. In the command window type the word "clear"
  9. Enter u = -5 : 0.2 : 5; [x, y] = meshgrid(u,u); this changes the format of the data so it can be used in three-dimensional plots
  10. To demonstrate your new found knowledge of Matlab use what you have learned to write
    in Matlab code. Remember that x and y are vectors and they need to be evaluated element-by-element.
  11. Enter surf(x,y,z) this creates a three-dimensional plot of the function z(x,y). Save this as "EG surface"
  12. Copy and paste your Matlab command history into a Word Document. Copy your Matlab graphs and paste them into the same Word Document underneath the code history you used to make them.
  13. Exit Matlab by clicking on the red "x" on the upper right hand corner of the window
  14. Submit this Word document to eg.poly.edu. Remember to include a cover page when you submit.

You have now completed the lab. Log off the PC and make sure your work area is clean, with any scrap paper you used, etc. properly of.

Return to Table of Contents