Introduction to Matlab - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Introduction to Matlab

Description:

Use predefined functions or write your own functions ... subtraction * multiplication / division ^ power. complex conjugate transpose. ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 27
Provided by: doi73
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Matlab


1
Introduction to Matlab
??????µ?d?? ??µ?t??? doikon_at_telecom.tuc.gr http//
www.telecom.tuc.gr
2
Desktop Tools (Matlab v6)
  • Command Window
  • type commands
  • Workspace
  • view program variables
  • clear to clear
  • double click on a variable to see it in the Array
    Editor
  • Command History
  • view past commands
  • save a whole session using diary
  • Launch Pad
  • access tools, demos and documentation

3
Matlab Files (.m)
  • Use predefined functions or write your own
    functions
  • Reside on the current directory or the search
    path
  • add with File/Set Path
  • Use the Editor/Debugger to edit, run

4
Matrices
  • a vector x 1 2 5 1
  • x
  • 1 2 5 1
  • a matrix x 1 2 3 5 1 4 3 2 -1
  • x
  • 1 2 3
  • 5 1 4
  • 3 2 -1
  • transpose y x. y
  • 1
  • 2
  • 5
  • 1

5
Matrices
  • x(i,j) subscription
  • whole row
  • whole column

yx(2,3) y 4 yx(3,) y 3 2
-1 yx(,2) y 2 1 2
6
Operators (arithmetic)
  • addition
  • - subtraction
  • multiplication
  • / division
  • power
  • complex conjugate transpose

. element-by-element mult ./ element-by-element
div . element-by-element power . transpose
7
Operators (relational, logical)
  • equal
  • not equal
  • lt less than
  • lt less than or equal
  • gt greater than
  • gt greater than or equal
  • AND
  • OR
  • NOT
  • pi 3.14159265
  • j imaginary unit,
  • i same as j

8
Generating Vectors from functions
  • x zeros(1,3)
  • x
  • 0 0 0
  • x ones(1,3)
  • x
  • 1 1 1
  • x rand(1,3)
  • x
  • 0.9501 0.2311 0.6068
  • zeros(M,N) MxN matrix of zeros
  • ones(M,N) MxN matrix of ones
  • rand(M,N) MxN matrix of uniformly distributed
    random numbers on (0,1)

9
Operators
  • concatenation
  • ( ) subscription
  • x zeros(1,3) ones(1,2)
  • x
  • 0 0 0 1 1
  • x 1 3 5 7 9
  • x
  • 1 3 5 7 9
  • y x(2)
  • y
  • 3
  • y x(24)
  • y
  • 3 5 7

10
Matlab Graphics
  • x 0pi/1002pi
  • y sin(x)
  • plot(x,y)
  • xlabel('x 02\pi')
  • ylabel('Sine of x')
  • title('Plot of the Sine Function')

11
Multiple Graphs
  • t 0pi/1002pi
  • y1sin(t)
  • y2sin(tpi/2)
  • plot(t,y1,t,y2)
  • grid on

12
Multiple Plots
  • t 0pi/1002pi
  • y1sin(t)
  • y2sin(tpi/2)
  • subplot(2,2,1)
  • plot(t,y1)
  • subplot(2,2,2)
  • plot(t,y2)

13
Graph Functions (summary)
  • plot linear plot
  • stem discrete plot
  • grid add grid lines
  • xlabel add X-axis label
  • ylabel add Y-axis label
  • title add graph title
  • subplot divide figure window
  • figure create new figure window
  • pause wait for user response

14
Math Functions
  • Elementary functions (sin, cos, sqrt, abs, exp,
    log10, round)
  • type help elfun
  • Advanced functions (bessel, beta, gamma, erf)
  • type help specfun
  • type help elmat

15
Functions
  • function fmyfunction(x,y)
  • fxy
  • save it in myfunction.m
  • call it with ymyfunction(x,y)

16
Flow Control
  • if A gt B
  • 'greater'
  • elseif A lt B
  • 'less'
  • else
  • 'equal'
  • end
  • for x 110
  • r(x) x
  • end
  • if statement
  • switch statement
  • for loops
  • while loops
  • continue statement
  • break statement

17
Miscellaneous
  • Loading data from a file
  • load myfile.dat
  • Suppressing Output
  • x 1 2 5 1

18
Getting Help
  • Using the Help Browser (.html, .pdf)
  • View getstart.pdf, graphg.pdf, using_ml.pdf
  • Type
  • help
  • help function, e.g. help plot
  • Running demos
  • type demos
  • type help demos

19
Random Numbers
  • xrand(100,1)
  • stem(x)
  • hist(x,100)

20
Coin Tosses
  • Simulate the outcomes of 100 fair coin tosses
  • xrand(100,1)
  • psum(xlt0.5)/100
  • p
  • 0.5400
  • Simulate the outcomes of 1000 fair coin tosses
  • xrand(1000,1)
  • psum(xlt0.5)/1000
  • p
  • 0.5110

21
Coin Tosses
  • Simulate the outcomes of 1000 biased coin tosses
    with pHead0.4
  • xrand(1000,1)
  • psum(xlt0.4)/1000
  • p
  • 0.4160

22
Sum of Two Dies
  • Simulate 10000 observations of the sum of two
    fair dies

23
Sum of Two Dies
  • Simulate 10000 observations of the sum of two
    fair dies
  • x1floor(6rand(10000,1)1)
  • x2floor(6rand(10000,1)1)
  • yx1x2
  • sum(y2)/10000 ans 0.0275 p20.0278
  • sum(y3)/10000 ans 0.0554 p30.0556
  • sum(y4)/10000 ans 0.0841 p40.0833
  • sum(y5)/10000 ans 0.1082 p50.1111
  • sum(y6)/10000 ans 0.1397 p60.1389
  • sum(y7)/10000 ans 0.1705 p70.1667
  • sum(y8)/10000 ans 0.1407 p80.1389
  • sum(y9)/10000 ans 0.1095 p90.1111
  • sum(y10)/10000 ans 0.0794 p100.0833
  • sum(y11)/10000 ans 0.0585 p110.0556
  • sum(y12)/10000 ans 0.0265 p120.0278

24
Sum of Two Dies
  • for i212
  • z(i)sum(yi)/10000
  • end
  • bar(z)

25
Bernoulli Trials-Binomial Distribution
  • k020
  • ybinopdf(k,20,0.5)
  • stem(k,y)

Bernoulli 1720 k020 ybinopdf(k,20,0.2) stem(
k,y)
26
Combinatorics
  • Permutations n objects
  • Permutations choose k objects from n
  • (hint fill 2 spaces on a bookshelf with books
    chosen from 5 available books)
  • Combinations choose k objects from n without
  • regard to the order
  • (hint make a committee of 2 people chosen from
    a group of 5 people)
Write a Comment
User Comments (0)
About PowerShow.com