A=[0 1;1 0]
A =
0 1
1 0
A0=A;
[Q,R]=qr(A0);A1=R*Q
A1 =
0 1
1 0
n=length(A),m=n-1
n =
2
m =
1
del=(A0(m,m)-A0(n,n))/2
del =
0
mi=A0(n,n)-(A0(m,n)*A0(n,m))/(abs(del)+sqrt(del^2+A0(m,n)*A0(n,m)))
mi =
-1
I=eye(2);
[Q,R]=qr(A0-mi*I);A1=R*Q+mi*I
A1 =
1.0000 0
0.0000 -1.0000
A
A =
0 1
1 0
A=[7 -2 -4 -2;10 -3 -6 -2; 6 -2 -3 -2;2 0 -2 1]
A =
7 -2 -4 -2
10 -3 -6 -2
6 -2 -3 -2
2 0 -2 1
A=[7 -2 -4 -2;10 -3 -6 -2; 6 -2 -3 -2;2 0 -2 -1]
A =
7 -2 -4 -2
10 -3 -6 -2
6 -2 -3 -2
2 0 -2 -1
eig(A)
ans =
1.0000
-1.0000
1.0000
-1.0000
AA=A;
AA=hess(A);
AA
AA =
7.0000 4.0567 2.6149 -0.8398
-11.8322 -7.0000 -4.3717 0.8796
0 -0.0000 0.1584 0.8327
0 0 1.1708 -0.1584
[Q,R]=qr(AA);AA=R*Q
AA =
0.0370 15.9515 -0.4911 5.2068
0.0626 -0.0370 -0.2692 0.0612
0 0.0000 0.1135 1.1768
0 0 0.8388 -0.1135
[Q,R]=qr(AA);AA=R*Q
AA =
7.0000 4.0567 2.6149 -0.8398
-11.8322 -7.0000 -4.3717 0.8796
0 -0.0000 0.1584 0.8327
0 0 1.1708 -0.1584
AA-hess(A)
ans =
1.0e-12 *
0.3020 0.5329 0.1457 -0.0313
0.5222 -0.2993 0.1332 -0.0309
0 -0.0000 -0.0006 0.0004
0 0 -0.0013 -0.0002
[Q,R]=qr(AA);AA=R*Q
AA =
0.0370 15.9515 -0.4911 5.2068
0.0626 -0.0370 -0.2692 0.0612
0 0.0000 0.1135 1.1768
0 0 0.8388 -0.1135
AA-hess(A)
ans =
-6.9630 11.8948 -3.1060 6.0467
11.8948 6.9630 4.1026 -0.8184
0 0.0000 -0.0449 0.3441
0 0 -0.3320 0.0449
AA-hess(A)
ans =
-6.9630 11.8948 -3.1060 6.0467
11.8948 6.9630 4.1026 -0.8184
0 0.0000 -0.0449 0.3441
0 0 -0.3320 0.0449
AA=hess(A);
AA
AA =
7.0000 4.0567 2.6149 -0.8398
-11.8322 -7.0000 -4.3717 0.8796
0 -0.0000 0.1584 0.8327
0 0 1.1708 -0.1584
[Q,R]=qr(AA);AA=R*Q
AA =
0.0370 15.9515 -0.4911 5.2068
0.0626 -0.0370 -0.2692 0.0612
0 0.0000 0.1135 1.1768
0 0 0.8388 -0.1135
[Q,R]=qr(AA);AA=R*Q
AA =
7.0000 4.0567 2.6149 -0.8398
-11.8322 -7.0000 -4.3717 0.8796
0 -0.0000 0.1584 0.8327
0 0 1.1708 -0.1584
[Q,R]=qr(AA);AA=R*Q
AA =
0.0370 15.9515 -0.4911 5.2068
0.0626 -0.0370 -0.2692 0.0612
0 0.0000 0.1135 1.1768
0 0 0.8388 -0.1135
[Q,R]=qr(AA);AA=R*Q
AA =
7.0000 4.0567 2.6149 -0.8398
-11.8322 -7.0000 -4.3717 0.8796
0 -0.0000 0.1584 0.8327
0 0 1.1708 -0.1584
AA-hess(A)
ans =
1.0e-11 *
0.0744 0.1322 0.0373 -0.0076
0.1297 -0.0741 0.0311 -0.0082
0 -0.0000 -0.0003 0.0001
0 0 -0.0002 0.0002
[Q,R]=qr(AA);AA=R*Q
AA =
0.0370 15.9515 -0.4911 5.2068
0.0626 -0.0370 -0.2692 0.0612
0 0.0000 0.1135 1.1768
0 0 0.8388 -0.1135
[Q,R]=qr(AA);AA=R*Q
AA =
7.0000 4.0567 2.6149 -0.8398
-11.8322 -7.0000 -4.3717 0.8796
0 -0.0000 0.1584 0.8327
0 0 1.1708 -0.1584
AA-hess(A)
ans =
1.0e-11 *
0.1136 0.2027 0.0573 -0.0113
0.1991 -0.1136 0.0473 -0.0131
0 -0.0000 -0.0007 0.0002
0 0 -0.0003 0.0005
AA=hess(A);
n=length(A);m=n-1;
I=eye(4);
mi=AA(n,n);[Q,R]=qr(AA-mi*I);AA=R*Q+mi*I
AA =
0.1949 15.9492 0.1842 5.2273
0.0603 -0.1949 -0.2608 0.0440
0 0.0000 0.3683 1.1140
0 0 0.7759 -0.3683
mi=AA(n,n);[Q,R]=qr(AA-mi*I);AA=R*Q+mi*I
AA =
1.8857 15.7264 3.8799 -3.4690
-0.1625 -1.8857 -0.5641 0.1509
0 -0.0000 0.9245 0.2479
0 0 0.5859 -0.9245
mi=AA(n,n);[Q,R]=qr(AA-mi*I);AA=R*Q+mi*I
AA =
0.9760 15.8919 2.6747 4.4896
0.0030 -0.9760 -0.3383 -0.0551
0 0.0000 0.9959 0.3607
0 0 0.0226 -0.9959
mi=AA(n,n);[Q,R]=qr(AA-mi*I);AA=R*Q+mi*I
AA =
1.0000 15.8889 2.7250 -4.4589
-0.0000 -1.0000 -0.3430 0.0580
0 -0.0000 1.0000 -0.3380
0 0 0.0000 -1.0000
mi=AA(n,n);[Q,R]=qr(AA-mi*I);AA=R*Q+mi*I
AA =
1.0000 15.8889 2.7250 4.4590
-0.0000 -1.0000 -0.3430 -0.0580
0 -0.0000 1.0000 0.3381
0 0 -0.0000 -1.0000
format long
AA
AA =
1.000000000195093 15.888886907467519 2.725000140927230 4.458963215277410
-0.000000000024557 -0.999998353618960 -0.343006771114585 -0.057978726425901
0 -0.000009600829579 0.999998353454758 0.338061423429690
0 0 -0.000000000182742 -1.000000000030889
mi=AA(n,n);[Q,R]=qr(AA-mi*I);AA=R*Q+mi*I
AA =
1.000000000000001 0.000050384474267 16.120738515862158 -4.459427719227266
-0.000000000000000 0.999998927992718 -0.342993537892770 0.343007053276551
0 -0.000006250885741 -0.999998927992718 -0.000001072049524
0 0 0.000000000000001 -1.000000000000000
mi=AA(n,n);[Q,R]=qr(AA-mi*I);AA=R*Q+mi*I
AA =
1.000000000000001 -0.000000000000001 2.392618117596035 16.554156107325554
-0.000000000000000 1.000000000000000 0.176318006977842 -0.451891666290069
0 -0.000000000000000 -1.000000000000000 0.000000000000001
0 0 -0.000000000000000 -1.000000000000000
mi=AA(n,n);[Q,R]=qr(AA-mi*I);AA=R*Q+mi*I
AA =
1.000000000000001 -0.000000000000001 0.451795622778518 -16.720065388556076
-0.000000000000000 1.000000000000000 0.227659581936163 0.428328416521370
0 -0.000000000000000 -1.000000000000000 -0.000000000000001
0 0 0.000000000000000 -1.000000000000000
eig(A)
ans =
1.000000000000005
-1.000000000000005
1.000000000000000
-1.000000000000000
clc
help var
var Variance.
For vectors, Y = var(X) returns the variance of the values in X. For
matrices, Y is a row vector containing the variance of each column of
X. For N-D arrays, var operates along the first non-singleton
dimension of X.
var normalizes Y by N-1 if N>1, where N is the sample size. This is
an unbiased estimator of the variance of the population from which X is
drawn, as long as X consists of independent, identically distributed
samples. For N=1, Y is normalized by N.
Y = var(X,1) normalizes by N and produces the second moment of the
sample about its mean. var(X,0) is the same as var(X).
Y = var(X,W) computes the variance using the weight vector W. W
typically contains either counts or inverse variances. The length of W
must equal the length of the dimension over which var operates, and its
elements must be nonnegative. If X(I) is assumed to have variance
proportional to 1/W(I), then Y * MEAN(W)/W(I) is an estimate of the
variance of X(I). In other words, Y * MEAN(W) is an estimate of
variance for an observation given weight 1.
Y = var(X,0,'all') or Y = var(X,1,'all') returns the variance of all
elements of X. A weight of 0 normalizes by N-1 and a weight of 1
normalizes by N.
Y = var(X,W,DIM) takes the variance along the dimension DIM of X.
Y = var(X,0,VECDIM) or Y = var(X,1,VECDIM) operates on the dimensions
specified in the vector VECDIM. A weight of 0 normalizes by N-1 and a
weight of 1 normalizes by N. For example, var(X,0,[1 2]) operates on
the elements contained in the first and second dimensions of X.
The variance is the square of the standard deviation (STD).
var(...,NANFLAG) specifies how NaN (Not-A-Number) values are treated.
The default is 'includenan':
'includenan' - the variance of a vector containing NaN values
is also NaN.
'omitnan' - elements of X or W containing NaN values are ignored.
If all elements are NaN, the result is NaN.
Example:
X = [4 -2 1; 9 5 7]
var(X,0,1)
var(X,0,2)
Class support for inputs X, W:
float: double, single
See also mean, std, cov, corrcoef.
Reference page for var
Other functions named var
A=rand(10,4);
V=var(A)
V =
0.119613625289474 0.109457235310597 0.086136729256627 0.130080252189071
X=rand(10,1);
Y=rand(10,1);
V=var(X,Y)
V =
0.026223519097581
V=var(X',Y')
V =
0.026223519097581
A=rand(10,4);
A=A'*A
A =
3.495385931891446 2.798559415830879 2.969735783168923 2.305665676072355
2.798559415830879 3.185054810974127 2.910934413185069 2.009437886940056
2.969735783168923 2.910934413185069 3.990239074078911 2.684742913541900
2.305665676072355 2.009437886940056 2.684742913541900 2.444444952254683
format short
A
A =
3.4954 2.7986 2.9697 2.3057
2.7986 3.1851 2.9109 2.0094
2.9697 2.9109 3.9902 2.6847
2.3057 2.0094 2.6847 2.4444
[V,D]=eig(A)
V =
0.3572 0.6272 -0.4594 0.5176
-0.4295 -0.5456 -0.5286 0.4883
0.5231 -0.3946 0.5030 0.5636
-0.6437 0.3914 0.5065 0.4194
D =
0.3241 0 0 0
0 0.6315 0 0
0 0 0.9221 0
0 0 0 11.2374
B=V*sqrt(D)*V'
B =
1.4862 0.7212 0.6658 0.5685
0.7212 1.4092 0.7105 0.4172
0.6658 0.7105 1.5874 0.7227
0.5685 0.4172 0.7227 1.1937
B*B-A
ans =
1.0e-14 *
0.0444 0.1776 0.1776 0.1332
0.1776 0.3109 0.2665 0.1776
0.1776 0.2665 0.3553 0.2220
0.0888 0.1776 0.1776 0.3997
A=[1 1 1 1 1;1:5];
AA=A'*A
AA =
2 3 4 5 6
3 5 7 9 11
4 7 10 13 16
5 9 13 17 21
6 11 16 21 26
[V,D]=eig(AA)
V =
0.4554 0.3725 0.2319 -0.7579 0.1600
-0.1251 -0.8067 -0.1832 -0.4675 0.2853
-0.8082 0.3285 0.1971 -0.1772 0.4106
0.1701 0.2730 -0.7724 0.1131 0.5359
0.3078 -0.1673 0.5265 0.4035 0.6612
D =
-0.0000 0 0 0 0
0 -0.0000 0 0 0
0 0 0.0000 0 0
0 0 0 0.8452 0
0 0 0 0 59.1548
B=V*sqrt(D)*V'
B =
Columns 1 through 4
0.7250 + 0.0000i 0.6769 - 0.0000i 0.6288 - 0.0000i 0.5807 + 0.0000i
0.6769 - 0.0000i 0.8270 + 0.0000i 0.9772 - 0.0000i 1.1273 - 0.0000i
0.6288 - 0.0000i 0.9772 - 0.0000i 1.3256 + 0.0000i 1.6740 - 0.0000i
0.5807 + 0.0000i 1.1273 - 0.0000i 1.6740 - 0.0000i 2.2207 + 0.0000i
0.5326 + 0.0000i 1.2775 + 0.0000i 2.0224 - 0.0000i 2.7673 + 0.0000i
Column 5
0.5326 + 0.0000i
1.2775 + 0.0000i
2.0224 - 0.0000i
2.7673 + 0.0000i
3.5123 + 0.0000i
D(1,1)
ans =
-1.4134e-15
D(2,2)
ans =
-2.5184e-16
D(1,1)=0;
D(2,2)=0;
D(3,3)=0;
D
D =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0.8452 0
0 0 0 0 59.1548
B=V*sqrt(D)*V'
B =
0.7250 0.6769 0.6288 0.5807 0.5326
0.6769 0.8270 0.9772 1.1273 1.2775
0.6288 0.9772 1.3256 1.6740 2.0224
0.5807 1.1273 1.6740 2.2207 2.7673
0.5326 1.2775 2.0224 2.7673 3.5123
B*B-A
{Matrix dimensions must agree.}
B*B-AA
ans =
1.0e-13 *
0.0266 0.0355 0.0444 0.0888 0.0533
0.0355 0.0533 0.0799 0.1243 0.1066
0.0533 0.0888 0.1421 0.1776 0.1776
0.0888 0.1421 0.1954 0.2842 0.2132
0.0533 0.1066 0.2132 0.2132 0.2132
help sqrtm
sqrtm Matrix square root.
X = sqrtm(A) is the principal square root of the square matrix A.
That is, X*X = A.
X is the unique square root for which every eigenvalue has nonnegative
real part. If A has any real, negative eigenvalues then a complex
result is produced. If A is singular then A may not have a
square root. A warning is printed if exact singularity is detected.
[X, RESNORM] = sqrtm(A) does not print any warning, and returns the
residual, norm(A-X^2,1)/norm(A,1).
[X, ALPHA, CONDX] = sqrtm(A) returns a stability factor ALPHA and an
estimate CONDX of the matrix square root condition number of X, in
the 1-norm. The residual norm(A-X^2,1)/norm(A,1) is bounded
approximately by N*ALPHA*EPS and the 1-norm relative error in X is
bounded approximately by N*ALPHA*CONDX*EPS, where N is the size of
the matrix.
See also expm, logm, funm.
Reference page for sqrtm
Other functions named sqrtm
AA
AA =
2 3 4 5 6
3 5 7 9 11
4 7 10 13 16
5 9 13 17 21
6 11 16 21 26
B1=sqrtm(AA)
B1 =
Columns 1 through 4
0.7250 + 0.0000i 0.6769 - 0.0000i 0.6288 - 0.0000i 0.5807 + 0.0000i
0.6769 - 0.0000i 0.8270 + 0.0000i 0.9772 - 0.0000i 1.1273 - 0.0000i
0.6288 - 0.0000i 0.9772 - 0.0000i 1.3256 + 0.0000i 1.6740 - 0.0000i
0.5807 + 0.0000i 1.1273 - 0.0000i 1.6740 - 0.0000i 2.2207 + 0.0000i
0.5326 + 0.0000i 1.2775 + 0.0000i 2.0224 - 0.0000i 2.7673 + 0.0000i
Column 5
0.5326 + 0.0000i
1.2775 + 0.0000i
2.0224 - 0.0000i
2.7673 + 0.0000i
3.5123 + 0.0000i
format long
B1
B1 =
Columns 1 through 2
0.724996307143256 + 0.000000010000682i 0.676891130716175 - 0.000000006911824i
0.676891130716175 - 0.000000006911824i 0.827042785460437 + 0.000000010916425i
0.628785963384378 - 0.000000011896653i 0.977194433021672 - 0.000000000403653i
0.580680780618533 + 0.000000004526051i 1.127346092771996 - 0.000000004294674i
0.532575623792015 + 0.000000004281744i 1.277497732036653 + 0.000000000693726i
Columns 3 through 4
0.628785963384378 - 0.000000011896653i 0.580680780618533 + 0.000000004526051i
0.977194433021672 - 0.000000000403653i 1.127346092771996 - 0.000000004294674i
1.325602910386535 + 0.000000026271339i 1.674011374638263 - 0.000000003745109i
1.674011374638263 - 0.000000003745109i 2.220676707899748 + 0.000000002270034i
2.022419860928661 - 0.000000010225925i 2.767341954783550 + 0.000000001243697i
Column 5
0.532575623792015 + 0.000000004281744i
1.277497732036653 + 0.000000000693726i
2.022419860928661 - 0.000000010225925i
2.767341954783550 + 0.000000001243697i
3.512264107523782 + 0.000000004006758i
[V,D]=eig(AA);
B=V*sqrt(D)*V'
B =
Columns 1 through 2
0.724996307143256 + 0.000000010000682i 0.676891130716175 - 0.000000006911824i
0.676891130716175 - 0.000000006911824i 0.827042785460437 + 0.000000010916425i
0.628785963384378 - 0.000000011896653i 0.977194433021672 - 0.000000000403653i
0.580680780618533 + 0.000000004526051i 1.127346092771996 - 0.000000004294674i
0.532575623792015 + 0.000000004281744i 1.277497732036653 + 0.000000000693726i
Columns 3 through 4
0.628785963384378 - 0.000000011896653i 0.580680780618533 + 0.000000004526051i
0.977194433021672 - 0.000000000403653i 1.127346092771996 - 0.000000004294674i
1.325602910386535 + 0.000000026271339i 1.674011374638263 - 0.000000003745109i
1.674011374638263 - 0.000000003745109i 2.220676707899748 + 0.000000002270034i
2.022419860928661 - 0.000000010225925i 2.767341954783550 + 0.000000001243697i
Column 5
0.532575623792015 + 0.000000004281744i
1.277497732036653 + 0.000000000693726i
2.022419860928661 - 0.000000010225925i
2.767341954783550 + 0.000000001243697i
3.512264107523782 + 0.000000004006758i
eig(AA)
ans =
-0.000000000000001
-0.000000000000000
0.000000000000003
0.845240525773498
59.154759474226509
clc
A=[1 3;2 1];
[V,D]=eig(A)
V =
0.774596669241483 -0.774596669241483
0.632455532033676 0.632455532033676
D =
3.449489742783178 0
0 -1.449489742783178
B=V*exp(D)*inv(V)
B =
14.859506558059788 19.136413933893575
12.757609289262383 16.859506558059788
expm(A)
ans =
15.859506558059815 19.136413933893607
12.757609289262406 15.859506558059815
help expm
expm Matrix exponential.
expm(A) is the matrix exponential of A and is computed using
a scaling and squaring algorithm with a Pade approximation.
Although it is not computed this way, if A has a full set
of eigenvectors V with corresponding eigenvalues D then
[V,D] = EIG(A) and expm(A) = V*diag(exp(diag(D)))/V.
EXP(A) computes the exponential of A element-by-element.
See also logm, sqrtm, funm.
Reference page for expm
Other functions named expm
exp(D)
ans =
31.484323106301588 1.000000000000000
1.000000000000000 0.234690009817989
B=V*diag(exp(diag(D)))*inv(V)
B =
15.859506558059788 19.136413933893575
12.757609289262383 15.859506558059788
expm(A)
ans =
15.859506558059815 19.136413933893607
12.757609289262406 15.859506558059815
B=V*diag(exp(diag(D)))/V
B =
15.859506558059785 19.136413933893575
12.757609289262382 15.859506558059790
A
A =
1 3
2 1
B=V*diag(sin(diag(D)))/V
B =
-0.647853332621885 0.422289633333561
0.281526422222374 -0.647853332621885
B=V*diag(cos(diag(D)))/V
B =
-0.415981841084960 -0.657677136995375
-0.438451424663583 -0.415981841084960
[V,D]=diag(A+eye(2))
{Error using diag
Too many output arguments.}
A1=A+eye(2)
A1 =
2 3
2 2
[V,D]=eig(A+eye(2))
V =
0.774596669241483 -0.774596669241483
0.632455532033676 0.632455532033676
D =
4.449489742783179 0
0 -0.449489742783178
g=gamma(D)
g =
10.847156348521503 Inf
Inf -3.592282741123797
g=diag(gamma(diag(D)))
g =
10.847156348521503 0
0 -3.592282741123797
G=V*g*inv(V)
G =
3.627436803698854 8.842314485407156
5.894876323604773 3.627436803698853
A
A =
1 3
2 1
[V,D]=eig(A)
V =
0.774596669241483 -0.774596669241483
0.632455532033676 0.632455532033676
D =
3.449489742783178 0
0 -1.449489742783178
ID=diag(1./diag(D))
ID =
0.289897948556636 0
0 -0.689897948556636
B=V*ID(inv(V)
B=V*ID(inv(V)
↑
{Error: Invalid expression. When calling a function or indexing a variable, use
parentheses. Otherwise, check for mismatched delimiters.
}
B=V*ID*inv(V)
B =
-0.200000000000000 0.600000000000000
0.400000000000000 -0.200000000000000
inv(A)
ans =
-0.200000000000000 0.600000000000000
0.400000000000000 -0.200000000000000
A=[0 -1;4 4]
A =
0 -1
4 4
[V,D]=eig(A)
V =
-0.447213595499958 -0.447213595499958
0.894427190999916 0.894427190999916
D =
2.000000000000000 0
0 2.000000000000000
V=[1 -1;-2 1];
VI=-[1 1;2 1];
J=[2 1; 0 2];
V*J*VI
ans =
0 -1
4 4
EJ=[exp(2) exp(2);0 exp(2)]
EJ =
7.389056098930650 7.389056098930650
0 7.389056098930650
B=V*EJ*VI
B =
-7.389056098930650 -7.389056098930650
29.556224395722602 22.167168296791949
A
A =
0 -1
4 4
expm(A)
ans =
-7.389056098930650 -7.389056098930650
29.556224395722602 22.167168296791953
SJ=[sin(2) cos(2);0 sin(2)]
SJ =
0.909297426825682 -0.416146836547142
0 0.909297426825682
B=V*SJ*VI
B =
1.741591099919966 0.416146836547142
-1.664587346188570 0.077003753731397
LJ=[log(2) 1/2;0 log(2)]
LJ =
0.693147180559945 0.500000000000000
0 0.693147180559945
B=V*LJ*VI
B =
-0.306852819440055 -0.500000000000000
2.000000000000000 1.693147180559945
logm(A)
ans =
-0.306852819440055 -0.500000000000000
2.000000000000000 1.693147180559945
inv(J)
ans =
0.500000000000000 -0.250000000000000
0 0.500000000000000
IJ=[0.5 -0.25;0 0.5]
IJ =
0.500000000000000 -0.250000000000000
0 0.500000000000000
B=V*IJ*VI
B =
1.000000000000000 0.250000000000000
-1.000000000000000 0
imv(A)
{Undefined function or variable 'imv'.}
inv(A)
ans =
1.000000000000000 0.250000000000000
-1.000000000000000 0
diary off