Defining a function: - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Defining a function:

Description:

[a b]=matlab(6,3) a= 18. b= 2. rand: Uniformly distributed random numbers and arrays ... Description: The rand function generates arrays of random numbers whose ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 8
Provided by: vikranth8
Category:

less

Transcript and Presenter's Notes

Title: Defining a function:


1
  • Defining a function
  • Open an m-file and write the following statement.
  • function OUTPUT1, OUTPUT2 function_name(INPUT1
    , INPUT2)
  • Statements
  • Statements
  • Save the m-file as the function name. The
    following statement,
  • gtgt function_name(INPUT1, INPUT2)
  • in the command window gives the output
    corresponding to the inputs given .
  • When you call an M-file function from the
    command line or from within another M-file,
    MATLAB parses the function and stores it in
    memory. The parsed function remains in memory
    until cleared with the clear command or you quit
    MATLAB.

2
  • Example of a function MFILE
  • function output1,output2 matlab(inp1,inp2)
  • output1inp1inp2
  • output2inp1/inp2
  • Save the m file as matlab.m.
  • Command window
  • gtgt a bmatlab(6,3)
  • a 18
  • b 2

3
  • rand Uniformly distributed random numbers and
    arrays
  • Syntax Y rand(n) ,Y rand(m,n)
  • Description The rand function generates arrays
    of random numbers whose elements are uniformly
    distributed in the interval (0,1).
  • To generate uniform random numbers with minimum
    and maximum bounds other than 0 and 1
    respectively, use the following formula
  • x ((b-a) Y) a with "Y" being the variable
    from above and "b" representing the upper bound
    and "a" representing the lower bound.     

4
  • randn
  • Syntax Y randn(n) Y randn(m,n)
  • Description The randn function generates arrays
    of random numbers whose elements are normally
    distributed (Gaussian) with mean 0 and variance
    1.
  • To generate 1000 Gaussian random numbers with
    something other than a zero mean and a standard
    deviation (STD) of 1 (both default values), use
    the following formula
  • (STD randn(1000,1)) MEAN  

5
  • A1 2 3 4 5 Mean mean(A) mean of A
  • Median median(A) median of A
  • Standard deviationstd(A) standard deviation of
    A.
  • randperm(10)
  • gtgtans 8 2 10 7 4 3
    6 9 5 1

6
  • HIST plots a histogram
  • hist(y) draws a 10-bin histogram for the data in
    vector y. The bins are equally spaced between the
    minimum and maximum values in y.
  • hist(y,nb) draws a histogram with nb bins.
  • hist(y,x), if x is a vector, draws a histogram
    using the bins specified in x.

7
  • Generate bell-curve histograms from Gaussian
    data.
  • gtgtx -2.90.12.9 y randn(10000,1)
    gtgthist(y,x)
  • hist(y) draws a 10-bin histogram for the data in
    vector y. The bins are equally spaced between the
    minimum and maximum values in y.
  • hist(y,nb) draws a histogram with nb bins.
  • hist(y,x), if x is a vector, draws a histogram
    using the bins specified in x.
Write a Comment
User Comments (0)
About PowerShow.com