REALIZATION OF ARRAYS AND MATRICES

 

OBJECTIVE

·        To familiarize with the realization of arrays and matrixes and their visualization using plotting functions and GUI

 

LEARNING OUTCOMES

·   After the completion of this experiment students will be able to approximate an array/matrix with matrix decomposition.

SOFTWARE USED:

                 MATLAB® R2017a

1.  The voltage, v, across a resistance is given as (Ohm’s Law), v=iR , where i is the current and R the resistance. The power dissipated in resistor R is given by the expression

P = i2R.

If R = 10 Ohms and the current is increased from 0 to 10 A with increments of 2A, write a MATLAB program to generate a table of current, voltage and power dissipation.

PROGRAM

clc;

clear all;

close all;

R=10; % Resistance value

i=[0:2:10]; % Generate current values

v=i.*R; % array multiplication to obtain voltage

p=(i.^2)*R; % power calculation

sol=[i;v;p] % current, voltage and power values are printed

 

OUTPUT

sol =

           0           2           4           6           8          10

           0          20          40          60          80         100

           0          40         160         360         640        1000

2. Find the roots of the following quadratic equation

x2 -2x +3 = 0

PROGRAM

clc;

clear all;

close all;

a=input('enter  a');

b=input('enter  b');

c=input('enter  c');

i = b^2 - 4*a*c;

if i > 0

 srint = sqrt(i);

 x1= (-b + srint)/(2*a);

 x2= (-b - srint)/(2*a);

elseif i == 0

 x1= -b/(2*a);

 x2= x1;

elseif i < 0

 srint = sqrt(-i);

 p1 = -b/(2*a);

 p2 = srint/(2*a);

 x1 = p1+p2*j;

 x2 = p1-p2*j;

end

rt =[x1;x2];

OUTPUT

rt =

   1.0000 + 1.4142i

   1.0000 - 1.4142i

3.  Create two separate row vectors(arrays) a and b that contains elements from 1 to 10. Create an array of complex numbers z with a as the real part and b as the imaginary part. Find the sum and complex conjugate of the array z 

PROGRAM

clc;

clear all;

close all;

a = [1:10];

b = [1:10];

z = complex(a,b);

A = sum(z);

complex_conjugate = conj(z);

 

OUTPUT

A =55.0000 +55.0000i

complex_conjugate =

Columns 1 through 4

1.0000 - 1.0000i   2.0000 - 2.0000i   3.0000 - 3.0000i   4.0000 - 4.0000i

Columns 5 through 8

5.0000 - 5.0000i   6.0000 - 6.0000i   7.0000 - 7.0000i   8.0000 - 8.0000i

Columns 9 through 10

9.0000 - 9.0000i  10.0000 -10.0000i

4.  If w is a complex matrix given as

Find the sum of all the elements of the matrix, conjugate transpose and un-conjugate transpose of the matrix

PROGRAM

clc;

clear all;

close all;

c=[1+j 2-2*j; 3+2*j 4+3*j]

s=sum(c);

S1=sum(s);

trans = c.';

conjugate_trans=c';


OUTPUT

S1 =10.0000 + 4.0000i

trans =

   1.0000 + 1.0000i   3.0000 + 2.0000i

   2.0000 - 2.0000i   4.0000 + 3.0000i

conjugate_trans =

   1.0000 - 1.0000i   3.0000 - 2.0000i

   2.0000 + 2.0000i   4.0000 - 3.0000i

5. Create an image that consists of alternate rows of black and white pixels without using the inbuilt function ‘image’.

PROGRAM

clc;

clear all;

close all;

row = 126;

col = 126;

img = zeros(row, col);

i=[1:2:125];

img(i, :) = 0;

 

k=[2:2:126];

img(k, :) = 1;

 

figure;

imshow(img);     


OUTPUT


6. Create a matrix of order 256 x 256 with some random values in the range [1, 80]. Display the corresponding image on the screen with colorbar.

PROGRAM

C = randi(80,256,256);

image(C);

colormap('default')

colorbar

 

OUTPUT


7. Check whether a matrix inverse exists, and if exists, find the inverse.

PROGRAM

clc;

clear all;

close all;

A=input('enter the matrix');

%inverse of A exists only if matrix is square and non singular

[m n]= size(A);%check the no. of rows and columns

 

if m==n

    if det(A)==0 %matrix is singular

        disp('Inverse does not exist');

    else

        inv_A=inv(A);

        disp('Inverse is')

        disp(inv_A);

    end

else

    disp('Matrix is not a square matrix');

end

 

INPUT


[0 1 2;1 2 3; 3 1 1]


OUTPUT

Inverse is

    0.5000   -0.5000    0.5000

   -4.0000    3.0000   -1.0000

    2.5000   -1.5000    0.5000

8.   Solve the system of equations

(a) 2x-y+3z = 5; 4x+5z =12; x+y+2z = -3.

(b) x+y+z=6; x+2y+3z=10; 2x+4y+6z=20

(c) x1+3x2+2x3=0; 2x1-x2+3x3 =0; 3x1-5x2+4x3=0; x1+17x2+4x3 = 0

Verify your answer using ‘linsolve’ function

PROGRAM

clc;

clear all;

close all;

A=input('enter the A matrix');

B=input('enter the B matrix');

[m n]=size(A); %m rows and n columns

 

C=[A B];

if rank(A)==rank(C)

    if rank(A)==n %n is the no. of unknowns

       disp('The system has unique solution'); 

       z1=inv(A)*B

    else

        if rank(A)<n %n is the no. of unknowns

           disp('The system has infinite no.of solutions');

        end

    end

else

       disp('The system has no solution');

end

 

if B==0  %Homogeneous system of equations, AX=0

 disp('The trivial solution is');

    z1=0

end

       

OUTPUT

(a)

The system has unique solution z1 =

   10.0000

   -1.8000

   -5.6000

(b)

The system has infinite no. of solutions

(c) The system has infinite no.of solutions

The trivial solution is

z1 =0

9. Show that the sum of the eigen values is equal to the trace of the matrix and the product of the eigen values gives the determinant of the matrix.

PROGRAM

clc;

clear all;

close all;

A=input('enter the matrix');

Eig_values=eig(A);

Detr=det(A)

p=prod(Eig_values)

 T=trace(A) %sum of diagonal elements of A

S=sum(Eig_values)

INPUT

[-2 2 -3;2 1 -6;-1 -2 0]

OUTPUT

Detr =45

p =45.0000

T =-1

S =-1.0000

10. Show that AV=VD, where D is the eigen values and V is the eigen vectors of the square matrix A. From this relation, represent A matrix using eigen value decomposition.  

PROGRAM

clc;

clear all;

close all;

A=input('enter the matrix');

[V D]=eig(A);

Eig_values=diag(D);

 

%verifying AV = VD

LHS=A*V;

RHS=V*D;

Difference=LHS-RHS;

A_approximate= V*D*inv(V);

Diff=A-A_approximate;

 

INPUT

[-2 2 -3;2 1 -6;-1 -2 0] 

OUTPUT

A_approximate =

   -2.0000    2.0000   -3.0000

    2.0000    1.0000   -6.0000

   -1.0000   -2.0000   -0.0000

11. For the matrix A = 

 

      -2    2     -3

       2     1     -6

      -1     -2     0

 

Show that the eigen values are the roots of the characteristic equation

 

PROGRAM

 

A = [-2 2 -3; 2 1 -6; -1 -2 0];

p=poly(A);

Root = roots(p);

Eig_values=eig(A);

 

OUTPUT

Root =

   5.0000 + 0.0000i

  -3.0000 + 0.0000i

  -3.0000 - 0.0000i

Eig_values =

   -3.0000

    5.0000

   -3.0000

 

12.  Approximate the matrix A for N = 1000 with the help of singular value decomposition of A as 


where Ui and Vi are the singular vectors and ʎi are the eigen values with ʎi < ʎj for i > j. Plot the absolute error(ζ) between A and 
 
as

 ζ  =
against r for r = 10, 50, 75, 100, 250, 500, 750 and appreciate the plot

PROGRAM

clc;

clear all;

close all;

M=1050;

N=1000;

A=randi(10,M,N);

[U,S,V] = svd(A);

% U is M x M matrix , V is N x N matrix and S is M x N Diagonal matrix

 

M1=U*S*V';

 vtrans = V';

 

 r=[10 50 75 100 250 500 750 1000];

 

 for i=1:8

 U1=U(:,[1:r(i)]);

 V1trans=vtrans([1:r(i)],:);

 S1=S([1:r(i)],[1:r(i)]);

 M2=U1*S1*V1trans;

 

 sum1=0;

 for j=1:M

     for k=1:N

    sum1 =sum1+(abs(A(j,k)-M2(j,k)))^2;

     end

 end

 

 errors(i)=sum1;

 end

 

plot(r, errors);

title('Plot of error');

xlabel('values of r')

ylabel('absolute error');


OUTPUT


13. Plot sine and cosine waves (both continuous and discrete plots) using GUI.

PROCEDURE

1. Type GUIDE in the command window

2. In the GUIDE Quick Start dialog box, select the Blank GUI (Default) template, and then click OK.

3. Display the names of the components in the component palette:

a.       Select File > Preferences > GUIDE.

b.      Select Show names in component palette.

c.       Click OK.

 

4.  Add the two push buttons to the UI. Select the push button tool from the component palette at the left side of the Layout Editor and drag it into the layout area. Create three buttons

5. Add the remaining components to the UI.

                A static text area

                A pop-up menu

                An axes

     6. Label the Push Buttons

          (a) In the layout area, click the top push button.

          (b) In the Property Inspector, select the String property, and then replace the existing value with the word Continuous.

          (c) The push button label changes to Continuous


 


Comments

Popular posts from this blog

Teaching Stuff

NUMERICAL DIFFERENTIATION AND INTEGRATION

FAMILIARIZATION OF THE COMPUTING TOOL