Introduction to Matlab

Document Sample

```					                    Introduction to MATLAB

Dr. Lei Zheng
Department of Biomedical, Industrial and Human Factors Engineering
Wright State University
What is Matlab?
   Matlab (Matrix laboratory)
 An interactive software system for numerical computations and
graphics
 especially designed for matrix computations (solving systems of
linear equations, computing eigenvalues and eigenvectors,
factoring matrices, etc. )
 BASIC-like syntax
 basic data type: 2- dimensional floating-point matrix
 comprehensive toolboxes for easy access to standard algorithms
from many fields:
 popular for experimental/rapid-prototype number crunching
 widely used as a visualization and teaching tool

2
What Matlab is NOT?
   not a strong general purpose programming language
• limited support for other data structures
• few software-engineering features;
typical MATLAB programs are only a few lines long
• not suited for teaching OOP
   not a high-performance language (but fast matrix operators)
   not freely available

3
MATLAB matrices (1)
   Generate a “magic square” with equal row/column/diagonal sums and
assign the resulting 3 × 3 matrix to variable a:
>> a = magic(3)
a=
8 1 6
3 5 7
4 9 2
Assignments and subroutine calls normally end with a semicolon.
Without, MATLAB will print each result. Useful for debugging!
Results from functions not called inside an expression are assigned to
the default variable ans.
Type help magic for the manual page of this library function.

4
MATLAB matrices (2)
   zeros( ) --- Zeros matrix
zeros(N) is an N-by-N matrix of zeros.
zeros(M,N) is an M-by-N matrix of zeros.:
>> b = zeros(3,2)
   Eye( ) ----Identity matrix.
eye(N) is the N-by-N identity matrix.
eye(M,N) is an M-by-N matrix with 1's on the diagonal and zeros
elsewhere.
>> b = eye(3,2)
   Ones( ) ---Ones array.
ones(N) is an N-by-N matrix of ones.
ones(M,N) is an M-by-N matrix of ones..
>> ones(3,2)
5
MATLAB matrices (3)
   Rand( ) --- Uniformly distributed random numbers.
rand(N) is an N-by-N matrix with random entries, chosen from
a uniform distribution on the interval (0.0,1.0).
rand(M,N) is M-by-N matrices with random entries.
>> b = rand(1)
   Randn( ) --- Normally distributed random numbers.
randn(N) is an N-by-N matrix with random entries, chosen from
a normal distribution with mean zero, variance one and standard
deviation one.
randn(M,N) is M-by-N matrices with random
>> b = randn(2,3)

6
MATLAB matrices (4)

7
MATLAB matrices (5)

8
MATLAB matrices (6)

9
MATLAB matrices (7)

10
Some common functions and operators

11
An Example of Matrix Computation
   Given linear equations:
3x+2y+5z=7                              3 2 5  x   7 
           
2x+8y+7z=9                              2 8 7* y    9 
7x+3y+6z=10                             7 3 6   z  10 
           
1                            1
 3 2 5  3 2 5  x   3 2 5  7 
                              
 2 8 7 * 2 8 7 * y    2 8 7 * 9 
 7 3 6   7 3 6   z   7 3 6  10 
                              
1
 x   3 2 5  7 
              
 y    2 8 7 * 9 
 z   7 3 6  10 
              
>> s =inv([3, 2, 5; 2, 8, 7; 7, 3, 6])*[7, 9, 10]’

12
Some Amazing Functions (1)
  limit( ) ----limit of an expression.
limit(F,x,a) takes the limit of the symbolic expression F as x -> a.
limit(F,x,a,'right') or limit(F,x,a,'left') specify the direction
of a one-sided limit.
Examples:
(1) limit sin x
x0      x

>> limit(sin(x)/x, x, 0)                        %% returns 1

sin( x  h)  sin( x)
(2)   limit
h 0           h

>> syms x h; limit((sin(x+h)-sin(x))/h,h,0)               %%returns cos(x)

13
Some Amazing Functions (2)
   int( ) ---- Integrate.
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.
Examples:
1
(1)
 1  x 2 dx
>>syms x; int(1/(1+x^2), x)          %%returns      atan(x)

1
(2)
 x log(1  x)dx
0

>> syms x1; int(x1*log(1+x1),0,1) %%returns            1/4

14
Plotting (1)
   >> x=-50:0.1:50; y=x.^2; plot(x,y); title('y=x^2');

15
Plotting (2)
   >> fplot('sin(3*x)', [0,2*pi])

16
Plotting (3)

17
Functions and m-files
To define a new function, write into a file fname.m the lines
function v= fname(x)
Only the function that has the same name as the m-file in which it is
defined can be called from outside the file; all other functions are only
visible inside the file. The function keyword sets the variable whose
value will be returned and lists the parameter variables.
The m-file must be in the current directory (cd) or MATLAB’s search
path (path) to become accessible.
M-files can also contain just sequences of statements instead of a func-
tion definition. These are called simply by typing their name.

18
References
   http://www.mathworks.com/access/helpdesk/help/techdoc/helptoc.html
   Introduction to MATLAB, www.cl.cam.ac.uk/teaching/2006/UnixTools/matlab-
slides.pdf

19

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 0 posted: 2/1/2013 language: English pages: 19
xuxianglp http://
About