MATLAB Introduction - PowerPoint PPT Presentation

1 / 71
About This Presentation
Title:

MATLAB Introduction

Description:

... (i,j) ... (sum, sin, ) to advanced (fft, inv, Bessel functions, ) API: can use MATLAB with C, Fortran, and Java, ... solid b blue * star w white : ... – PowerPoint PPT presentation

Number of Views:339
Avg rating:3.0/5.0
Slides: 72
Provided by: sathiammu
Category:

less

Transcript and Presenter's Notes

Title: MATLAB Introduction


1
MATLAB Introduction
  • Main Features
  • Simple programming rules
  • Extended accuracy
  • Continuity among integer, real and complex values
  • Comprehensive mathematical library
  • Extensive graphics tools
  • Linkages with other languages
  • Transportability across environment
  • MATLAB scripts will work on PC, UNIX, Mac

2
  • Typical uses include
  • ?? Math and computation
  • ?? Algorithm development
  • ?? Modelling, simulation and prototyping
  • ?? Data analysis, exploration and visualization
  • ?? Scientific and engineering graphics
  • ?? Application development, including Graphical
    User Interface (GUI) building

3
The Advantages of MATLAB
  • Ease of Use.
  • Platform Independence
  • Predefined Function.
  • Device-Independent Plotting.
  • Graphical User Interface.
  • MATLAB Compiler.
  • a fraction of the time it would take to write a
    program in a scalar non-interactive language such
    as C, C or Fortran.
  • MATLAB is the tool of choice for
    high-productivity research, development and
    analysis

4
Disadvantage
  • it is an interpreted language and therefore can
    execute more slowly than compiled languages.
  • a full copy of MATLAB is five to ten times more
    expensive than a conventional C or Fortran
    compiler.

5
Starting MATLAB
  • On UNIX type matlab at command prompt
  • Click on the MATLAB icon if you are on a PC
  • Mac can probably do both
  • Issues on startup
  • MATLAB needs a connection to the license server
  • Check internet connection
  • Too many users can use all available licenses

6
Starting MATLAB
  • Once MATLAB is running the GUI (Graphical User
    Interface) will appear
  • Default Window apperance

7
Starting MATLAB
  • Command Window
  • Main window in MATLAB
  • Commands entered here

8
(No Transcript)
9
Starting MATLAB
  • MATLAB displays gtgt prompt when ready for a
    command
  • Will have no gtgt prompt when processing commands
  • Newer versions also say Ready or Busy in
    lower left corner of GUI
  • Can use arrow keys to work through command
    history and modify commands
  • Essentially the same as UNIX command prompt

10
  • MATrix LABoratory
  • Powerful, extensible, highly integrated
    computation, programming, visualization, and
    simulation package
  • Widely used in engineering, mathematics, and
    science
  • Why?
  • Interactive code development proceeds
    incrementally excellent development and rapid
    prototyping environment

11
  • Basic data element is the auto-indexed array
  • This allows quick solutions to problems that can
    be formulated in vector or matrix form
  • Powerful GUI tools
  • Large collection of toolboxes collections of
    topic-related MATLAB functions that extend the
    core functionality significantly

12
MATLAB Toolboxes
  • Signal Image Processing
  • Signal Processing
  • Image Processing
  • Communications 
  • Frequency Domain System
    Identification
  • Higher-Order Spectral Analysis
  • System Identification
  • Wavelet
  • Filter Design
  • Control Design 
  • Control System
  • Fuzzy Logic
  • Robust Control
  • µ-Analysis and Synthesis
  • Model Predictive Control

Math and Analysis Optimization Requirements
Management Interface Statistics Neural
Network Symbolic/Extended Math Partial
Differential Equations PLS Toolbox Mapping Spline
Data Acquisition and Import Data
Acquisition Instrument Control Excel
Link Portable Graph Object
Intro MATLAB
13
Toolboxes, Software, Links
Intro MATLAB
14
MATLAB System
  • Language arrays and matrices, control flow, I/O,
    data structures, user-defined functions and
    scripts
  • Working Environment editing, variable
    management, importing and exporting data,
    debugging, profiling
  • Graphics system 2D and 3D data visualization,
    animation and custom GUI development
  • Mathematical Functions basic (sum, sin,) to
    advanced (fft, inv, Bessel functions, )
  • API can use MATLAB with C, Fortran, and Java, in
    either direction

15
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

16
Data Types
  • logical
  • char
  • CELL
  • structure
  • Java Classes
  • Function handle
  • ARRAY
  • NUMERIC ------------------------int,single,double

17
Variable Basics
no declarations needed
gtgt 16 24 ans 40 gtgt product 16
23.24 product 371.84 gtgt product 16
555.24 gtgt product product 8883.8
mixed data types
semi-colon suppresses output of the calculations
result
Intro MATLAB
18
Variable Basics
gtgt clear gtgt product 2 33 gtgt comp_sum (2
3i) (2 - 3i) gtgt show_i i2 gtgt save
three_things gtgt clear gtgt load three_things gtgt
who Your variables are comp_sum product
show_i gtgt product product 54 gtgt
show_i show_i -1
clear removes all variables clear x y removes
only x and y
complex numbers (i or j) require no special
handling
save/load are used to retain/restore workspace
variables
use home to clear screen and put cursor at the
top of the screen
Intro MATLAB
19
MATLAB Data
  • The basic data type used in MATLAB is the double
    precision array
  • No declarations needed MATLAB automatically
    allocates required memory
  • Resize arrays dynamically
  • To reuse a variable name, simply use it in the
    left hand side
  • of an assignment statement
  • MATLAB displays results in scientific notation
  • Use File/Preferences and/or format function to
    change default
  • short (5 digits), long (16 digits)
  • format short g format compact (my preference)

Intro MATLAB
20
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

21
Matrices
yx(2,3) y 4 yx(3,) y 3 2
-1 yx(,2) y 2 1 2
  • x(i,j) subscription
  • whole row
  • whole column

22
Operators (arithmetic)
  • addition
  • Subtraction
  • Multiplication
  • / division
  • power
  • complex conjugate transpose

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

24
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)

25
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

26
To create variables
  • gtgta1
  • a 1
  • gtgt b4
  • b 4
  • gtgt cab
  • c 5
  • gtgt dcos(a)
  • d 0.5403

27
  • gtgtt1 2 3 4 5
  • t 1 2 3 4 5
  • gtgtt1 2 3 4 5
  • gtgt t15 equally spaced arrays
  • t 1 2 3 4 5
  • gtgt t10.54
  • t 1.0000 1.5000 2.0000 2.5000
    3.0000 3.5000 4.0000

28
  • gtgtWhos To know the variables typed so far
  • Name Size Bytes Class
    Attributes
  • a 1x1 8 double
  • b 1x1 8 double
  • c 1x1 8 double
  • d 1x1 8 double
  • t 1x6 48 double

29
One and two dimentional arrays
  • gtgtdatarand(2,2)
  • data 0.8147 0.1270
  • 0.9058 0.9134
  • gtgtsize(data)
  • ans 2 2
  • gtgtx34i
  • x 3.0000 4.0000i

30
  • gtgta1 2 34 5 67 8 9
  • a 1 2 3
  • 4 5 6
  • 7 8 9
  • gtgta(2,3)
  • ans 6
  • gtgtba'
  • b 1 4 7
  • 2 5 8
  • 3 6 9
  • gtgtcab
  • c 14 32 50
  • 32 77 122
  • 50 122 194

31
  • gtgtca.b
  • c 1 8 21
  • 8 25 48
  • 21 48 81

32
  • gtgt datarand(5,5)
  • data 0.5313 0.4235 0.4401 0.9436
    0.2891
  • 0.3251 0.0908 0.5271 0.6377
    0.6718
  • 0.1056 0.2665 0.4574 0.9577
    0.6951
  • 0.6110 0.1537 0.8754 0.2407
    0.0680
  • 0.7788 0.2810 0.5181 0.6761
    0.2548
  • gtgt data(13,2end)
  • ans 0.4235 0.4401 0.9436 0.2891
  • 0.0908 0.5271 0.6377 0.6718
  • 0.2665 0.4574 0.9577 0.6951

33
  • gtgt a1.5
  • a 1.5000
  • gtgt whos
  • Name Size Bytes Class
    Attributes
  • a 1x1 8 double
  • gtgtformat long
  • gtgt1/7
  • ans 0.142857142857143
  • gtgtformat short
  • gtgt 1/7

34
  • gtgtx3.2
  • x 3.2000
  • gtgtexp(x)
  • ans 24.5325
  • Boolean Expression
  • gtgt d(1)true
  • d 1
  • gtgt d(2)false
  • d 1 0
  • gtgta1.5

35
Matrix
  • gtgta1 2 3 4
  • a 1 2 3 4
  • gtgta1 23 4
  • a 1 2
  • 3 4
  • gtgta110
  • a 1 2 3 4 5 6 7 8
    9 10

36
  • gtgt a110
  • a 1 2 3 4 5 6 7 8
    9 10
  • gtgt a1210
  • a 1 3 5 7 9
  • gtgt10-21
  • ans 10 8 6 4 2

37
  • gtgtI eye(3), x 8 -4 1, Ix
  • I
  • 1 0 0
  • 0 1 0
  • 0 0 1
  • x
  • 8
  • -4
  • 1
  • ans
  • 8
  • 4
  • 1

38
  • gtgt arand(4,4)
  • a
  • 0.8147 0.6324 0.9575 0.9572
  • 0.9058 0.0975 0.9649 0.4854
  • 0.1270 0.2785 0.1576 0.8003
  • 0.9134 0.5469 0.9706 0.1419
  • gtgta(1,2)
  • ans 0.6324

39
  • gtgt a(1,1,2)
  • ans 0.8147 0.6324
  • gtgt a(1,)
  • ans 0.8147 0.6324 0.9575 0.9572
  • gtgt a(1,2end)
  • ans 0.6324 0.9575 0.9572

a 0.8147 0.6324 0.9575 0.9572 0.9058
0.0975 0.9649 0.4854 0.1270 0.2785
0.1576 0.8003 0.9134 0.5469 0.9706
0.1419
40
  • gtgt a(1,2end-1)10,10
  • a
  • 0.8147 10.0000 10.0000 0.9572
  • 0.9058 0.0975 0.9649 0.4854
  • 0.1270 0.2785 0.1576 0.8003
  • 0.9134 0.5469 0.9706 0.1419
  • gtgt a(12,)
  • a
  • 0.1270 0.2785 0.1576 0.8003
  • 0.9134 0.5469 0.9706 0.1419

a 0.8147 0.6324 0.9575 0.9572 0.9058
0.0975 0.9649 0.4854 0.1270 0.2785
0.1576 0.8003 0.9134 0.5469 0.9706
0.1419
41
  • gtgta(5)
  • ans 0.1576
  • gtgt a()
  • ans
  • 0.1270
  • 0.9134
  • 0.2785
  • 0.5469
  • 0.1576
  • 0.9706
  • 0.8003
  • 0.1419
  • a
  • 0.1270 0.2785 0.1576 0.8003
  • 0.9134 0.5469 0.9706 0.1419

42
  • gtgt alt0.5
  • ans
  • 1 1 1 0
  • 0 0 0 1
  • gtgt a(alt0.5) -1
  • a
  • -1.0000 -1.0000 -1.0000 0.8003
  • 0.9134 0.5469 0.9706 -1.0000
  • gtgt indfind(alt0.5)
  • ind
  • 1
  • 3
  • a
  • 0.1270 0.2785 0.1576 0.8003
  • 0.9134 0.5469 0.9706 0.1419

43
  • r,cfind(alt0.5)
  • r
  • 1
  • 1
  • 1
  • 2
  • c
  • 1
  • 2
  • 3
  • 4
  • a
  • 0.1270 0.2785 0.1576 0.8003
  • 0.9134 0.5469 0.9706 0.1419

44
  • gtgt numel(a)
  • ans 8
  • gtgt arand(2,2)
  • a
  • 0.4218 0.7922
  • 0.9157 0.9595
  • gtgt ba,a
  • b
  • 0.4218 0.7922 0.4218 0.7922
  • 0.9157 0.9595 0.9157 0.9595
  • 0.1270 0.2785 0.1576 0.8003
  • 0.9134 0.5469 0.9706 0.1419

45
  • gtgt baa
  • b
  • 0.4218 0.7922
  • 0.9157 0.9595
  • 0.4218 0.7922
  • 0.9157 0.9595
  • gtgt a1.5
  • a 1.5000
  • gtgt if alt5
  • disp('it is within the range')
  • end
  • it is within the range

b 0.4218 0.7922 0.4218
0.7922 0.9157 0.9595 0.9157 0.9595
46
Character constant
  • gtgt name'john'
  • name john
  • gtgtname 'smith'
  • ans Johnsmith
  • gtgtname(12)
  • ans jo
  • gtgt k1
    index with which to construct a sring
  • k 1
  • gtgt str'ring' num2str(k)
  • str ring1

47
Structure
  • gtgt car.year2010
  • ans 2010
  • gtgt car.colorred
  • ans red
  • gtgt car.namemaruthi
  • ans maruthi

48
  • gtgtcarstruct('year','2010','color','red','name','m
    aruthi')
  • car
  • year '2010
  • color 'red
  • name 'maruthi
  • gtgtcarscarcar
  • cars
  • 2x1 struct array with fields
  • year
  • color
  • name

49
  • gtgt cars(2).name'feat'
  • cars
  • 2x1 struct array with fields
  • year
  • color
  • name
  • gtgtcars.name
  • ans maruthi
  • ans feat

50
  • gtgt cars(1).name,cars(2).name
  • ans maruthi
  • ans Feat
  • Cell Array
  • mycell1 2 3'test' 12 false
  • mycell
  • 1 2 3
  • 'test' 2x1 double 0

51
  • gtgt mycell1, 2,'orange',true
  • mycell
  • 1 2 'orange' 1
  • gtgtymycell(1,1)
  • y
  • 1
  • gtgt ymycell(1,2)
  • y
  • 2

52
  • gtgt ymycell(1,3)
  • y 'orange
  • gtgtymycell(1,4)
  • y 1
  • gtgtclass(y)
  • ans Cell

53
  • gtgtymycell1,3
  • y orange
  • gtgtclass(y)
  • ans char

54
  • gtgt mycell34
  • ans Orange
  • ans 1
  • gtgt newcellmycell34
  • newcell 'orange' 1

55
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

56
  • gtgt x pi(-13), round(x) Round to nearest
    integer
  • x
  • -3.1416 0 3.1416 6.2832
    9.4248
  • ans
  • -3 0 3 6
    9
  • gtgt fix(x) Round toward zero
  • ans
  • -3 0 3 6 9
  • gtgt floor(x) rounds against negative
    infinity
  • ans
  • -4 0 3 6 9

57
  • gtgt ceil(x) Round towards positive infinity
  • ans -3 0 4 7 10
  • gtgt sign(x), 1 if the corresponding
    element of X is greater than zero
  • 0 if
    the corresponding element of X equals zero
  • -1 if
    the corresponding element of X is less than zero
  • ans -1 0 1 1 1

58
  • Plotting the figure
  • gtgtt00.050.5
  • gtgtysin(2pit)
  • y 0 0.5878 0.9511 0.9511 0.5878
    0.0000
  • gtgtplot(t,y)

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

60
Line Styles Colours
The default is to plot solid lines. A solid white
line is produced by gtgt plot(x,y,'w-') The third
argument is a string whose rst character species
the colour(optional) and the second the
line style. The options for colours and styles
are Colours Line Styles y yellow
. point m magenta o circle c cyan
x x-mark r red plus g green
- solid b blue star w
white dotted k black -.
dashdot -- dashed
61
  • x15
  • x
  • 1 2 3 4 5
  • gtgt ylog(x)
  • y
  • 0 0.6931 1.0986 1.3863
    1.6094
  • gtgt plot(x,y)
  • plot(x,y,'')

62
  • gtgtt12 23
  • t 12 23
  • gtgt y1,5
  • y 1 5
  • gtgt plot(t,y)
  • gtgt plot(t,y,'r-')

63
Loops
  • gtgtx -1.051
  • gtgt for n 128
  • subplot(4,2,n), plot(x,sin(npix))
  • subplot(4,2,n1), plot(x,cos(npix))
  • end

draw sin(npix)and cos sin(npix)for n 1 3
5 7 alongside each other. We may use any legal
variable name as the \loop counter" (n in the
above examples) and it can be made to run through
all of the values in a given vector (18
and 128 in the examples). We may also use for
loops of the type
64
Matlab Graphics
  • x 0pi/1002pi
  • y sin(x)
  • plot(x,y)
  • xlabel('x02\pi')
  • ylabel('Sine of x')
  • title('Plot of the Sine Function')

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

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

67
  • x110
  • y110
  • Zx'y
  • surf(x,y,z)

68
  • gtgt X,Y meshgrid(2.24, 1.23)
  • gtgt Z (X-3).2-(Y-2).2
  • gtgt mesh(X,Y,Z)
  • gtgt title('Saddle'), xlabel('x'),ylabel('y')

69
  • gtgtt00.10.5
  • t 0 0.1000 0.2000 0.3000 0.4000
    0.5000
  • gtgtysin(2pit)
  • y 0 0.5878 0.9511 0.9511 0.5878
    0.0000
  • gtgtwy'y
  • gtgtsurf(w)

70
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

71
Coin Tosses
  • Simulate the outcomes of 1000 biased coin tosses
    with pHead0.4
  • xrand(1000,1)
  • psum(xlt0.4)/1000
  • p
  • 0.4160
Write a Comment
User Comments (0)
About PowerShow.com