matlab conceptsjgjhjgug
TRANSCRIPT
-
8/13/2019 MATLAB Conceptsjgjhjgug
1/10
x = 1900:10:2000; 1900 to 2000 in with step size of 10
randnNormally distributed pseudorandom numbers
r = randn(n)returns an n-by-nmatrix containing pseudorandom values drawn from thestandard normal distribution.
r = randn(m,n)or r = randn([m,n])returns an m-by-nmatrix.
r = randn(m,n,p,...)or r = randn([m,n,p,...])returns an m-by-n-by-p-by-... array.
r = randnreturns a scalar.
r = randn(size(A))returns an array the same size as A.
r = randn(...,'double')or r = randn(...,'single')returns an array of normal values ofthe specified class.
Example 1
Generate values from a normal distribution with mean 1 and standard deviation 2:
r = 1 + 2.*randn(100,1);
Example 2
Generate values from a bivariate normal distribution with specified mean vector and covariancematrix:
mu = [1 2];Sigma = [1 .5; .5 2]; R = chol(Sigma);
z = repmat(mu,100,1) + randn(100,2)*R;
===================================================
-
8/13/2019 MATLAB Conceptsjgjhjgug
2/10
L = chol(A,'lower')produces a lower triangular matrix Lfrom the diagonal and lower triangle of
matrix A, satisfying the equation L*L'=A. The cholfunction assumes that Ais (complex Hermitian)
symmetric. If it is not, choluses the (complex conjugate) transpose of the lower triangle as the upper
triangle. When Ais sparse, this syntax of cholis typically faster. Matrix Amust be positive definite. R =
chol(A,'upper')is the same as R = chol(A).
abs
Absolute value (magnitude)
hist
Histogram plot
hist(data)creates a histogram bar plot of data. Elements in dataare sorted into 10 equally
spaced bins along the x-axis between the minimum and maximum values of data. Bins are
displayed as rectangles such that the height of each rectangle indicates the number of elements in
the bin.
example
http://www.mathworks.in/help/matlab/ref/hist.html#inputarg_datahttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_datahttp://www.mathworks.in/help/matlab/ref/hist.html#btsgtrz-1http://www.mathworks.in/help/matlab/ref/hist.html#btsgtrz-1http://www.mathworks.in/help/matlab/ref/hist.html#btsgtrz-1http://www.mathworks.in/help/matlab/ref/hist.html#inputarg_data -
8/13/2019 MATLAB Conceptsjgjhjgug
3/10
The values in dataare sorted among 10 equally spaced bins between 0 and 10, the minimum and
maximum values.
hist(data,nbins)sorts datainto the number of bins specified by nbins.
example
Specify Number of Bins
Initialize the random-number generator to make the output of randnrepeatable. Generate 1,000normally distributed pseudorandom numbers.
rng(0,'twister')
data = randn(1000,1);
Create a histogram plot of datasorted into 50 equally spaced bins.
figure
nbins = 50;hist(data,nbins)
http://www.mathworks.in/help/matlab/ref/hist.html#inputarg_datahttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_datahttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_nbinshttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_nbinshttp://www.mathworks.in/help/matlab/ref/hist.html#btsgtr0-1http://www.mathworks.in/help/matlab/ref/hist.html#btsgtr0-1http://www.mathworks.in/help/matlab/ref/hist.html#btsgtr0-1http://www.mathworks.in/help/matlab/ref/hist.html#inputarg_nbinshttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_data -
8/13/2019 MATLAB Conceptsjgjhjgug
4/10
hist(data,xvalues)uses the values in vector xvaluesto determine the bin intervals and sortsdatainto the number of bins determined by length(xvalues). To specify the bin centers, set
xvaluesequal to a vector of evenly spaced values. The first and last bins extend to cover the
minimum and maximum values in data.
example
Specify Bin Intervals
Create a figure divided into three subplots. Plot a histogram of the same data set in all three
subplots using different bin intervals for each histogram.
First, initialize the random-number generator to make the output of randnrepeatable. Generate1,000 normally distributed pseudorandom numbers.
rng(0,'twister')data = randn(1000,1);
In the upper subplot specify the bin centers using a vector of evenly spaced values that span the
values in data.
figure
subplot(3,1,1)xvalues1 = -4:4;hist(data,xvalues1)
In the middle subplot specify the bin centers using a vector of evenly spaced values that do not
span the values in data. Notice that the first and last bins extend to cover the minimum and
maximum values in data.
subplot(3,1,2)
http://www.mathworks.in/help/matlab/ref/hist.html#inputarg_datahttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_datahttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_xvalueshttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_xvalueshttp://www.mathworks.in/help/matlab/ref/hist.html#btsgtr0-1_1http://www.mathworks.in/help/matlab/ref/hist.html#btsgtr0-1_1http://www.mathworks.in/help/matlab/ref/hist.html#btsgtr0-1_1http://www.mathworks.in/help/matlab/ref/hist.html#inputarg_xvalueshttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_data -
8/13/2019 MATLAB Conceptsjgjhjgug
5/10
xvalues2 = -2:2;
hist(data,xvalues2)
In the lower subplot set the bin intervals using a vector of unevenly spaced values. Theseunevenly spaced values are not used as the bin centers. MATLAB
indicates the specified values
by markers along the x-axis.
subplot(3,1,3)
xvalues3 = [-4,-2.5,0,0.5,1,3];hist(data,xvalues3)
hist(axes_handle,___)plots into the axes specified by axes_handleinstead of into the
current axes (gca). The option axes_handlecan precede any of the input argumentcombinations in the previous syntaxes.
nelements= hist(___)returns a row vector, nelements, indicating the number of elements ineach bin.
nelementsNumber of elements in each bin row vector
centersBin centers row vector
[nelements,centers] = hist(___)returns an additional row vector, centers, indicating thelocation of each bin center on the x-axis. To plot the histogram, you can use
bar(centers,nelements).
example
http://www.mathworks.in/help/matlab/ref/hist.html#inputarg_axes_handlehttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_axes_handlehttp://www.mathworks.in/help/matlab/ref/hist.html#outputarg_nelementshttp://www.mathworks.in/help/matlab/ref/hist.html#outputarg_nelementshttp://www.mathworks.in/help/matlab/ref/hist.html#outputarg_nelementshttp://www.mathworks.in/help/matlab/ref/hist.html#outputarg_centershttp://www.mathworks.in/help/matlab/ref/hist.html#outputarg_centershttp://www.mathworks.in/help/matlab/ref/hist.html#btsgtr1-1http://www.mathworks.in/help/matlab/ref/hist.html#btsgtr1-1http://www.mathworks.in/help/matlab/ref/hist.html#btsgtr1-1http://www.mathworks.in/help/matlab/ref/hist.html#outputarg_centershttp://www.mathworks.in/help/matlab/ref/hist.html#outputarg_nelementshttp://www.mathworks.in/help/matlab/ref/hist.html#outputarg_nelementshttp://www.mathworks.in/help/matlab/ref/hist.html#inputarg_axes_handle -
8/13/2019 MATLAB Conceptsjgjhjgug
6/10
Plot Histogram Using Bar
Initialize the random-number generator to make the output of randnrepeatable. Generate 1,000normally distributed pseudorandom numbers.
rng(0,'twister')data = randn(1000,1);
Sort datainto 10 equally spaced bins. Get the number of elements in each bin and the locationsof the bin centers.
[nelements,centers] = hist(data)
nelements =
4 27 88 190 270 243 123 38 13 4centers =
-2.8915 -2.2105 -1.5294 -0.8484 -0.1673 0.5137 1.1947
1.8758 2.5568 3.2379
Use barto plot the histogram.
figure
bar(centers,nelements)
Why Do Random Numbers Repeat After Startup?
All the random number functions, rand, randn, randi, and randperm, draw values from a
shared random number generator. Every time you start MATLAB, the generator resets itself to
the same state. Therefore, a command such as rand(2,2)returns the same result any time you
execute it immediately following startup. Also, any script or function that calls the randomnumber functions returns the same result whenever you restart.
-
8/13/2019 MATLAB Conceptsjgjhjgug
7/10
If you want to avoid repeating the same random number arrays when MATLAB restarts, then
execute the command,
rng('shuffle');
before callingrand
,randn
,randi
, orrandperm
to ensure that you do not repeat a result from aprevious MATLAB session.
There also can be situations in which you want to repeat a result that you got at the start of a
MATLAB session, but you do not want to restart to do it. You can reset the generator to the
startup state at any time in a MATLAB session:
rng('default');
When you execute rng('default'), the ensuing random number commands return results that
match the output of a new MATLAB session.
subplot
Description
subplot(m,n,p) divides the current figure into an m-by-ngrid and creates an axes in the
grid position specified by p. MATLAB
numbers its grids by row, such that the first grid is thefirst column of the first row, the second grid is the second column of the first row, and so on.
example
subplot(m,n,p,'replace')deletes any existing axes in grid location pand creates a new axes.
subplot(m,n,p,'align') creates a new axes so that the plot boxes are aligned. This is thedefault behavior.
example
subplot('Position',positionVector)creates a new axes at the position specified by
positionVector. The positionVectoris a four-element vector of the form
[left,bottom,width,height], such that the entries are normalized values between 0.0 to 1.0.
If the position vector specifies an axes that overlaps any previous axes, then the new axesreplaces the existing ones.
example
subplot(___,Name,Value)specifies properties for the axes using any of the input argument
combinations in the previous syntaxes and one or more Name,Valuepair arguments.
http://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_mhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_mhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_nhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_nhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_phttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_phttp://www.mathworks.in/help/matlab/ref/subplot.html#btw2bg8-1http://www.mathworks.in/help/matlab/ref/subplot.html#btw2bg8-1http://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_mhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_mhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_nhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_nhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_phttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_phttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_mhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_mhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_nhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_nhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_phttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_phttp://www.mathworks.in/help/matlab/ref/subplot.html#btw2bh4-1http://www.mathworks.in/help/matlab/ref/subplot.html#btw2bh4-1http://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_positionVectorhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_positionVectorhttp://www.mathworks.in/help/matlab/ref/subplot.html#btw2bhs-1http://www.mathworks.in/help/matlab/ref/subplot.html#btw2bhs-1http://www.mathworks.in/help/matlab/ref/subplot.html#namevaluepairargumentshttp://www.mathworks.in/help/matlab/ref/subplot.html#namevaluepairargumentshttp://www.mathworks.in/help/matlab/ref/subplot.html#namevaluepairargumentshttp://www.mathworks.in/help/matlab/ref/subplot.html#btw2bhs-1http://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_positionVectorhttp://www.mathworks.in/help/matlab/ref/subplot.html#btw2bh4-1http://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_phttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_nhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_mhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_phttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_nhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_mhttp://www.mathworks.in/help/matlab/ref/subplot.html#btw2bg8-1http://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_phttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_nhttp://www.mathworks.in/help/matlab/ref/subplot.html#inputarg_m -
8/13/2019 MATLAB Conceptsjgjhjgug
8/10
example
h= subplot(___)returns the handle to the axes created by the subplotfunction.
example
subplot(h)makes the axes with handle hthe current axes for subsequent plotting commands.
bar
Bar graph
bar(Y)draws one bar for each element in Y.
example
bar(x,Y)draws bars for each column in Yat locations specified in x.
example
http://www.mathworks.in/help/matlab/ref/subplot.html#btw2bht-1http://www.mathworks.in/help/matlab/ref/subplot.html#btw2bht-1http://www.mathworks.in/help/matlab/ref/subplot.html#outputarg_hhttp://www.mathworks.in/help/matlab/ref/subplot.html#btw2bht-1_1http://www.mathworks.in/help/matlab/ref/subplot.html#btw2bht-1_1http://www.mathworks.in/help/matlab/ref/subplot.html#outputarg_hhttp://www.mathworks.in/help/matlab/ref/subplot.html#outputarg_hhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_Yhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_Yhttp://www.mathworks.in/help/matlab/ref/bar.html#btj3hs7-1http://www.mathworks.in/help/matlab/ref/bar.html#btj3hs7-1http://www.mathworks.in/help/matlab/ref/bar.html#inputarg_xhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_xhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_Yhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_Yhttp://www.mathworks.in/help/matlab/ref/bar.html#bthxceuhttp://www.mathworks.in/help/matlab/ref/bar.html#bthxceuhttp://www.mathworks.in/help/matlab/ref/bar.html#bthxceuhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_Yhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_xhttp://www.mathworks.in/help/matlab/ref/bar.html#btj3hs7-1http://www.mathworks.in/help/matlab/ref/bar.html#inputarg_Yhttp://www.mathworks.in/help/matlab/ref/subplot.html#outputarg_hhttp://www.mathworks.in/help/matlab/ref/subplot.html#btw2bht-1_1http://www.mathworks.in/help/matlab/ref/subplot.html#outputarg_hhttp://www.mathworks.in/help/matlab/ref/subplot.html#btw2bht-1 -
8/13/2019 MATLAB Conceptsjgjhjgug
9/10
bar(___,width)sets the relative bar width and controls the separation of bars within a groupand can include any of the input arguments in previous syntaxes.
example
bar(___,style)specifies the style of the bars and can include any of the input arguments inprevious syntaxes.
example
bar(___,bar_color)displays all bars using the color specified by the single-letter abbreviation
of bar_colorand can include any of the input arguments in previous syntaxes.
example
bar(___,Name,Value)sets the property names to the specified values and can include any ofthe input arguments in previous syntaxes.
http://www.mathworks.in/help/matlab/ref/bar.html#inputarg_widthhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_widthhttp://www.mathworks.in/help/matlab/ref/bar.html#bthxce9http://www.mathworks.in/help/matlab/ref/bar.html#bthxce9http://www.mathworks.in/help/matlab/ref/bar.html#inputarg_stylehttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_stylehttp://www.mathworks.in/help/matlab/ref/bar.html#bthxcfmhttp://www.mathworks.in/help/matlab/ref/bar.html#bthxcfmhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_bar_colorhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_bar_colorhttp://www.mathworks.in/help/matlab/ref/bar.html#bthxcfzhttp://www.mathworks.in/help/matlab/ref/bar.html#bthxcfzhttp://www.mathworks.in/help/matlab/ref/bar.html#namevaluepairargumentshttp://www.mathworks.in/help/matlab/ref/bar.html#namevaluepairargumentshttp://www.mathworks.in/help/matlab/ref/bar.html#namevaluepairargumentshttp://www.mathworks.in/help/matlab/ref/bar.html#bthxcfzhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_bar_colorhttp://www.mathworks.in/help/matlab/ref/bar.html#bthxcfmhttp://www.mathworks.in/help/matlab/ref/bar.html#inputarg_stylehttp://www.mathworks.in/help/matlab/ref/bar.html#bthxce9http://www.mathworks.in/help/matlab/ref/bar.html#inputarg_width -
8/13/2019 MATLAB Conceptsjgjhjgug
10/10