0% found this document useful (0 votes)
13 views20 pages

Matlab

The document outlines a curriculum on Linear Algebra and Probability Distributions, detailing learning outcomes, syntax, and examples for each module. It covers topics such as the Four Fundamental Subspaces, orthonormal bases, eigenvalues, and the binomial distribution. Exercises are provided to reinforce the concepts learned in each section.

Uploaded by

arya777r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views20 pages

Matlab

The document outlines a curriculum on Linear Algebra and Probability Distributions, detailing learning outcomes, syntax, and examples for each module. It covers topics such as the Four Fundamental Subspaces, orthonormal bases, eigenvalues, and the binomial distribution. Exercises are provided to reinforce the concepts learned in each section.

Uploaded by

arya777r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Contents

Introduction
Modules
1. Linear Algebra – I
2. Linear Algebra – II
3. Probability Distributions
4. Sampling Distributions
1 LINEAR ALGEBRA - I

Topic learning outcomes:

Student will be able to:


1. Find Four Fundamental Subspaces,
2. Basis for the Range and null space of a Linear Transformation.

Syntax and description:

• sym() -Stores the values in symbolic math toolbox notation.

• null(A)–Finds the basis for the nullspace of the matrix A.

• colspace(A)–Finds the basis for the columnspace of the matrix A.

• null(A')–Finds the basis for the left nullspace of the matrix A.

• colspace(A')– Finds the basis for the rowspace of the matrix A.

• B*inv(A)– Finds the matrix representation of Linear transformation, where A is the

matrix having the basis vectors of the domain as its columns, B is the matrix having the

images of the basis vectors as its columns.

• null(LT)– Finds the basis for the nullspace of the Linear transformation, where LT is

the matrix representation of the Linear transformation.

• 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.

• rank(colspace(B)) – Finds the rank of the Linear transformation

• rank(null(LT))– Finds the nullity of the Linear transformation.


Example 1.1: Obtain the bases for the Four Fundamental Subspaces of the matrix A, by storing
the matrix using symbolic math toolbox notation.

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.

(i) Create a function file with the name ffss.

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 =

(iii) call the m file in the command window as ffss(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

Topic learning outcomes:


Student will be able to:
1. Obtain orthonormal bases for vectors. Eigen values, Eigen vectors & diagonalization of a
matrix.
2. Form QR-factorization and singular value decomposition (SVD) of a matrix.

Syntax and description:

• [Q,R]=qr(A)-returns an upper triangular matrix R and a unitary matrix Q such that A =


Q*R.
• The orthogonal or QR, factorization expresses any rectangular matrix as the product of an
orthogonal or unitary matrix and an upper triangular matrix. An orthogonal matrix or a
matrix with orthonormal columns, is a real matrix whose columns all have unit length
and are perpendicular to each other. If Q is orthogonal then 𝑄 𝑇 𝑄 = 𝐼
• e=eig(A)-returns a column vector containing the eigenvalues of square matrix A.
• [V,D]=eig(A)-returns diagonal matrix D of eigenvalues and matrix V whose columns
are the corresponding right eigenvectors, so that 𝐴 ∗ 𝑉 = 𝑉 ∗ 𝐷.
• The eigenvalue problem is to determine the solution to the equation 𝐴𝑣 = 𝜆𝑣, where 𝐴
is an 𝑛 × 𝑛 matrix, 𝑣 is a column vector of length n and 𝜆 is a scalar. The values of 𝜆 that
satisfy the equation are the eigenvalues. The corresponding values of 𝑣 that satisfy the
equation are the right eigenvectors.
• s=svd(A)-returns the singular values of matrix 𝐴 in descending order.
• [U,S,V]=svd(A)-performs a singular value decomposition of matrix 𝐴 such that
𝐴 = 𝑈 ∗ 𝑆 ∗ 𝑉.

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:

1. If 𝑣1 = (0, 1, 2), 𝑣2 = (1, 1, 2), 𝑣3 = (1, 0, 1) construct an orthonormal basis.


1 0 0
2. Find the QR factorization of A = (1 1 0).
1 1 1
1 1 1
1 4
3. Consider the matrix D = (1 1), find a factorization D = QR.
1 1
1 1

4. Find the characteristic equation, eigenvalues of the matrix B = (21 2


1). Also diagonalize it.
2 2
5 0 4
5. Diagonalize the matrix A = (0 5 4). Also find its characteristic equation and its
4 4 9
eigenvalues.
4 11 14
6. Obtain the SVD of matrix A = ( ).
8 7 −2
3. Probability Distributions

Topic learning outcomes:


Student will be able to:

1. Generate probability distributions based on given parameters.


2. Plot the probability distribution functions.
3. Fit a probability distribution.

Syntax and description:

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:

• Only two outcomes are possible for each of n trials.


• The probability of success for each trial is constant.
• All trials are independent of each other.

The binomial distribution uses the following parameters.

Parameter Description Support


N Number of trials positive integer
P Probability of success 0≤p≤1

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.

• Create a distribution with specified parameter values using makedist.


• Fit a distribution to data using fitdist.
• Interactively fit a distribution to data using the Distribution Fitter app.

'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) =

31.4751 98.6682 42.3748


122.9576 48.9985 25.4413
r(:,:,2) =

96.1240 24.0135 17.3124


1.8054 36.8490 13.3768

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

Syntax and description:


• t = (xb-mu)/(s/sqrt(n)) returns the t score for a sample of size n, mean xb, standard
deviation s, coming from a normal population of mean mu.

• 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.

x=[70 120 110 101 88 83 95 98 107 100];


n=length(x);
xb=mean(x);
s=std(x);
mu=100;
t = (xb-mu)/(s/sqrt(n))

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.

x=[1.9 2.4 3.0 3.5 4.2];


n=length(x);
xb=mean(x);
s=std(x);
sig=1;
chi2=(n-1)*s^2/sig^2

chi2=3.26

Example 4: A certain stimulus administered to each of 12 patients resulted in the following


change in blood pressure: 5, 2, 8, -1, 3, 0, 6, -2, 1, 5, 0, 4. Can it be concluded that, on the whole,
the stimulus will change the blood pressure at 1% and 5% level of significance.

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.

You might also like