0% found this document useful (0 votes)
61 views13 pages

Week 2

The document provides code to calculate the eigenvalues and eigenvectors of a matrix A, and use them to find the canonical form and powers of the matrix. It inputs different 3x3 matrices, calculates their eigenvalues and eigenvectors, and displays the canonical forms. It also includes code to visualize the eigenvectors and transformed vectors graphically for 2x2 and 3x3 matrices.

Uploaded by

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

Week 2

The document provides code to calculate the eigenvalues and eigenvectors of a matrix A, and use them to find the canonical form and powers of the matrix. It inputs different 3x3 matrices, calculates their eigenvalues and eigenvectors, and displays the canonical forms. It also includes code to visualize the eigenvectors and transformed vectors graphically for 2x2 and 3x3 matrices.

Uploaded by

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

clc

clear all

syms x1 x2 x3 y1 y2 y3 real

Q=3*x1^2+3*x2^2+3*x3^2-2*x2*x3+2*x1*x3+2*x1*x2

%Q=x1^2+3*x2^2+6*x3^2+2*x2*x3+4*x1*x3+2*x1*x2

%Q=3*x1^2+5*x2^2+3*x3^2-2*x2*x3+2*x1*x3-2*x1*x2

a11=(1/2)*diff(diff(Q,x1),x1)

a22=(1/2)*diff(diff(Q,x2),x2)

a33=(1/2)*diff(diff(Q,x3),x3)

a12=(1/2)*diff(diff(Q,x1),x2)

a21=a12

a13=(1/2)*diff(diff(Q,x1),x3)

a31=a13

a23=(1/2)*diff(diff(Q,x2),x3)

a32=a23

A=[a11 a12 a13;a21 a22 a23;a31 a32 a33]

A=double(A)

[N D]=eig(A)

D1=N'*A*N

Y=[y1 y2 y3];

disp('The canonical form is ')

cf=vpa(Y*D*Y',5)

input

Q=3*x1^2+3*x2^2+3*x3^2-2*x2*x3+2*x1*x3+2*x1*x2

Output

Q=

3*x1^2 + 2*x1*x2 + 2*x1*x3 + 3*x2^2 - 2*x2*x3 + 3*x3^2

a11 =
3

a22 =

a33 =

a12 =

a21 =

a13 =

a31 =

a23 =

-1

a32 =

-1

A=

[ 3, 1, 1]

[ 1, 3, -1]

[ 1, -1, 3]

A=

3 1 1

1 3 -1

1 -1 3
N=

-0.5774 0.1870 0.7948

0.5774 0.7818 0.2354

0.5774 -0.5948 0.5594

D=

1.0000 0 0

0 4.0000 0

0 0 4.0000

D1 =

1.0000 0.0000 -0.0000

0.0000 4.0000 -0.0000

-0.0000 -0.0000 4.0000

The canonical form is

cf =

y1^2 + 4.0*y2^2 + 4.0*y3^2

input

output

Q=

x1^2 + 2*x1*x2 + 4*x1*x3 + 3*x2^2 + 2*x2*x3 + 6*x3^2

a11 =

a22 =

a33 =

6
a12 =

a21 =

a13 =

a31 =

a23 =

a32 =

A=

[ 1, 1, 2]

[ 1, 3, 1]

[ 2, 1, 6]

A=

1 1 2

1 3 1

2 1 6

N=

-0.9323 -0.1203 0.3411

0.2293 -0.9259 0.3002

0.2797 0.3581 0.8908

D=
0.1540 0 0

0 2.7431 0

0 0 7.1029

D1 =

0.1540 0.0000 -0.0000

0.0000 2.7431 0.0000

-0.0000 0.0000 7.1029

The canonical form is

cf =

0.15397*y1^2 + 2.7431*y2^2 + 7.1029*y3^2

Input

Output

Q=

3*x1^2 - 2*x1*x2 + 2*x1*x3 + 5*x2^2 - 2*x2*x3 + 3*x3^2

a11 =

a22 =

a33 =

a12 =

-1

a21 =

-1

a13 =
1

a31 =

a23 =

-1

a32 =

-1

A=

[ 3, -1, 1]

[ -1, 5, -1]

[ 1, -1, 3]

A=

3 -1 1

-1 5 -1

1 -1 3

N=

0.7071 -0.5774 0.4082

-0.0000 -0.5774 -0.8165

-0.7071 -0.5774 0.4082

D=

2.0000 0 0

0 3.0000 0

0 0 6.0000

D1 =

2.0000 -0.0000 0.0000


0.0000 3.0000 0.0000

0.0000 0.0000 6.0000

The canonical form is

cf =

2.0*y1^2 + 3.0*y2^2 + 6.0*y3^2

Code:

clc
clear all
a=input('enter the matrix a:')
n=input('enter value of n:')
[x,d]=eig(a)
option=isequal(a,a')
if(option==0)
m=x
d=inv(m)*a*m
powerofthematrix=m*(d^n)*inv(m)
else
m1=x
d=(m1)'*a*(m1)
powerofthematrix=m1*(d^n)*m1'
end

output: problem1

enter the matrix a:[2 -2 2;1 1 1;1 3 -1]

a=

2 -2 2

1 1 1

1 3 -1

enter value of n:4

n=

x=
0.0000 -0.0000 -0.4924

-0.7071 -0.7071 -0.1231

-0.7071 -0.7071 0.8616

d=

2.0000 0 0

0 2.0000 0

0 0 -2.0000

option =

m=

0.0000 -0.0000 -0.4924

-0.7071 -0.7071 -0.1231

-0.7071 -0.7071 0.8616

d=

2.0000 0.0000 -0.0000

-0.0000 2.0000 0.0000

0.0000 0.0000 -2.0000

powerofthematrix =

16.0000 -0.0000 0.0000

30.4199 0.7901 15.2099

30.4199 -15.2099 31.2099

Output: problem 2

enter the matrix a:[2 0 4;0 6 0;4 0 2]

a=
2 0 4

0 6 0

4 0 2

enter value of n:5

n=

x=

0.7071 0.7071 0

0 0 -1.0000

-0.7071 0.7071 0

d=

-2 0 0

0 6 0

0 0 6

option =

m1 =

0.7071 0.7071 0

0 0 -1.0000

-0.7071 0.7071 0

d=

-2.0000 0.0000 0

0.0000 6.0000 0

0 0 6.0000

powerofthematrix =
1.0e+03 *

3.8720 0 3.9040

0 7.7760 0

3.9040 0 3.8720

VISUALISATION

CODE: for 3x3 matrix

clc
clear all
a=input('enter a 3x3 matrix');
[p,d]=eig(a)
o=zeros(3);
q=quiver3(o(1,:),o(2,:),o(3,:),p(1,:),p(2,:),p(3,:));
pause
hold on
ev=diag(d)'
lp=a*p
q=quiver(o(1,:),o(2,:),o(3,:),lp(1,:),lp(2,:),lp(3,:));

OUTPUT:

enter a 3x3 matrix[2 0 4; 0 6 0; 4 0 2]

a=

2 0 4

0 6 0

4 0 2

p=

0.7071 0.7071 0

0 0 -1.0000

-0.7071 0.7071 0

d=
-2 0 0

0 6 0

0 0 6

ev =

-2 6 6

lp =

-1.4142 4.2426 0

0 0 -6.0000

1.4142 4.2426 0

Pictorial output:

For 2x2 matrix:


code:

clc
clear all
a=input('enter a 2x2 matrix')
[p,d]=eig(a)
o=zeros(2);
q=quiver(o(1,:),o(2,:),p(1,:),p(2,:));
ev=diag(d)'
lp=a*p
q=quiver(o(1,:),o(2,:),lp(1,:),lp(2,:));

output:
enter a 2x2 matrix[2 3;0 -5]

a=

2 3

0 -5

p=

1.0000 -0.3939

0 0.9191

d=

2 0

0 -5

ev =

2 -5

lp =

2.0000 1.9696

0 -4.5957
Pictorial output:

You might also like