



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Introductory examples and solutions for matrix calculations using matlab, including verifying matrix properties such as symmetry and determinant. It also includes practice problems for calculating transposes, cross products, and matrix multiplication.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Answers: Prob 1:
A=[2 4 1;4 8 2;1 2 3] A = 2 4 1 (^41 82 )
B=[4 6 3;6 1 2;3 2 8] B = (^46 61 ) 3 2 8 D=A*B D = (^3570 1836 ) 25 14 31 % A and B are both symmetric matrices, where their product, which is D,% is not a symmetric matrix.
Prob 2:
% Using the same matrices A and B from the problem 1.1;
D=B'AB D = (^635330 330172 ) 445 238 402 % The matrix D=B'AB is symmetric.
Prob 3:
B=[0 0;0 0];>> A=[1 2;3 2]; C=[4 4;5 2]; AB ans = 0 0 0 0 CB ans = (^00 )
% Here, AB=CB, although A and C are different matrices.
Prob 4:
A=[5 7;3 2];>> B=[1 3;4 4]; D=A*B; A = (^53 )
B = (^14 )
D = 33 11 4317
D' ans =
% B= any matrix of size m x n % Output variable: % A= a matrix of size n x m [m n]=size(B); for i=1:mfor j=1:n
end A(j,i)=B(i,j); end
% Now we are going to check the function where we let it operate on a % random matrix B of size 4x6. % >> B=rand(4,6) % B = %% 0.95010.2311 0.89130.7621 0.82140.4447 0.92180.7382 0.93550.9169 0.05790. % 0.6068 0.4565 0.6154 0.1763 0.4103 0. % 0.4860 0.0185 0.7919 0.4057 0.8936 0. % >> [A]=newtransp(B) % A = %% 0.95010.8913 0.23110.7621 0.60680.4565 0.48600. % 0.8214 0.4447 0.6154 0. %% 0.92180.9355 0.73820.9169 0.17630.4103 0.40570. % 0.0579 0.3529 0.8132 0.
% You can see that the output matrix A is the transpose of the % input matrix B. The function operates correctly.
function [w]=crossprod(u,v) % Input variables % Two vectors u=(u1,u2,u3) and v=(v1,v2,v3) % Output variable is the vector w=(w1,w2,w3), which is the cross % product of the vectors u,v. w(1,1)=u(2,1)v(3,1)-u(3,1)v(2,1); w(2,1)=u(3,1)v(1,1)-u(1,1)v(3,1);w(3,1)=u(1,1)v(2,1)-u(2,1)v(1,1);
% Now we want to check the function by taking cross products of pairs% of unit vectors.
%% >> i=[1;0;0];>> j=[0;1;0]; % >> k=[0;0;1];
% >> [w]=crossprod(i,j) % w = %% (^00) % 1 % The output vector w is equal to the unit vector k=[0;0;1], which % shows that the function works correctly.
% Similarly; % >> [w]=crossprod(j,k) % w = % 1 %% (^00)
% >> [w]=crossprod(i,k) % w = % 0 %% -1 0
% Now we call the function;
[P]=matmul(K,d) P = 148 129 142 56 flops ans = 24
% For checking purposes, we simply calculate the product of our two % matrices K and d.
K*d ans = (^148142 )
flops ans = 24 % Multiplying a 2x3 matrix K with a 3x2 matrix d resulted in a 2x2% matrix P, as expected.
% The result we get using the program we wrote and the result we get by% simply multiplying the two matrices K and d are the same. So the % program works correctly and also in a most efficient way by % performing 24 flops, which is exactly the same number of flops the% MATLAB built-in arithmetic operation uses to perform the same % calculation.