Basics of MATLAB (2) - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Basics of MATLAB (2)

Description:

Basics of MATLAB (2) * * Row vector containing integers from 1 to 10 * * Xvalues and yvalues are vectors containing the x- and y- coordinates of points on the graph. – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 34
Provided by: suhy
Category:

less

Transcript and Presenter's Notes

Title: Basics of MATLAB (2)


1
Basics of MATLAB (2)
2
Topics
  • Do some more matrix operations
  • Differentiation and integration
  • Plotting graphs
  • Some review questions at the end of the class

3
Matrix Operations - Colon Operator
  • Try typing in 110
  • How about 50-420?
  • Subscript expressions involving colons
  • A(1k, j) first k elements of the jth column of
    A
  • A(k,1j) first j elements of the kth row of A

4
Matrix Operations - Colon Operator
  • A(2,24) and A(14,4)
  • A(14,4) A(,4) A(,end)
  • Colon by itself refers to all the elements in a
    row or column of a matrix
  • The keyword end refers to the last row or column
  • A(,end) Elements in the last column of A
  • Exercise 7

5
Exercise 7
  • gtgt A16 3 2 135 10 11 89 6 7 124 5 4 1
  • A
  • 16 3 2 13
  • 5 10 11 8
  • 9 6 7 12
  • 4 5 4 1
  • gtgt Tdiag(A) A(,1) A(2,)'
  • T
  • 16 16 5
  • 10 5 10
  • 7 9 11
  • 1 4 8

6
Matrix Operations Deleting Rows and Columns
  • Delete rows and columns from a matrix using just
    a pair of square brackets
  • X A Creating new X matrix
  • X(,2) Deleting the 2nd column of X
  • will result in
  • X 16 2 13
  • 5 11 8
  • 9 7 12
  • 4 14 1 Exercise 8

Before, X
7
Exercise 8
  • gtgt A(,3)
  • A
  • 16 3 13
  • 5 10 8
  • 9 6 12
  • 4 5 1
  • gtgt A(2,)
  • A
  • 16 3 13
  • 9 6 12
  • 4 5 1

  • A(,3) , A(2,)

8
Relational Operations
  • lt less than
  • lt less than or equal
  • gt greater than
  • gt greater than or equal
  • equal
  • not equal
  • Ex)
  • x1 5 3 7, y0 2 8 7
  • kxlty results in k?
  • gives a vector with 1 where the relation is true
    and 0 otherwise

9
Differentiation
  • Using Symbolic Math Toolbox software to take
    derivatives, (ex f x2 4x 3)
  • any variable to be used should be introduced with
    a syms command (creating symbolic expressions)
  • syms x
  • second, create a new var.(f) that is a function
    of x
  • f x2 4x 3
  • third, take derivative with the command diff
  • diff(f) differentiates f with respect to x

10
Differentiation
  • To take the second derivative of f, enter
  • diff(f,2)
  • Derivatives with several variables, exsin(st)
  • Create a symbolic expression
  • syms s t
  • Creat a function
  • gsin(st)
  • Take partial derivative wrt t
  • diff(g,t)

Exercise 1.
11
Exercise 1.
  • syms x
  • fexp(x23)/(x3-x3)
  • diff(f,x)
  • syms x y
  • g(x2xy-1)/(y3x3)
  • diff(g,y)

12
Exercise 1.
  • syms x
  • fexp(x23)/(x3-x3)
  • diff(f,x)
  • (2xexp(x2 3))/(x3 - x 3) - (exp(x2
    3)(3x2 - 1))/(x3 - x 3)2
  • syms x y
  • g(x2xy-1)/(y3x3)
  • diff(g,y)
  • x/(y3 x 3) - (3y2(x2 yx - 1))/(y3
    x 3)2

13
Integration
  • INT(S,v) is the indefinite integral of S with
    respect to v.
  • INT(S,v,a,b) is the definite integral of S with
    respect to v from a to b.

14
Integration
  • Exercise 2.

15
Exercise 2
  • int((x2x)/(x3),x)
  • ans
  • int((x2-x1)/(x3),x,0,10)
  • ans

16
Exercise 2
  • int((x2x)/(x3),x)
  • ans
  • 6log(x 3) - 2x x2/2
  • int((x2-x1)/(x3),x,0,10)
  • ans
  • 13log(13/3) 10

17
Plotting - example
  • Plotting x vs. y,
  • where xt, yet (tintegers between 0 and 40)
  • t040
  • xtyexp(t)
  • plot(x,y)

18
Plotting
  • Labels, title, legend, and other text objects
    ex) x 0.120 yexp(0.1x) plot (x,y)
  • xlabel(time)
  • ylabel(response)
  • title(time response)
  • Saving as a file
  • print G\Suhyun\Classes\MATLAB\Programming\a.eps
    -deps
  • In jpeg format a.jpg djpeg
  • In bmp format a.bmp -dbitmap

Exercise 1.
19
Exercise 1.
  • xlinspace(0,2pi,100)
  • plot(x,sin(x))
  • xlable(x), ylabel(sin(x))
  • title(Plot created by Me)

20
Plotting
  • Basic 2-D plots (help graph2d)
  • plot (xvalues, yvalues, style-option)
  • Style option spcifies color, line style, and
    point-marker style

Color style-option Color style-option Line style-option Line style-option Marker style-option Marker style-option
y yellow - solid plus sign
m magenta -- dashed o circle
c cyan dotted asterisk
r red -. dash-dot x x-mark
g green . point
b blue up triangle
w white s square
k black d diamond
21
Plotting
  • default blue solid line
  • plot(x,y,color line or marker style)
  • Examples
  • plot(x,y,r) plots y vs. x with a red solid
    line
  • plot(x,y,b--) plots y vs. x with a blue dashed
    line
  • plot(x,y,) plots y vs. x as unconnected
    points marked by
  • Exercise 2.

22
Plotting - fplot
  • fplot (function, xmin xmax)
  • It takes the function of a single variable and
    the limits of the axes as the input and produces
    a plot of the function
  • ex) plot exercise 1 using fplot
  • fplot('sin(x)',0 2pi)

23
Overlay Plots
  • With two sets of data
  • (x1,y1) and (x2,y2) xi and yi are column vectors
  • plot(x1,y1,,x2,y2,)
  • plots (x1,y1) with a sign, (x2,y2) with a
    dotted line
  • gtext(label)
  • writes the label at a location clicked with the
    mouse in the graphic window after entering the
    command

Exercises 3,4
24
Data
  • 2000 Census
  • 4915 obs.
  • Census Block Group
  • Variables
  • Housing Value
  • Income
  • Unemployment
  • Education

25
Exercises 3
  • gtgthvdata00(,1)
  • gtgt incomedata00(,2)
  • gtgt unempdata00(,3)
  • gtgt plot(income, hv,'.')
  • gtgt xlabel('income')
  • gtgt ylabel('housing value')
  • gtgt title('housing value on income')

26
Exercises 3
27
Exercise 4
  • gtgtplot(hv,edu,'g.',hv,unemp,'')
  • gtgt gtext('Education')
  • gtgt gtext('Unemployment')
  • gtgt xlabel('Housing value')

28
Exercise 4
Plotting tools (plottools)
29
Plotting contour and 3-D
  • ezcontour contour plots
  • ex) zinline('x2y') ezcontour(z,0,10,0,10)
  • ezcontourf filled contours
  • ezsurf surface plots in 3-D
  • ex) zinline('x2y') ezsurf(z,0,10,0,10)
  • (rotation function)
  • Exercise 5.

30
Exercise 5
  • zinline('xy1/2') ezcontour(z,0,20,0,15)

31
Review Questions
  • exp(pi/2i)
  • exp(pi/2i)

32
Review Questions
  • gtgt A7 -3 5 1-1 2 3 94 5 7 42 6 -2 8
  • A
  • 7 -3 5 1
  • -1 2 3 9
  • 4 5 7 4
  • 2 6 -2 8
  • gtgt A1sort(A,'descend')
  • A1
  • 7 6 7 9
  • 4 5 5 8
  • 2 2 3 4
  • -1 -3 -2 1

33
Review Questions
  • Fxyzinline('(x3y-exp(yz))/(3xz2)','x','y','
    z')
  • Fxyz
  • Inline function
  • Fxyz(x,y,z) (x3y-exp(yz))/(3xz2)
  • gtgt Fxyz(2,3,-2)
  • ans
  • 0.9999
Write a Comment
User Comments (0)
About PowerShow.com