ENGG 1801 Engineering Computing - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

ENGG 1801 Engineering Computing

Description:

Scalar and array operations, programming advice. Plotting and formatting 2D graphs. ... X = X' gives transpose (rows to columns, columns to rows) Zeros and ones: ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 23
Provided by: amic52
Category:

less

Transcript and Presenter's Notes

Title: ENGG 1801 Engineering Computing


1
ENGG 1801Engineering Computing
  • MATLAB Lecture 5 Tutorial (Weeks 9-10 )
  • Matrices and Graphing in MATLAB

2
Outline of Lecture
  • Scalars, vectors and matrices, generating them.
  • Scalar and array operations, programming advice.
  • Plotting and formatting 2D graphs.
  • Functions of two variables 3D graphs.
  • Creating 3D graphs, different types.

3
Scalars, Vectors and Matrices
  • Scalar 2.4
  • Vector 1.5 3.1
  • Or 2
  • 3
  • 1
  • Matrix

3 1 2 5 6 8 7 1 7
4
Generating matrices
  • X 10.53 5,4,3,2,1 2210
  • gives 1 1.5 2 2.5 3
  • 5 4 3 2 1
  • 2 4 6 8 10
  • Y X(,34) Z X(23,23)
  • gives 2 2.5 gives 4 3
  • 3 2 4 6
  • 6 8

5
Generating Matrices (cont)
  • Other options
  • X X' gives transpose (rows to columns, columns
    to rows)
  • Zeros and ones
  • A zeros (3) gives a 3x3 matrix of 0
  • B ones (3,2) gives a 3x2 matrix of 1

6
Scalar and Array Operations
  • Operations on scalars
  • Normal mathematics
  • Operations on arrays
  • Element-by-Element Operations
  • Specified by putting a . directly after
    variable name
  • Required for multiplication, division and
    exponentiation
  • NOT required for addition and subtraction
  • NOT required for (scalar nonscalar) or
    (nonscalar / scalar)

7
Miscellaneous Advice for Programming, Including
Matrices
  • Colon may be used to enter matrices
  • Enter starting point, step, end point
  • e.g., H 2210 gives 2 4 6 8 10
  • Type the semi-colon to prevent results of
    operations being shown in the command window
  • 3 Ellipses can be used if you wish to continue
    typing on the next line
  • Edit matrices by double clicking on the matrix in
    the Workspace window
  • Sign to enter comments to your code

8
Class Exercise
  • Calculate the following

4 1 1 8 7 6 3 3 2
3 1 2 5 6 8 7 1 7
?


4 1 8 7 3 3
3 1 2 5 6 8 7 1 7
?


.
4 1 1 8 7 6 3 3 2
3 1 2 5 6 8 7 1 7
?

9
Class Exercise Answers
4 1 1 8 7 6 3 3 2
26 16 13 92 71 57 57 35 27
3 1 2 5 6 8 7 1 7


4 1 8 7 3 3
26 16 92 71 57 35
3 1 2 5 6 8 7 1 7


.
4 1 1 8 7 6 3 3 2
12 1 2 40 42 48 21 3 14
3 1 2 5 6 8 7 1 7

10
Summary Note on size of matrices
  • To do a matrix multiplication AB, the number of
    columns in A must be equal to the number of rows
    in B
  • To do an array multiplication both matrices must
    have the same number of rows AND columns

11
Plotting Graphs
  • plot command
  • Used to graph data
  • Can specify
  • Grids
  • Markers for data points eg
  • plot(x,T,x,T,o)
  • Line style
  • Many plots on one window (subplots)

12
Formatting Graphs
  • Lines
  • Default solid blue line
  • Dashed line --
  • Dotted line
  • Dash-dot -.
  • Markers
  • Use ., , , o, x, d, v, lt, gt
  • Colours
  • b, g, y, m, r, k, c

13
  • Problem plot the expression
  • Solution
  • Define x values
  • x 10.15
  • y ((2x.2).exp(x))./(sqrt(x)-100)
  • Plot results
  • plot(x,y),...
  • title('Computing Sample Question'),...
  • xlabel('x value'),...
  • ylabel('y value'),...
  • grid

14
  • Problem subplots
  • This example illustrates subplots
  • x 10.15
  • y1 2x.2
  • y2 exp(x)
  • y3 sqrt(x)-100
  • y (y1.y2)./y3
  • Plot results
  • subplot(2,2,1), plot(x,y1,'o'),...
  • title('y1 as a function of x'),...
  • xlabel('x value'),...
  • ylabel('y1 value'),grid
  • subplot(2,2,2), plot(x,y2,''),...
  • title('y2 as a function of x'),...
  • xlabel('x value'),...
  • ylabel('y2 value'),grid

15
Functions of Two Variables
  • Wish to plot in 3 dimensions (i.e., 2 variables
    against function value)
  • First generate a two dimensional grid in the x-y
    plane
  • Then evaluate function value at the grid points
    to develop a 3 dimensional surface

16
Functions of Two variables (continued)
  • The 2-D grid consists of 2 matrices
  • X-coordinates at all points in the grid (m) and
  • Y- coordinates at all points in the grid (n)
  • New matrices are size m x n
  • e.g., if x goes from 2 to 2 in increments of 1
    and y goes from 1 to 2 in increments of 1 then

-1 -1 -1 -1 -1 0 0 0 0 0 1 1 1
1 1 2 2 2 2 2
-2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1
2 -2 -1 0 1 2
y_grid
x_grid
17
Creating 3-D grids
  • Use the meshgrid command
  • x_grid, y_grid meshgrid(x,y)
  • Creates 2 new matrices as shown on previous slide
    from vectors x and y
  • Then calculate the function value for each of the
    values in vectors x and y, ie
  • If z 1/(1 x2 y2)
  • Then code is
  • z 1./(1 x_grid.2 y_grid.2)
  • Result is an m x n matrix

18
Types of 3-D plots
  • Mesh(x_grid,y_grid,z) gives an open mesh plot
  • Number of points in original vector determines
    closeness of mesh

19
Types of 3-D plots
  • surf(x_grid,y_grid,z) gives a shaded mesh plot
  • Number of points in original vector determines
    closeness of mesh

20
Types of 3-D plots
  • contour(x_grid,y_grid,z) gives a contour plot

21
Types of 3-D plots
  • meshc(x_grid,y_grid,z) gives a contour plot
    combined with a mesh plot

22
Conclusions
  • MATLAB is focussed on manipulating matrices.
  • Scalars lead to vectors, lead to matrices.
  • MATLAB is good for plotting graphs
  • subplots are useful.
  • 2D 3D graphing available.
Write a Comment
User Comments (0)
About PowerShow.com