MATLAB Project Subject: Linear Algebra: 3. Solutions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

HCMC University of Technology MATLAB Project

Department of Applied Mathematics Subject: Linear Algebra

1. Introduction
Currently, science is growing, with this development strength, the application of science and
scientific patents in schools is very feasible and significant. Since the first year, Ho Chi Minh
City University of Technology lecturers have helped technical students get acquainted with
programming applications such as MATLAB. MATLAB is a rising and programming
environment that allows calculating numbers with matrices, graphing functions or charting
information, implementing algorithms, creating user interfaces, and linking to computer
programs written in many other programming languages. MATLAB enables simulation of
calculations with the Toolbox library, experimenting with many models in practice and
techniques. With more than 40 years of establishment and development, MATLAB is an
effective calculation tool to solve technical problems today with a relatively simple and universal
design. Therefore, for the problems in Algebra, especially matrix problems, we can use
MATLAB's computing applications to solve most simply and understandably, helping us get
acquainted and add more skills to use programs application for students.
2. Group information
Student name Student ID

3. Solutions:

4. MATLAB code:
Question 1:
z =(1+i*sqrt(3))/(1+i)
z =1.3660 + 0.3660i
argument=angle(z)
module=abs(z)
splh=z'

Question 2:
z=(1+i*sqrt(3))*(1-i);
argument=angle(z)
module=abs(z)
splh=z'
Question 3:
z=(-1+i*sqrt(3))/(1-i);
argument=angle(z)
module=abs(z)
splh=z'

Question 4:
syms x y

[x,y] = solve('x^2 - y^2 - x= 0','2*x*y + y = 0');


disp('The solution to the complex equation is ');

for i = 1:length(x)

xval = double(x(i));
yval = double(y(i));
var = ['Z = ',num2str(xval),'+(',num2str(yval),')i'];
disp(var);

end

Question 5:
syms x y

[x,y] = solve('x^2 - y^2 = 0','2*x*y - 2*y = 0');


disp('The solution to the complex equation is ');

for i = 1:length(x)

xval = double(x(i));
yval = double(y(i));
var = ['Z = ',num2str(xval),'+(',num2str(yval),')i'];
disp(var);

end

Question 6:
A=[2 -1 4 5;2 1 3 -1];
b=[1 2 0 -1;-1 3 0 -1];
C=A'*b
tracec=trace(C)
rankc=rank(C)
determinantc=det(C)

Question 7:
>> A=[0 2 -4;-1 -4 5;3 1 7;0 5 10];
>> rank(A)

ans = 3

>> rank(A*A')

ans = 3

>> rank(A'*A)

ans = 3

Question 8:
A=[1 2 1;-1 1 -2];
B=[-1 2;0 2;-1 1];
C=[2 1 0;-1 1 1;0 2 -1];
D=2*A*C-B'*C';

Question 9:
syms m
A=[1 1 1 1;2 3 -1 4;-1 1 0 2;2 2 3 m];
m~=solve(det(A))

Question 10:
inv([1 0 2;0 1 0]*[1 0;1 1;0 1])

Question 11:
clc;clear;
%declare array A
A = [2 1 1;3 1 2;1 -1 0];
%compute f(A) using the formula
f_A = A^2 -(2*A) -3

Question 12:
A=[3 -2 6;5 1 4;3 1 1];
syms m
B=[1 1 -1;0 2 5;1 -2 m];
m~=solve(det(A*B))

Question 13:
A=[-1 3 2;2 1 0;4 3 1];
P_A=det(A)*inv(A)

Question 14:
syms m
A=[1 2 1;2 3 m;3 2 -1];
B=[1 1 1;2 3 2;5 7 5];
C=det(B)
D=det(A)
E=solve(C*D)
m~=E

Question 15:
A=[2 3 1;3 4 2;5 3 -1];
P_A=det(A)*inv(A)
Question 16:
>>A=[1 1 2 1;2 3 4 5;3 4 6 9]

A =

1 1 2 1
2 3 4 5
3 4 6 9

>>A(2,:)=A(2,:)-2*A(1,:)

A =

1 1 2 1
0 1 0 3
3 4 6 9

>>A(3,:)=A(3,:)-3*A(1,:)

A =

1 1 2 1
0 1 0 3
0 1 0 6

>>A(3,:)=A(3,:)-A(2,:)

A =

1 1 2 1
0 1 0 3
0 0 0 3

Question 17:
A=[5 6,7 8]
B=[3 -1,5 -2]
X=A\B

Question 18:
A=[0 -8 3,1 -5 9,2 3 8]
B=[23 -30,-2 -26,-16 7]
X=B\A

Question 19:
A=[1 2 3 4;2 1 2 3;3 2 1 2;4 3 0 4]
B=[7;6;7;8];
%Rank of A
C=rank(A)
%The number of solutions of the system
D=rank([A B])
Question 20:
A=[1 2 -3 5;1 3 -13 22;3 5 1 -2;2 3 4 -7];
B=[1;-1;5;4];
C=rank(A)
D=rank([A B])
if C==D & D==4
disp('The system has a unique solution')
elseif C==D & D<4
disp('The system has infinite solution')
else C<D
disp('The system has no solution')
end
Question 21:
A=[1 -2 3 -4;3 3 -5 1;-2 1 2 -3;3 0 3 -10];
B=[2;-3;5;8];
C=rank(A)
D=rank([A B])
if C==D & D==4
disp('The system has a unique solution')
elseif C==D & D<4
disp('The system has infinite solution')
else C<D
disp('The system has no solution')
end

Question 22:
syms m
A=[1 1 1 1;2 1 3 -1;3 4 2 0;-2 -1 0 m];
B=[1;2;6;m-1];
C=rank(A)
D=rank([A B])
E=solve(C-D,m)
if E==0 & D==4
disp('All values of m give the system having a unique solution')
else
m==E
end

Question 23:
A=[1 3 3 2 4;1 4 5 3 7;2 5 4 1 5;1 5 7 6 10];
null(A,'r')

Question 24:
syms m
M=[1 1 1 0;1 2 1 1;2 0 m -1];
rank(M)

Question 25:
V=[1 2 1 -1;3 1 0 5;0 5 3 -8];
dim=ndims(V)
rref(V)
basis=ans(1:2,:)

Question 26:
>>syms m
>>V=[1 2 1 1;2 -1 1 3;5 5 4 m];
>>V(2,:)=V(2,:)-2*V(1,:)

V =

[ 1, 2, 1, 1]
[ 0, -5, -1, 1]
[ 5, 5, 4, m]

>>V(3,:)=V(3,:)-5*V(1,:)

V =

[ 1, 2, 1, 1]
[ 0, -5, -1, 1]
[ 0, -5, -1, m - 5]

>>V(3,:)=V(3,:)-V(2,:)

V =

[ 1, 2, 1, 1]
[ 0, -5, -1, 1]
[ 0, 0, 0, m - 6]

>>m~=6

ans =

m ~= 6

>>basis=subs(V,m,6)

basis =

[ 1, 2, 1, 1]
[ 0, -5, -1, 1]
[ 0, 0, 0, 0]

Question 27:
V=[1 1 -1 0;2 0 -1 -1];
dimension=ndims(V)
basis=(null(V,'r'))'

Question 28:
E=[1 1 1;1 1 2;1 2 1];
xE=[1 -3 2];
x=E'*xE'

Question 29:
E=[1 1 1;1 1 0;1 0 1];
x=[1 2 -1];
xE=inv(E')*x'

Question 30:
syms m
M=[1 2 -1;2 1 3;-1 2 m];
m~=solve(det(M))

Question 31:
syms m
M=[1 -2 1;3 1 -1;m 0 1];
m~=solve(det(M))

Question 32:
Question 33:
E=[1 0 1;1 1 1;1 1 0];
F=[1 1 2;1 2 1;1 1 1];
the_change_of_basis_from_E_to_F=inv(E)*F

Question 34:
syms m
M=[1 1 1;2 3 1]'
x=[1 0 m]'
conj(m)
inv(M(1:2,1:2))*x(1:2)
m=[1 1]*ans

Question 35:
>> F=[1 1 -1 -1;1 -1 3 -1]

F =

1 1 -1 -1
1 -1 3 -1

>> syms m
>> G=[2 -1 0 m]'

G =

2
-1
0
conj(m)
>> A=null(F,'r')
A =

-1 1
2 0
1 0
0 1
>> rank(A(1:3,:))

ans =

2
>> rank([A(1:3,:) G(1:3)])

ans =

3
>> %m does not exist
Question 36:
V=[1 -1 1 0;0 1 1 1]
one_basis_of_V=null(V,'r')

Question 37:
>>V1=[8 -6 1 0;-7 5 0 1];
>>V2=[1 0 -8 7;0 1 6 -5];
>>V1(1,:)*V2'

ans =

0 0
>>V1(2,:)*V2'

ans =

0 0
>>if V2(2,:)*V1(1,:)' == 0
disp('V1 is perpendicular to V2')
else disp('V1 is not perpendicular to V2')
end
V1 is perpendicular to V2

Question 38:
>>V1=[-2 0 -6 5;1 1 -1 0];
>>syms m
>>V2=[2 -1 1 2;-1 3 2 m];
>>V2(1,:)*V1'

ans =

[ 0, 0]
>>V2(2,:)*V1(2,:)'

ans =

0
>>m=solve(V2(2,:)*V1(1,:)')

m =

Question 39:
u=[1 1 2];
v=[2 1 -1];
cos_u_v=dot(u,v)/(norm(u)*norm(v))
Question 40:
u=[1 1 2];
v=[2 1 -1];
d=norm(u-v)
w=cross(u,v)

Question 41, 42 & 43:


A=[2 -3 0;-3 5 -1;0 -1 4];
u=[1 2 1];
v=[-1 1 2];
w=u-v;
distance=sqrt(w*A*w')
cos_u_v=(u*A*v')/(norm(u)*norm(v))
F=[1 2 1];
Basis_required=null([F*A],'r')

Question 44:
f=[2 1 -3;1 -4 0];
basis=null(f,'r')
dimension=size(basis',1)

Question 45:
f=[1 1 0;0 1 1;1 0 -1];
imf=rref(f')
dimension=ndims(imf)
basis=imf(1:2,:)

Question 46:
f1=[1 1 1;1 1 0;1 0 1];
f2=[1 2;2 -1;-1 1];
f=[2 0 3]';
f_required=(inv(f1')*f)'*f2
Question 47:
E=[1 1 1;1 0 1;1 1 0];
F=[1 1;2 1];
A=[2 1 -3;0 3 4];
f=[1;2;3];
f_required=F'*A*inv(E)*f

Question 48:
E=[1 1 0;0 1 1;1 0 1];
f=[1;2;3];
x=E\f

Question 49:
>> A=[1 6;5 2];
>> u=[6;-5];
>> v=[3;-2];
>> eigenvalue1=unique((A*u)./u)
eigenvalue1 =
-4
% u is eigenvector of A
>> eigenvalue2=unique((A*v)./v)
eigenvalue2 =
-5.5000
-3.0000
% v is not eigenvector of A

Question 50:
>> A=[3 4;6 5];
>> null(A+eye(2),'r')
ans =
-1
1
% -1 is an eigenvector of A
>> null(A-3*eye(2),'r')
ans =
Empty matrix: 2-by-0
% 3 is not an eigenvector of A

Question 51:
A=[3 1 1;2 4 2;1 1 3];
syms a
eigenvalues=unique(solve(det(A-a*eye(3))))
eigenvector1=null(A-2*eye(3),'r')
eigenvector2=null(A-6*eye(3),'r')

Question 52:
>> syms a m

>> A=[0 -8 6;-1 -8 7;1 -14 m];

>> m=solve(det(A-2*eye(3)))

m =

11

>> A=[0 -8 6;-1 -8 7;1 -14 11];

>> eigenvalues=unique(solve(det(A-a*eye(3))))

>> eigenvector1=null(A-2*eye(3),'r')

>> eigenvector2=null(A+2*eye(3),'r')

>> eigenvector3=null(A-3*eye(3),'r')

Question 53:
A=[1 2 3;2 5 4;3 7 8];
rref([A eye(3)])
inverseA=ans(:,4:6)

Question 54:
M=[1 2 -1;3 2 -1;0 2 -1];
syms m
u=[3;8;m];
M=(rref(M))'
x=M(1:2,1:2)\u(1:2)
m=M(3,1:2)*x

Question 55:
V=[1 2 -1;3 2 -1;0 2 -1];
syms m
u=[-3;5;m];
V=(rref(V))'
x=V(1:2,1:2)\u(1:2)
m=V(3,1:2)*x

Question 56:
>> U=[1 2 1 1;2 1 0 -2];
>> syms m
>> V=[1 5 3 5;3 0 -1 m];
>> A=[U;V];
>> A(2,:)=A(2,:)-2*A(1,:)
A =
[ 1, 2, 1, 1]
[ 0, -3, -2, -4]
[ 1, 5, 3, 5]
[ 3, 0, -1, m]
>> A(3,:)=A(3,:)-A(1,:)
A =
[ 1, 2, 1, 1]
[ 0, -3, -2, -4]
[ 0, 3, 2, 4]
[ 3, 0, -1, m]
>> A(4,:)=A(4,:)-3*A(1,:)
A =
[ 1, 2, 1, 1]
[ 0, -3, -2, -4]
[ 0, 3, 2, 4]
[ 0, -6, -4, m - 3]
>> A(3,:)=A(3,:)+A(2,:)
A =
[ 1, 2, 1, 1]
[ 0, -3, -2, -4]
[ 0, 0, 0, 0]
[ 0, -6, -4, m - 3]
>> A(4,:)=A(4,:)-2*A(2,:)
A =
[ 1, 2, 1, 1]
[ 0, -3, -2, -4]
[ 0, 0, 0, 0]
[ 0, 0, 0, m + 5]
>> %m = 5 is the result

Question 57:
>> syms m
>> V=[1 1 -1 0;2 2 1 1;1 1 2 m];
>> V(2,:)=V(2,:)-2*V(1,:)
V=
[ 1, 1, -1, 0]
[ 0, 0, 3, 1]
[ 1, 1, 2, m]
>> V(3,:)=V(3,:)-V(1,:)
V=
[ 1, 1, -1, 0]
[ 0, 0, 3, 1]
[ 0, 0, 3, m]
>> V(3,:)=V(3,:)-V(2,:)
V =
[ 1, 1, -1, 0]
[ 0, 0, 3, 1]
[ 0, 0, 0, m - 1] % m~=1 such that dim(V) max
>> V=subs(V,m,1)
>> basis=V(1:2,:)
basis =
[ 1, 1, -1, 0]
[ 0, 0, 3, 1]
>> dimension=size(coso,1)
dimension = 2

Question 58:
U=[1 2 1 0;2 -1 1 1];
syms m
V=[1 1 -2 1;2 0 4 m];
solve(det([U;V]))
W=rref(subs([U;V],m,0))
basis=W(1:3,:)
dimension=size(basis,1)

Question 59:
>> U=[1 1 2 0;-1 1 -1 2];
>> syms m
>> V=[1 2 2 2;-1 0 -1 m];
>> A=[U;V];
>> A(2,:)=A(2,:)+A(1,:)
A =
[ 1, 1, 2, 0]
[ 0, 2, 1, 2]
[ 1, 2, 2, 2]
[ -1, 0, -1, m]
>> A(3,:)=A(3,:)-A(1,:)
A =
[ 1, 1, 2, 0]
[ 0, 2, 1, 2]
[ 0, 1, 0, 2]
[ -1, 0, -1, m]
>> A(4,:)=A(4,:)+A(1,:)
A =
[ 1, 1, 2, 0]
[ 0, 2, 1, 2]
[ 0, 1, 0, 2]
[ 0, 1, 1, m]
>> A(3,:)=2*A(3,:)-A(2,:)
A =
[ 1, 1, 2, 0]
[ 0, 2, 1, 2]
[ 0, 0, -1, 2]
[ 0, 1, 1, m]
>> A(4,:)=2*A(4,:)-A(2,:)
A =
[ 1, 1, 2, 0]
[ 0, 2, 1, 2]
[ 0, 0, -1, 2]
[ 0, 0, 1, 2*m - 2]
>> A(4,:)=A(4,:)+A(3,:)
A =
[ 1, 1, 2, 0]
[ 0, 2, 1, 2]
[ 0, 0, -1, 2]
[ 0, 0, 0, 2*m]
>> %The dimension of U intersecting V is 1 along all the values of m
>> A=[1 1 2 0;0 2 1 2;0 0 -1 2];
>> basis=(null(A,'r'))'
basis =
-2 -2 2 1
Question 60:
V=[1 -1 1 0;0 1 1 1];

basis=null(V,'r')

Question 61:
>> V=[1 1 1 0;-1 1 0 1];
>> basis_required=rref(V)
basis_required =
1.0000 0 0.5000 -0.5000
0 1.0000 0.5000 0.5000
>> basis_required=2*rref(V)
basis_required =

2 0 1 -1
0 2 1 1

Question 62:
V=[2 -1 1 0;-2 1 0 1]';
x=[1 1 0 1]';
PrVx=V*inv(V'*V)*V'*x

Question 63:
>> V1=[1 2 1;-1 0 1];
>> syms m
>> V2=[1 -1 m]';
>> V1=(rref(V1))'
V1 =
1 0
0 1
-1 1
>> m=V1(3,:)*inv(V1(1:2,:))*V2(1:2,1)
m = -2
Question 64:
F=[1 1 2;2 1 -1]';
x=[1 2 3]';
PrFx=F*inv(F'*F)*F'*x

Question 65:
A=[1 0 -1;0 2 0;-1 0 3];
u=[1 1 2];
v=[2 1 -1];
w=u-v;
the_angle_u_v=acos((u*A*v')/(norm(u)*norm(v)))
distance=sqrt(w*A*w')

Question 66:
A=[1 0 -2;0 2 0;-2 0 5];
f=[1 2 3];
the_orthogonal_complement =null(f*A,'r')

Question 67:
R3=[1 1 0;1 1 1;1 0 1];
R2=[2 -1;1 2;-1 1];
A=rref([R3 R2])
syms x1 x2 x3
result=x1*A(1,4:5)+x2*A(2,4:5)+x3*A(3,4:5)

Question 68:
f=[1 2 -3;2 0 1];
E=[1 1 1;1 0 1;1 1 0];
F=[1 3;2 5];
A=E\f';
P=E\E;
Q=F\F;
AEF=P\A*Q

Question 69:
A=[1 1 1;1 1 2;1 2 1];
B=[1 2 1;2 1 -1;5 4 -1];
E=[1 1 0;0 1 1;1 1 1];
fE=(A\E)*B*(A\E)

Question 70:
E=[1 1 1;1 0 1;1 1 0];
F=[1 1;2 1];
A=[2 1 -3;0 3 4];
result=(E'*A'*inv(F'))'

Question 71:
E=[1 2 1;1 1 2;1 1 1];
A=[1 0 1;2 1 4;1 1 3];
result=E'*A*inv(E')

Question 72:
E=[1 2 1;1 1 2;1 1 1];
A=[1 0 1;2 1 4;1 1 3];
F=[1 2 3;2 3 5;5 8 4];
P=inv(E')*F';
AF=P\A*P

Question 73:
E=[1 1 1;1 0 1;1 1 0];
A=[1 1 -1;2 3 3;1 2 4];
Act=E'*A*inv(E')
imf =rref(Act')
basis=imf(1:2,:)
dimension=ndims(imf)

Question 74:
E=[1 1 1;1 0 1;1 1 0];
A=[1 1 -1;2 3 3;1 2 4];
Act=E'*A*inv(E')
ker=(null(Act,'r'))'
dimension=size(ker,1)

Question 75:
% ask user to enter rows and columns
m=input('Enter the rows: ');
n=input('Enter the columns: ');

% ask user to enter the elements of the martix


for i=1:m
for j=1:n
a(i,j)=input('Enter the Element(s): ');
end
end

% reshape a to a matrix
A=reshape(a,m,n);
evenEntries = 0;

% check for even entries


for i=1:m
for j=1:n
if mod(a(i,j),2)==0
evenEntries=evenEntries+1;
end
end
end

fprintf('Total even entries are: %d',evenEntries)

Question 76:
% ask user to enter rows and columns
m=input('Enter the rows: ');
n=input('Enter the columns: ');

% ask user to enter the elements of the martix


for i=1:m
for j=1:n
a(i,j)=input('Enter the Element(s): ');
end
end

% reshape a to a matrix
a=reshape(a,m,n);

% Find the maximum element in each row of A


disp('The maximum elements in each row are :')
max(a,[],2) % Find the maximum element in each row of A

Question 77:
% ask user to enter rows and columns
m=input('Enter the rows: ');
n=input('Enter the columns: ');

% ask user to enter the elements of the martix


for i=1:m
for j=1:n
a(i,j)=input('Enter the Element(s): ');
end
end

% reshape a to a matrix
a=reshape(a,m,n);
maxPositive = -1;

% check for all positive elements and find maximum


% consider 0 to be positive as well
for i=1:m
for j=1:n
if a(i,j)>maxPositive
maxPositive=a(i,j);
end
end
end

% print the required answer


if maxPositive<0
fprintf('There is no Positive Elements in Matrix')
else
fprintf('Max Positive Element is %d',maxPositive)
end

Question 78:
% ask user to enter rows and columns
m=input('Enter the rows: ');
n=input('Enter the columns: ');

% ask user to enter the elements of the martix


for i=1:m
for j=1:n
a(i,j)=input('Enter the Element(s): ');
end
end

% reshape a to a matrix
a=reshape(a,m,n);
sumOfOddEntries = 0;

% check for all odd elements


% and add them to a sum
for i=1:m
for j=1:n
if mod(a(i,j),2)==1
sumOfOddEntries=sumOfOddEntries+a(i,j);
end
end
end

% print the required answer


fprintf('Sum of Odd Elements is %d',sumOfOddEntries)
Question 79:
% ask user to enter rows and columns
m=input('Enter the rows: ');
n=input('Enter the columns: ');

% ask user to enter the elements of the martix


for i=1:m
for j=1:n
a(i,j)=input('Enter the Element(s): ');
end
end

% reshape a to a matrix
a=reshape(a,m,n);
product=1

% check for all odd elements

for i=1:m
for j=1:n
if mod(a(i,j),2)==1
product=product*a(i,j)
end
end
end

% print the required answer


disp(' The Product of all elements in a matrix is');
disp(product);

Question 80:
% ask user to enter rows and columns
m=input('Enter the rows: ');
n=input('Enter the columns: ');

% ask user to enter the elements of the martix


for i=1:m
for j=1:n
A(i,j)=input('Enter the Element(s): ');
end
end

% reshape a to a matrix
A=reshape(A,m,n);

if size(A,1)~=size(A,2)
disp('Matrix A is neither square nor symmetric');
else
disp('A is a square matrix');
if A==A'
disp('A is symmetric');
else
disp('A is not symmetric');
end
end

Question 81:
% ask user to enter rows and columns
m=input('Enter the rows: ')
n=input('Enter the columns: ')

% ask user to enter the elements of the martix


for i=1:m
for j=1:n
A(i,j)=input('Enter the Element(s): ')
end
end

% reshape A to a matrix
A=reshape(A,m,n);

% make B as a transpose of A
B=transpose(A)

% make identity matrix of size n


I = eye(n)

% if rows and columns are not equal then it cant be orthogonal


if n~=m
fprintf('Orthogonal Matrix exists for only Square Matrix')
% if matrix product of A and B forms identity matrix it is Orthogonal
elseif A*B==I
fprintf('The given matrix is Orthogonal')
% in all other cases it is not Orthogonal
else
fprintf('The given matrix is Not Orthogonal')
end

You might also like