You are on page 1of 25

KHULNA UNIVERSITY OF ENGINEERING &

TECHNOLOGY

Department of Mechatronics Engineering

Sessional Lab Report


Course No: MTE 2210

Course Title: Systems Simulation

Submitted By:
Name: Faria Sultana Mimi

Roll: 1931001

Batch: 2k19

Year: 2’nd

Semester: 2’nd

Date: 21/ 09/ 2022


Experiment-01
Answer to the exercise 1(a):

edit dct;
edit sin;

Output:
function b=dct(a,varargin)
%DCT Discrete cosine transform.
% Y = DCT(X) returns the discrete cosine transform of vector X.
% If X is a matrix, the DCT operation is applied to each
% column. For N-D arrays, DCT operates on the first non-singleton
% dimension. This transform can be inverted using IDCT.
%
% Y = DCT(X,N) pads or truncates the vector X to length N
% before transforming.
%
% Y = DCT(X,[],DIM) or Y = DCT(X,N,DIM) applies the DCT operation along
% dimension DIM.
%
% Y = DCT(...,'Type',K) specifies the type of discrete cosine transform
% to compute. K can be one of 1, 2, 3, or 4, to represent the DCT-I,
% DCT-II, DCT-III, and DCT-IV transforms, respectively. The default
% value for K is 2 (the DCT-II transform).
%
% % Example:
% % Find how many DCT coefficients represent 99% of the energy
% % in a sequence.
%
% x = (1:100) + 50*cos((1:100)*2*pi/40); % Input Signal
% X = dct(x); % Discrete cosine transform
% [XX,ind] = sort(abs(X)); ind = fliplr(ind);
% num_coeff = 1;
% while (norm([X(ind(1:num_coeff)) zeros(1,100-num_coeff)])/norm(X)<.99)
% num_coeff = num_coeff + 1;
% end
% num_coeff
%
% See also FFT, IFFT, IDCT.

% Author(s): C. Thompson, 2-12-93


% S. Eddins, 10-26-94, revised
% Copyright 1988-2018 The MathWorks, Inc.

% References:
% 1) A. K. Jain, "Fundamentals of Digital Image
% Processing", pp. 150-153.
% 2) Wallace, "The JPEG Still Picture Compression Standard",
% Communications of the ACM, April 1991.

if nargin == 0
error(message('signal:dct:Nargchk'));
end

if nargin > 1
[varargin{:}] = convertStringsToChars(varargin{:});
end

% checks if X is a valid numeric data input


signal.internal.sigcheckfloattype(a,'','dct','X');
if isempty(a)
b = zeros(0,0,'like',a);
return
end

if isa(a,'gpuArray')
coder.internal.errorIf(~ismatrix(a),'signal:dct:NotAMatrixGPU');
if ~isempty(varargin)
coder.internal.errorIf(~isscalar(varargin{1})||length(varargin)>1,...
'signal:dct:TooManyInputs');
end
end

if (~coder.target('MATLAB') || isa(a,'gpuArray')) && (isempty(varargin) || isscalar(varargin{1}))


b = dctcodegen(a,varargin{:});
else
b = dctinternal(a,varargin{:});
end

function b = dctinternal(varargin)

p = inputParser;
p.addRequired('X');
p.addOptional('N',[]);
p.addOptional('DIM',[]);
p.addParameter('Type',2);
p.parse(varargin{:});

r = p.Results;
x = r.X;
type = r.Type;
if isempty(r.N) && isempty(r.DIM)
[dim,n] = firstNonSingletonDimension(x);
elseif isempty(r.N)
dim = r.DIM;
n = size(x,dim);
elseif isempty(r.DIM)
dim = firstNonSingletonDimension(x);
n = r.N;
else
dim = r.DIM;
n = r.N;
end

validateattributes(n,{'numeric'},{'integer','scalar','positive','finite'});
validateattributes(dim,{'numeric'},{'integer','scalar','positive','finite'});
n = double(n);

scale = sqrt([1/(2*(n-1)) 1/(2*n) 1/(2*n) 1/(2*n)]);


dcscale = sqrt([2 1/2 2 1]);

if type==1
x = x .* scale(type);
idc = dimselect(dim,size(x));
x(1+idc) = x(1+idc) * dcscale(type);
if size(x,dim) >= n
x(end-idc) = x(end-idc) * dcscale(type);
end
elseif type==3
x = x .* scale(type);
idc = 1+dimselect(dim,size(x));
x(idc) = x(idc) * dcscale(type);
end

if n==1
if dim==1
b = matlab.internal.math.transform.mldct(x(1,:),n,dim,'Variant',type);
elseif dim==2
b = matlab.internal.math.transform.mldct(x(:,1),n,dim,'Variant',type);
else
b = matlab.internal.math.transform.mldct(x,n,dim,'Variant',type);
end
else
b = matlab.internal.math.transform.mldct(x,n,dim,'Variant',type);
end

if type==1
idc = dimselect(dim,size(b));
b(idc+1) = b(idc+1) * sqrt(0.5);
b(end-idc) = b(end-idc) * sqrt(0.5);
elseif type==2
b = b .* scale(type);
idc = 1+dimselect(dim,size(b));
b(idc) = b(idc) * dcscale(type);
elseif type==4
b = b .* scale(type);
idc = 1+dimselect(dim,size(b));
b(idc) = b(idc) * dcscale(type);
end

function idx = dimselect(idim, dim)


ndim = numel(dim);
nel = prod(dim);
dcterm = prod(dim(1:min(idim-1,ndim)));
if idim<=ndim
nskip = dcterm*dim(idim);
else
nskip = dcterm;
end
idx = (0:dcterm-1)' + (0:nskip:nel-1);
idx = idx(:);

function [dim,n] = firstNonSingletonDimension(a)


sz = size(a);
dim = find(sz~=1,1,'first');
if isempty(dim)
dim = 1;
n = 1;
else
n = sz(dim);
end

function b = dctcodegen(a,n)

% If input is a vector, make it a column:


do_trans = (size(a,1) == 1);
if do_trans
a = a(:);
end

if nargin==1
n = size(a,1);
end
% Cast to enforce precision rules
n = signal.internal.sigcasttofloat(n,'double','dct','N','allownumeric');
m = size(a,2);

% Pad or truncate input if necessary


if size(a,1)<n
aa = zeros(n,m,'like',a);
aa(1:size(a,1),:) = a;
else
aa = a(1:n,:);
end

% Compute weights to multiply DFT coefficients (cast enforces the precision


% rules)
ww = (exp(-1i*cast(0:n-1,'like',a)*pi/(2*n))/sqrt(2*n)).';

ww(1) = ww(1) / sqrt(2);

if isodd(n) || ~isreal(a) % odd case


% Form intermediate even-symmetric matrix
y = zeros(2*n,m,'like',a);
y(1:n,:) = aa;
y(n+1:2*n,:) = flipud(aa);

% Compute the FFT and keep the appropriate portion:


yy = fft(y);
yy = yy(1:n,:);

else % even case


% Re-order the elements of the columns of x
y = [ aa(1:2:n,:); aa(n:-2:2,:) ];
yy = fft(y);
ww = 2*ww; % Double the weights for even-length case
end

% Multiply FFT by weights:


b = ww(:,ones(1,m)) .* yy;

if isreal(a)
b = real(b);
end
if do_trans
b = b.';
end

Answer to the exercise 1(b):

Help rand

Output:
rand Uniformly distributed pseudorandom numbers.

R = rand(N) returns an N-by-N matrix containing pseudorandom values drawn

from the standard uniform distribution on the open interval(0,1). rand(M,N)

or rand([M,N]) returns an M-by-N matrix. rand(M,N,P,...) or

rand([M,N,P,...]) returns an M-by-N-by-P-by-... array. rand returns a

scalar. rand(SIZE(A)) returns an array the same size as A.

Note: The size inputs M, N, P, ... should be nonnegative integers.

Negative integers are treated as 0.

R = rand(..., CLASSNAME) returns an array of uniform values of the

specified class. CLASSNAME can be 'double' or 'single'.


R = rand(..., 'like', Y) returns an array of uniform values of the

same class as Y.

The sequence of numbers produced by rand is determined by the settings of

the uniform random number generator that underlies rand, RANDI, and RANDN.

Control that shared random number generator using RNG.

Examples:

Example 1: Generate values from the uniform distribution on the

interval (a, b).

r = a + (b-a).*rand(100,1);

Example 2: Use the RANDI function, instead of rand, to generate

integer values from the uniform distribution on the set 1:100.

r = randi(100,1,5);

Example 3: Reset the random number generator used by rand, RANDI, and

RANDN to its default startup settings, so that rand produces the same

random numbers as if you restarted MATLAB.

rng('default')

rand(1,5)

Example 4: Save the settings for the random number generator used by

rand, RANDI, and RANDN, generate 5 values from rand, restore the

settings, and repeat those values.

s = rng

u1 = rand(1,5)

rng(s);

u2 = rand(1,5) % contains exactly the same values as u1

Example 5: Reinitialize the random number generator used by rand,

RANDI, and RANDN with a seed based on the current time. rand will

return different values each time you do this. NOTE: It is usually

not necessary to do this more than once per MATLAB session.


rng('shuffle');

rand(1,5)

See Replace Discouraged Syntaxes of rand and randn to use RNG to replace

rand with the 'seed', 'state', or 'twister' inputs.

See also randi, randn, rng, RandStream, RandStream/rand,

sprand, sprandn, randperm

Documentation for rand

Other functions named rand

Answer to the exercise 1(c):

>> a=3;

>> b=5;

>> c=7;

>> clear b

Output:

Experiment-02
Answer to the exercise 1(a):

>> x=[1 4; 8 3];

>> inv(x)

ans =

-0.1034 0.1379

0.2759 -0.0345

Answer to the exercise 1(b):


>> diag(x)

ans =

Answer to the exercise 1(c)

>> sum(x)

ans =

9 7

>> sum(sum(x))

ans =

16

Answer to the exercise 1(d):

>> x'

ans =

1 8

4 3

Answer to the exercise 2(a):

>> x= [2 8 5; 9 7 1];

>> b=[2 4 5];

>> max(x)

ans =

9 8 5

>> max(max(x))

ans =

>> min(x)

ans =
2 7 1

>> min(min(x))

ans = 1

Answer to the exercise 2(b):

>> median(x)

ans =

2 7 5

>> median(median(x))

ans =

Answer to the exercise 2(c):

> x( end+1, : ) = b

x=

2 8 5

9 7 1

2 4 5

Answer to the exercise 3(a):

>> x=[ 2 6 12; 15 6 3; 10 11 1];

>> a=mean(x)

a=

9.0000 7.6667 5.3333

>> x(1, 1:3)=a

x=

9.0000 7.6667 5.3333

15.0000 6.0000 3.0000

10.0000 11.0000 1.0000

Answer to the exercise 3(b):


>> reshape(x,[9,1])

ans =

9.0000

15.0000

10.0000

7.6667

6.0000

11.0000

5.3333

3.0000

1.0000

Answer to the exercise 4:

>> eye(4)

ans =

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Answer to the exercise 5:

>> b=[5:5:100]

b=

Columns 1 through 16

5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80

Columns 17 through 20

85 90 95 100

>> numel(b)
ans =

20

Experiment-03:
Answer to the exercise 1(a):

>> x=2

x=

>> y=4

y=

>> z=sqrt(5*x^2+y^2)

z=

Answer to the exercise 1(b):

>> x=pi/4

x=

0.7854

>> z=4*cos(x)+i*6*sin(x)

z=

2.8284 + 4.2426i

Answer to the exercise 1(c):

>> x=pi/3

x=
1.0472

>> y=2

y=

>> z=3*sin(x)+4*cos(x)+3*exp(y)

z=

26.7652

Answer to the exercise 1(d):

>> x=[0:2*pi]

x=

0 1 2 3 4 5 6

>> y=sin(x)/x

y=

-0.0705

Answer to the exercise 2:

>> a=[1 1 -2; 2 1 0; 1 1 -1];

>> b=[3;7;4];

>> solution=a\b

solution =

Answer to the exercise 3:

>> floor(1.3)

ans =

>> fix(1.5)
ans =

>> round(1.9)

ans =

>> fix(11.9)

ans =

11

>> ceil(-2.9)

ans =

-2

>> floor(-3.9)

ans =

-4

>> round(3.4)

ans = 3

Answer to the exercise 4:

>> r=2*10^-13

r=

2.0000e-13

>> Q=1.6e-19

Q=

1.6000e-19

>> K=9e9

K=

9.0000e+09

>> Electromagnetic_Force=K*Q^2/r^2
Electromagnetic_Force =

0.0058

Answer to the exercise 5:

>> [2:0.15:3.5]

ans =

Columns 1 through 9

2.0000 2.1500 2.3000 2.4500 2.6000 2.7500 2.9000 3.0500 3.2000

Columns 10 through 11

3.3500 3.5000

Experiment-04
Answer to the exercise 1(a):

x = linspace(2,3.5,10) %Generating ten valuesx =1; y=0; z=1;

v = (x | y) & z

w = ~ (x | y) & z

u = (x & (~y)) | ((~x) & y)


Answer to the exercise 2:

x = input('Enter the bit sequence in MAT to test for Even parity: ');

t = 0;

for i = 1:length(x) %can replace this 'for' loop just by t=sum(x)

if(x(i))

t = t + 1; %increment by one if a bit is one

end

end

if(mod(t,2)~=0) %check if not even then attach another '1' to make the

parity

even

y = [x 1]; disp('Parity bit generated : 1');

else %check if even then attach another '0' to let the parity be even

y = [x 0]; disp('Parity bit generated : 0');

end

disp('Input bit sequence:');

disp(x); %display the input bit sequence

disp('Bit sequence with parity (Even) bit : ');

disp(y); %display the resultant bit sequence after parity bit addition

Output:
Answer to the exercise 3:

a=rand(10,10)>0.5;

%the variable 'a' contains the binary values in terms of a matrix

currently

its a 100x100 binary matrix %you can provide your own matrix of any size

but

remember it will calculate the gray code row wise

g(:,1) = a(:,1); %assign the 1st element of each row to the resultant 'g',

as

it is.for i = 2:size(a,2)

g(:,i) = xor( a(:,i-1), a(:,i) );%processing as per the procedure

end

disp('Input: ');

disp(a);

disp('Output: ');
disp(g);

Output:

Answer to the exercise 4:

(a) >> q=[1 5 6 8 3 2 4 5 9 10 1];

>> x=[3 5 7 8 1 2 4 11 5 9];

>> q>2

ans =

1×11 logical array

0 1 1 1 1 0 1 1 1 1 0

(b) >> find(q==x)


ans =

2 4 5

(c) >> x<=7


ans =

1×11 logical array

1 1 1 0 1 1 1 1 0 1 0

Answer to the exercise 5:


>> x=[10 3; 9 15];

>> y=[10 0; 9 3];

>> z=[-1 0; -3 2];

(a) >> v=x>y

v=

2×2 logical array

0 1
1 1
(b) >> w=z>=y

w=

2×2 logical array

0 1

0 0

(c) ©>> u=-z&y

u=

2×2 logical array

1 0

1 1

(d) >> t=x&y<z

t=

2×2 logical array

0 0

0 0

Experiment-05
Answer to the exercise 1:

>> t=0:0.001:30;
A=4;

w=2*pi*2;

y=A*sawtooth(w*t);

plot(t,y)

Answer to the exercise 2:

>> x=[-2*pi:0.1:2*pi];

>> y=sin(x)./x;

>> plot(y)

Answer to the exercise 3:


>> x=(-pi:0.1:pi);
>> y=sin(x);
>> z=cos(x);
>> plot(y,'r')
>> hold on
>> plot(z,'b')

Answer to the exercise 4:


>> x=[-pi:0.1:pi];
y=sin(x);
z=cos(x);
v=exp(x);
subplot(2,2,1); plot(y)
title('y=sin(x)')
>> subplot(2,2,2); plot(z)
title('z=cos(x)')
>> subplot(2,2,3); plot(v)
title('v=exp(x)')

Experiment 06
Answer to the exercise 1:

>> z=2+5i
z=
2.0000 + 5.0000i
>> abs_of_z=abs(z)
abs_of_z =
5.3852
>> angle_of_z=angle(z)*180/pi
angle_of_z =
68.1986
>> Y=-3-3i
Y=
-3.0000 - 3.0000i
>> abs_of_Y=abs(Y)
abs_of_Y =
4.2426
>> angle_of_Y=angle(Y)*180/pi
angle_of_Y =
-135
>> D=-2+6i
D=
-2.0000 + 6.0000i
>> abs_of_D=abs(D)
abs_of_D =
6.3246
>> angle_of_D=angle(D)*180/pi
angle_of_D =
108.4349

Answer to the exercise 2:

>> Conjugate_of_Z=conj(z)
Conjugate_of_Z =
2.0000 - 5.0000i
>> Conjugate_of_D=conj(D)
Conjugate_of_D =
-2.0000 - 6.0000i
>> Conjugate_of_Y=conj(Y)
Conjugate_of_Y =
-3.0000 + 3.0000i

Answer to the exercise 3:

>> W=5*cosd(30)+5*sind(30)*i
W=
4.3301 + 2.5000i

>> A=2.5*cosd(-20)+2.5*sind(-20)*i

A=

2.3492 - 0.8551i
>> r=3e+15
r=
3.0000e+15
>> Q=r*cosd(-73)+r*sind(-73)*i
Q=
8.7712e+14 - 2.8689e+15i

Answer to the exercise 4:


Answer to the exercise 5:

>> a=[1 3];


>> b=[2 3 5;4 7 8];
>> c=[2 3 3; 4 7 7];
>> x=a*b
x=
14 24 29
>> y=a*c
y=
14 24 24

Experiment 07
Answer to the exercise 1:

>> x=[1 5 9; 2 7 4];


>> s='The sum of 1st row ='
t='The sum of 2nd row ='
>> a=x(2,3)

a=
4
>> disp([s num2str(a)])
The sum of 1st row =4
b=
7
>> disp([t num2str(b)])
The sum of 2nd row =7

Answer to the exercise 2:

a=
0.9649 0.9572 0.1419
0.1576 0.4854 0.4218
0.9706 0.8003 0.9157
>> d=diag(a)
d=
0.9649
0.4854
0.9157
>> s='The Diagonal of This Matrix ='
s=
'The Diagonal of This Matrix ='
b=d'
b=
0.9649 0.4854 0.9157
>> disp([s num2str(b)])
The Diagonal of This Matrix =0.96489 0.48538 0.91574

Answer to the exercise 3:

x = input('Enter the string: ');

y= double(x);

disp(y);

Output:

Answer to the exercise 4:

>> faria_avg = mean([80,80,80]);

sultana_avg = mean([75,80,70]);

mimi_avg = mean([80,90,85]);

disp(' Name Degree');


disp('----------------------'); fprintf('Faria %d\n', faria_avg);

fprintf('Sultana %d\n', sultana_avg);

fprintf('Mimi %d\n', mimi_avg);

Name Degree

----------------------

Faria 80

Sultana 75

Mimi 85

Answer to the exercise 5 :


upper(a)

lower(W)

Experiment 08
Answer to the exercise 1:

x = input('Enter the value of x : ');

y = input('Enter the value of y : ');

z = input('Enter the value of z : ');

if y>=4*x*z;

S = sqrt(y^2-4*x*z);

else y<4*x*z;

S = inf;

end

fprintf('x = %d\n', x);

fprintf('y = %d\n', y);

fprintf('z = %d\n', z);

fprintf('s = %d\n', S);


Output:

Answer to the exercise 2:


%By using conditional statements
s=input('enter 1 if switch is open otherwise enter 0:');
v=5;
r1=2.5;
r2=5;
r3=5;
if(s==1)
i=v/(r1+r2);
else b=(r2*r3)/(r2+r3);
i=v/(b+r1);
end
fprintf('current i is %d A\n',i);
% without using any conditional statements
s=input('enter 1 if switch is open otherwise enter 0:');
v=5;
r1=2.5;
r2=5;
r3=5; b=(r2*r3)/(r2+r3);

i=(s)*(v/(r1+r2))+(~s)*(v/(b+r1)); fprintf('current i is %d A\n',i);

You might also like