Matlab
Matlab
Introduction
Modules
1. Linear Algebra – I
2. Linear Algebra – II
3. Probability Distributions
4. Sampling Distributions
1 LINEAR ALGEBRA - I
matrix having the basis vectors of the domain as its columns, B is the matrix having the
• null(LT)– Finds the basis for the nullspace of the Linear transformation, where LT is
• colspace(B)– Finds the basis for the columnspace of the Linear transformation, where
B is the matrix having the images of the Linear transformation as its columns.
A= sym([1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16])
A =
nsA=null(A)
nsA =
csA=colspace(A)
csA =
lnsA=null(A')
lnsA =
rsA=colspace(A')
rsA =
Note:
One can also find the Four Fundamental Subspaces using .m file.
function[fourfundamentalsubspaces]=ffss(A)
nsA=null(A)
csA=colspace(A)
lnsA=null(A')
rsA=colspace(A')
end
(ii)Enter the matrix A in command window using symbolic math toolbox notation as:
A= sym([1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16])
A =
ffss(A)
nsA =
csA =
rsA =
lnsA =
Example 1.2: Find the linear transformation 𝑇: ℝ2 → ℝ3 such that
.
A=[1,-1;1,1]
A = 2×2
1 -1
1 1
B=[0,2;1,1;2,0]
B = 3×2
0 2
1 1
2 0
LT=B*inv(A)
LT = 3×2
-1 1
0 1
1 1
Example 1.3: Find the range space, null space, rank and nullity of 𝑇, where 𝑇: 𝑉3 (𝑅) → 𝑉4 (𝑅),
defined by 𝑇(𝑒1) = (0,1,0,2), 𝑇(𝑒2 ) = (0,1,1,0), 𝑇(𝑒3 ) = (0,1, −1,4)
A= sym([1,0,0;0,1,0;0,0,1])
A =
B=sym([0,0,0;1,1,1;0,1,-1;2,0,4])
B =
LT=B*inv(A)
LT =
nsLT=null(LT)
nsLT =
rsLT=colspace(B)
rsLT =
rLT=rank(rsLT)
rLT = 2
nLT=rank(nsLT)
nLT = 1
Exercise:
1. Compute the bases for the four fundamental subspace of the following matrix
1 2 3
A=( 5 6 7 ).
9 10 11
13 14 15
2. Compute the bases for the four fundamental subspace of the following matrix
3 4 −2 −5
B = (4 3 2 4 ).
2 5 −6 −14
3. Find the bases for the range space and null space of the transformation 𝑇: ℝ3 → ℝ3
1 2 3
defined by 𝑇(𝑥) = 𝐴𝑥 , where 𝐴 = ( 3 1 0).
−2 1 3
4. Find the bases for the range space and null space of the transformation 𝑇: ℝ3 → ℝ3
2 3 4
defined by 𝑇(𝑥) = 𝐴𝑥 , where A = [ 1 1 2 ].
−2 1 −3
2. LINEAR ALGEBRA II
Singular value decomposition is a process through which any 𝑚 × 𝑛 matrix A can be factored
into 𝐴 = 𝑈 ∗ 𝑆 ∗ 𝑉 = (orthonormal matrix) (diagonal matrix) (orthonormal matrix). The
columns of 𝑈 (𝑚 × 𝑚) are eigenvectors of 𝐴𝐴𝑇 and the columns of V (𝑛 × 𝑛) are eigenvectors
of 𝐴𝑇 𝐴. The 𝑟 singular values on the diagonal of S (𝑚 × 𝑛) are the square roots of the non-zero
eigenvalues of both 𝐴𝐴𝑇 and𝐴𝑇 𝐴.
1 1 1
Example 2.1: For the matrix 𝐴 = [1 2 3] find 𝑄 and 𝑅 matrices representing the 𝑄𝑅
1 3 6
decomposition of 𝐴.
A=[1,1,1;1,2,3;1,3,6]
A = 3×3
1 1 1
1 2 3
1 3 6
[Q,R]=qr(A)
Q = 3×3
-0.5774 0.7071 0.4082
-0.5774 -0.0000 -0.8165
-0.5774 -0.7071 0.4082
R = 3×3
-1.7321 -3.4641 -5.7735
0 -1.4142 -3.5355
0 0 0.4082
Example 2.2: Find the characteristic equation, the eigenvalues and the eigenvectors of
8 −6 2
[−6 7 −4].
2 −4 3
A = [8 -6 2; -6 7 -4; 2 -4 3]
A = 3×3
8 -6 2
-6 7 -4
2 -4 3
p=poly(A)
p = 1×4
1.0000 -18.0000 45.0000 -0.0000
e=eig(A)
e = 3×1
0.0000
3.0000
15.0000
[V,D]=eig(A)
V = 3×3
0.3333 0.6667 -0.6667
0.6667 0.3333 0.6667
0.6667 -0.6667 -0.3333
D = 3×3
0.0000 0 0
0 3.0000 0
0 0 15.0000
1 0 1
Example 2.3: Compute the singular values of the matrix [1 −2 0 ].
0 1 −1
A = [1 0 1; -1 -2 0; 0 1 -1]
A = 3×3
1 0 1
-1 -2 0
0 1 -1
s = svd(A)
s = 3×1
2.4605
1.6996
0.2391
−1
Example 2.4: Find the singular value decomposition of a rectangular matrix [ 2 ].
2
A = [-1; 2; 2]
A = 3×1
-1
2
2
[U,S,V] = svd(A)
U = 3×3
-0.3333 0.6667 0.6667
0.6667 0.6667 -0.3333
0.6667 -0.3333 0.6667
S = 3×1
3
0
0
V = 1
2 3 −1
Example 2.5: Obtain an orthonormal basis of the range of the matrix 𝐴 = [1 1 −1].
0 1 −1
A = sym([2 -3 -1; 1 1 -1; 0 1 -1])
B = orth(A)
B =
Exercise:
A Binomial Distribution object consists of parameters, a model description, and sample data
for a binomial probability distribution
The binomial distribution models the total number of successes in repeated trials from an infinite
population under the following conditions:
Distribution Parameters
N — Number of trials
positive integer value
p — Probability of success
positive scalar value in the range [0,1]
Creation
There are several ways to create a Binomial Distribution probability distribution object.
'Name'
Input Input Parameter
Distribution B
Parameter A
p probability of
'Binomial'
Binomial n number of
success for each
Distribution trials
trial
Chi-Square ν degrees of
'Chisquare' —
Distribution freedom
Exponential
'Exponential' μ mean —
Distribution
'Gamma'
Gamma a shape
b scale parameter
Distribution parameter
Normal σ standard
'Normal' μ mean
Distribution deviation
Poisson
'Poisson' λ mean —
Distribution
Example 2.1: The probability that a man aged 60 will live to be 70 is 0.65. What is the
probability that out of 10 men now aged 60,
(i) at most 6,
(ii) at least 7
will live to be 70?
pd = makedist('Binomial','N',10,'p',0.65)
x = [6];
y = cdf(pd,x)
z=1-y
pd = Binomial Distribution
Binomial distribution
N = 10
p = 0.65
y =
0.4862
z =
0.513
Example 2.2: Create a binomial distribution object by specifying the parameter values. Also compute the
mean of the distribution.
pd = makedist('Binomial','N',30,'p',0.25)
m = mean(pd)
pd =
Binomial distribution
N = 30
p = 0.25
m =
7.5000
Example 2.3: Generate a plot of the binomial pdf for n = 10 and p = 1/2.
x = 0:10;
y = binopdf(x,10,0.5);
plot(x,y,'+')
Example 2.4: Create a normal probability distribution object with mean 50 and SD 30. Generate
a 2-by-3-by-2 array of random numbers from the distribution.
pd = makedist('Normal','mu',50,'sigma',30)
r = random(pd,[2,3,2])
pd =
NormalDistribution
Normal distribution
mu = 50
sigma = 30
r =
r(:,:,1) =
Example 2.5: Create an exponential probability distribution object using the default parameter
values, generate random numbers from the distribution. Construct a histogram using 100 bins
with Exponential distribution fit.
pd = makedist('Exponential')
rng('default') % For reproducibility
r = random(pd,10000,1)
histfit(r,100,'Exponential')
pd =
Exponential Distribution
Exponential distribution
mu = 1
r = 10000×1
0.2049
0.0989
2.0637
0.0906
0.4583
2.3275
1.2783
0.6035
0.0434
0.0357
Example 2.6: Create a Poisson distribution object with the rate parameter, λ, equal to 2.
Compute the pdf and cdf values for the Poisson distribution at the values in the input vector x =
[0,1,2,3,4].
lambda = 2;
pd = makedist('Poisson','lambda',lambda);
x = [0,1,2,3,4];
y = cdf(pd,x)
z = pdf(pd,x)
y = 1×5
0.1353 0.4060 0.6767 0.8571 0.9473
Z = 1×5
0.1353 0.2707 0.2707 0.1804 0.0902
Exercise:
1. Execute the example questions for different distributions with different parameter values.
2. The probability that an individual will suffer a bad reaction from an injection of a given
serum is 0.001. Determine the probability that out of 2000 individuals
(i) exactly 3
(ii) more than 2
individuals will suffer a bad reaction. (use lambda = np)
3. The length of a telephone conversation on a cell phone has been an exponential distribution
and found on an average to be 3 minutes. Find the probability that a random call made from this
phone ends in less than 3 minutes.
4. In a test of 2000 electric bulbs, it was found that the life of a particular make was normally
distributed with an average life of 2040 hours and standard deviation of 60 hours. Estimate the
number of bulbs likely to burn for
(i) more than 1950 hours
(ii) more than 1920 and less than 2160 hours.
4. Sampling Distributions
• chi2=(n-1)*s^2/sig^2 returns the chi square value of a sample of size n and standard
deviation s, drawn from a population of standard deviation sig
• h=ttest(x,mu,'alpha',0.01) return a value of either ‘0’ or ‘1’ for a sample x, drawn from a
population of mean mu, for a t test under 1% level of significance. A score of 0 indicates
to accept the hypothesis, and a score td 1 indicates to reject the hypothesis.
• h=ttest(x,mu,'alpha',0.05) return a value of either ‘0’ or ‘1’ for a sample x, drawn from a
population of mean mu, for a t test under 5% level of significance. A score of 0 indicates
to accept the hypothesis, and a score td 1 indicates to reject the hypothesis.
• [h,p,st] = chi2gof(bins,'Ctrs',bins,...
'Frequency',o, ...
'Expected',e,...
'Alpha',0.01)
returns the h value of either ‘0’ or ‘1’, p-the p value, chi2stat-the chi square value, O-the
observed frequencies, E-the expected frequencies, at 1% level of significance. A score of
0 indicates to accept the hypothesis, and a score td 1 indicates to reject the hypothesis.
• [h,p,st] = chi2gof(bins,'Ctrs',bins,...
'Frequency',o, ...
'Expected',e,...
'Alpha',0.05)
returns the h value of either ‘0’ or ‘1’, p-the p value, chi2stat-the chi square value, O-the
observed frequencies, E-the expected frequencies, at 5% level of significance. A score of
0 indicates to accept the hypothesis, and a score td 1 indicates to reject the hypothesis.
Example 1: Find the student’s t for a sample of size 10, sample mean 0.742, sample standard
standard deviation 0.04 and population mean 0.7.
n=10;
xb=0.742;
s=0.04;
mu=0.7;
t = (xb-mu)/(s/sqrt(n))
t= 3.3204
Example 2: The following are the IQs of a randomly chosen sample of 10 boys: 70, 120, 110,
101,88, 83, 95, 98, 107, 100. Find the student’s t for the given sample taking the mean of
population to be 100.
t=-0.6203
Example 3: Find the chi square value of the sample 1.9, 2.4, 3.0, 3.5 and 4.2 chosen from a
normal population with mean 3 and standard deviation 1.
chi2=3.26
x=[5,2,8,-1,3,0,6,-2,1,5,0,4];
n=length(x);
xb=mean(x);
s=std(x);
mu=0;
t = (xb-mu)/(s/sqrt(n));
h1=ttest(x,mu,'alpha',0.01)
if h1==1
'reject the hypothesis at 1% level of significance'
else 'accept the hypothesis at 1% level of significance'
end
h5=ttest(x,mu,'alpha',0.05)
if h5==1
'reject the hypothesis at 5% level of significance'
else 'accept the hypothesis at 5% level of significance'
end
h1= 0
ans = ‘accept the hypothesis at 1% level of significance.
h5= 1
ans = ‘reject the hypothesis at 5% level of significance.
Example 5: The observed frequencies and expected frequencies are given as follows:
O 37 101 84 18
E 30 90 90 30
Using chi square test, test the hypothesis that the observed frequencies are consistent with the
expected frequencies, at 1% and 5% level of significance.
bins = 0:3;
o = [37 101 84 18];
n = sum(o);
e = [30 90 90 30];
[h1,p,st] = chi2gof(bins,'Ctrs',bins,...
'Frequency',o, ...
'Expected',e,...
'Alpha',0.01)
if h1==1
'reject the hypothesis at 1% level of significance'
else 'accept the hypothesis at 1% level of significance'
end
[h5,p,st] = chi2gof(bins,'Ctrs',bins,...
'Frequency',o, ...
'Expected',e,...
'Alpha',0.05)
if h5==1
'reject the hypothesis at 5% level of significance'
else 'accept the hypothesis at 5% level of significance'
end
chi2stat:8.1778
h1= 0
ans = ‘accept the hypothesis at 1% level of significance.
h5= 1
ans = ‘reject the hypothesis at 5% level of significance.