saksham MATLAB

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

MATLAB

THEORY AND PRACTICE


LAB FILE
(Online mode)

Amity School of Engineering and Technology


Amity University Noida – 201303

Saksham Arora
A2324621013
B. Tech. (7EEE)

Submitted to
Dr. V.K. Tayal

1
List of Contents

1. (a) Insert any two 2D matrixes with user input commands and perform addition,
subtraction, multiplication of the matrix.
(b) Generate the following vector b = [1, 2, 3, 4, 5,…………9, 10], then transpose it
to column and
i. Replace the first five elements of vector b with its maximum value.
ii. Reshape this vector into a 3x3 matrix.
iii. Find inverse of 3x3 matrix get by b and assign it to x.
iv. Find diagonal of x.
v. The sum of each column and the sum of whole matrix x.
vi. Transpose of matrix x.
(c) Write a MATLAB program to calculate the following expression and round the
answers to the nearest integer.

i. z=√ 5 x 2+ y 2 where x=2 , y=4


π
ii. z=4 cos ( x ) + j 6 sin ( x ) where x=
4
y π
iii. z=3 sin ( x ) +4 cos ( x )+ 3 e where x= , y=2
3
sin ( x )
iv. y= where 0 ≤ x ≤ 2 π
x
(d) Write a Program to calculate the electromagnetic force between two electrons
placed (in vacuum) at a distance (r = 2*10-13 m) from each other. Charge of
electron (Q) is 1.6*10-19 C.
2. write a program to read three bits x, y, z, then compute:
(a) v=( x∧ y )∨z
(b) w=not ( x∨ y )∧z

(c) u=( x∧not ( y )) ∨( not ( x ) ∧ y )

3. If x=[10 3 ; 9 15], y=[10 0; 9 3], z=[-1 0; -3 2], what is the output of the following
statements:
(a) v=x> y
(b) w=z ≥ y
(c) u= z∧ y

2
(d) t=x∧ y < z
sin ( x )
4. Plot sinx function where sinc ( x )= ∧−2 π ≤ x ≤ 2 π and if
x
y=sin ( x ) , z=cos ( x ) , v=exp ( x ) , where−π ≤ x ≤ π the plot y, z, v in a single figure with
different line style and different colors.
5. Compute the standard deviation by using the following equations then compare the
result with that one obtained by std command
n 1
1
s={ ∑( x
n−1 i=1 i
−x )
2 2
}

n
1
x= ∑x
n i=1 i

6. Write a program to find the current I in the given circuit


(a) By using conditional statements.
(b) Without using any conditional statements.
7. To obtain simulation of single phase fully controlled bridge rectifier and draw load
voltage and load current waveform for inductive load.
8. Transformer Tests using Simulink:
(a) Open Circuit Test
(b) Short Circuit Test
(c) Load Test
9. Newton Raphson method of load flow analysis using MATLAB Software
10. To explore the stability of a control system using pole zero map of transfer function.
Routh-Hurwitz criteria to determine stability
11. To explore the root-locus analysis of a control system. Range of gain for stable
operation using root locus. Effect of PI controller and integral controller on system
response.
12. To study time and frequency domain analysis of a given system using Linear
System Analyzer in Matlab.
13. To tune the values of gains of a PID controller using PID Tuner to achieve desired
performance.
14. To minimize a simple function of two variables with bound constraints using
particle swarm optimization technique.
15. To Minimize a Nonsmooth Function with Linear Equality and Inequality
Constraints using genetic algorithm.

3
Index
Experiment Date
Experiment 1
Experiment 2
Experiment 3
Experiment 4
Experiment 5
Experiment 6
Experiment 7
Experiment 8
Experiment 9
Experiment 10
Experiment 11
Experiment 12
Experiment 13
Experiment 14
Experiment 15

4
EXPERIMENT 1(a)
AIM: Insert any two 2D matrixes with user input commands and perform addition,
subtraction, multiplication of the matrix
TOOLS USED: MATLAB R2024a
CODE:
rows1 = input('Enter number of rows for matrix 1: ');
cols1 = input('Enter number of columns for matrix 1: ');
rows2 = input('Enter number of rows for matrix 2: ');
cols2 = input('Enter number of columns for matrix 2: ');
if rows1 ~= rows2 || cols1 ~= cols2
error('Matrix dimensions must be the same for addition, subtraction, and multiplication.');
end
matrix1 = zeros(rows1, cols1);
matrix2 = zeros(rows2, cols2);
fprintf('Enter elements for matrix 1:\n');
for i = 1:rows1
for j = 1:cols1
matrix1(i, j) = input(sprintf('Enter element (%d, %d): ', i, j));
end
end
fprintf('Enter elements for matrix 2:\n');
for i = 1:rows2
for j = 1:cols2
matrix2(i, j) = input(sprintf('Enter element (%d, %d): ', i, j));
end
end
addition = matrix1 + matrix2;
subtraction = matrix1 - matrix2;
multiplication = matrix1 * matrix2;
disp('Matrix 1:');

5
disp(matrix1);
disp('Matrix 2:');
disp(matrix2);
disp('Addition Result:');
disp(addition);
disp('Subtraction Result:');
disp(subtraction);
disp('Multiplication Result:');
disp(multiplication);

6
OUTPUT:

7
EXPERIMENT 1(b)
AIM: Generate the following vector b = [1, 2, 3, 4, 5…………9, 10], then transpose it to
column and
I. Replace the first five elements of vector b with its maximum value.
II. Reshape this vector into a 3x3 matrix.
III. Find inverse of 3x3 matrix get by b and assign it to x.
IV. Find diagonal of x.
V. The sum of each column and the sum of whole matrix x.
VI. Transpose of matrix x.
TOOLS USED: MATLAB R2024a
CODE:
b = 1:9;
b_col = b';
max_val = max(b);
b_col(1:5) = max_val;
B_matrix = reshape(b_col, [3, 3]);
x = inv(B_matrix);
diagonal_x = diag(x);
column_sum = sum(x);
total_sum = sum(column_sum);
x_transpose = x';
disp('Column vector b:');
disp(b_col);
disp('3x3 Matrix B:');
disp(B_matrix);
disp('Inverse of B (matrix x):');
disp(x);
disp('Diagonal of x:');

8
OUTPUT:
disp(diagonal_x);
disp('Sum of each column:');
disp(column_sum);
disp('Total sum of the matrix x:');
disp(total_sum);
disp('Transpose of matrix x:');
disp(x_transpose);

9
EXPERIMENT 1(c)
AIM: Write a MATLAB program to calculate the following expression and round the answers
to the nearest integer.

I. z=√ 5 x 2+ y 2 where x=2 , y=4


π
II. z=4 cos ( x ) + j 6 sin ( x ) where x=
4
y π
III. z=3 sin ( x ) +4 cos ( x )+ 3 e where x= , y=2
3
sin ( x )
IV. y= where 0 ≤ x ≤ 2 π
x
TOOLS USED: MATLAB R2024a
CODE:
%Finding value of first expression
x=2;
y=4;
z1=round(sqrt(5*x^2+y^2))
%Finding value of second expression
x=pi/4;
z2=round(sqrt(4*cos(x)+i*6*sin(x)))
%Finding value of third expression
x=pi/3;
y=2;
z3=round(sqrt(4*cos(x)+3*sin(x)+3*exp(y)))
%Finding value of fourth expression
for x=0:2*pi
z4=round(sin(x)/x)
end

10
OUTPUT:

11
EXPERIMENT 1(d)
AIM: Write a Program to calculate the electromagnetic force between two electrons placed
(in vacuum) at a distance (r = 2*10-13 m) from each other. Charge of electron (Q) is 1.6*10-
19 C.
TOOLS USED: MATLAB Ra2024a
CODE:
%Charge of electron
e=1.6*10^(-19)
%Distance between the electrons
r=2*10^(-13)
%Electromagnetic force between elements
f=(9*10^9)*e^2/r^2
OUTPUT:

12
EXPERIMENT 2
AIM: Write a program to read three bits x ,y , z then compute:
(a) v = (x and y )or z
(b) w = not (x or y)and z
(c) u = (x and not (y))or(not(x)and y)
TOOLS USED: MATLAB R2024a
CODE:
x=input('Give x:');
if x==0||x==1
y=input('Give y:');
else
disp('Value not logical!!');
end
if y==0||y==1
z=input('Give z:');
else
disp('Value not logical!!!');
end
if z==0||z==1
v=(x&y)|z;
w=~(x|y)&z;
u=(x&~y)|(~x&y);
disp(['v=',num2str(v)]);
disp(['w=',num2str(w)]);
disp(['u=',num2str(u)]);
else
disp('Value not logical!!!');
end

13
OUTPUT:

14
EXPERIMENT 3
AIM: If x=[10 3 ; 9 15], y=[10 0 ; 9 3], z=[-1 0 ; -3 2], what is the output of the following
statements:
(a) v=x>y
(b) w=z≥y
(c) u = ~z & y
(d) t=x&y<z
TOOLS USED: MATLAB R2024a
CODE:
% Program to input three matrices and perform logical operations

% Get the order of the matrices


disp('Enter the order of the matrices:');

rows = input('Number of rows: ');


cols = input('Number of columns: ');

% Initialize the matrices


x = zeros(rows, cols);
y = zeros(rows, cols);
z = zeros(rows, cols);

% Get elements for matrix x


disp('Enter elements for matrix x:');
for i = 1:rows
for j = 1:cols
x(i, j) = input(sprintf('x(%d,%d): ', i, j));
end
end

15
% Get elements for matrix y
disp('Enter elements for matrix y:');
for i = 1:rows
for j = 1:cols
y(i, j) = input(sprintf('y(%d,%d): ', i, j));
end
end

% Get elements for matrix z


disp('Enter elements for matrix z:');
for i = 1:rows
for j = 1:cols
z(i, j) = input(sprintf('z(%d,%d): ', i, j));
end
end

% Perform logical operations


v = x > y;
w = z >= y;
u = ~z & y;
t = x & (y < z);

% Display the results


disp('Result of v = x > y:');
disp(v);

disp('Result of w = z ≥ y:');
disp(w);

disp('Result of u = ~z & y:');

16
disp(u);

disp('Result of t = x & (y < z):');


disp(t);

OUTPUT:

17
EXPERIMENT 4
sin ( x )
AIM:- Plot sinx function where sinc ( x )= ∧−2 π ≤ x ≤ 2 π and if
x
y=sin ( x ) , z=cos ( x ) , v=exp ( x ) , where−π ≤ x ≤ π the plot y, z, v in a single figure with
different line style and different colors.
TOOLS USED:- Matlab R2024a
CODE:-
% Plot sinc function in the range -2*pi to 2*pi
x1 = linspace(-2*pi, 2*pi, 1000);
sinc_x1 = sin(x1) ./ x1;

% Handle the singularity at x = 0


sinc_x1(x1 == 0) = 1;

figure;
plot(x1, sinc_x1, 'b-', 'LineWidth', 2);
title('sinc(x) = sin(x) / x');
xlabel('x');
ylabel('sinc(x)');
grid on;
hold off;

% Plot y = sin(x), z = cos(x), v = exp(x) in the range -pi to pi


x2 = linspace(-pi, pi, 1000);
y = sin(x2);
z = cos(x2);
v = exp(x2);

figure;
plot(x2, y, 'r--', 'LineWidth', 2); % Red dashed line for sin(x)

18
hold on;
plot(x2, z, 'g:', 'LineWidth', 2); % Green dotted line for cos(x)
plot(x2, v, 'b-.', 'LineWidth', 2); % Blue dash-dot line for exp(x)
hold off;

title('y = sin(x), z = cos(x), v = exp(x)');


xlabel('x');
ylabel('Function values');
legend('y = sin(x)', 'z = cos(x)', 'v = exp(x)');
grid on;
% Plot sinc function in the range -2*pi to 2*pi
x1 = linspace(-2*pi, 2*pi, 1000);
sinc_x1 = sin(x1) ./ x1;

% Handle the singularity at x = 0


sinc_x1(x1 == 0) = 1;

figure;
plot(x1, sinc_x1, 'b-', 'LineWidth', 2);
title('sinc(x) = sin(x) / x');
xlabel('x');
ylabel('sinc(x)');
grid on;
hold off;

% Plot y = sin(x), z = cos(x), v = exp(x) in the range -pi to pi


x2 = linspace(-pi, pi, 1000);
y = sin(x2);
z = cos(x2);
v = exp(x2);

19
figure;
plot(x2, y, 'r--', 'LineWidth', 2); % Red dashed line for sin(x)
hold on;
plot(x2, z, 'g:', 'LineWidth', 2); % Green dotted line for cos(x)
plot(x2, v, 'b-.', 'LineWidth', 2); % Blue dash-dot line for exp(x)
hold off;

title('y = sin(x), z = cos(x), v = exp(x)');


xlabel('x');
ylabel('Function values');
legend('y = sin(x)', 'z = cos(x)', 'v = exp(x)');
grid on;
OUTPUT: -

20
21
EXPERIMENT-5
AIM:- Compute the standard deviation by using the following equations then compare the
result with that one obtained by std command
n 1
1
s={ ∑( x
n−1 i=1 i
−x )
2 2
}

n
1
x= ∑x
n i=1 i

TOOLS USED: - Matlab 2024Ra


CODE: -

% Standard Deviation
% Method One
x = [1 3 2 6 8 5 3 4 2];
std(x)

% Second Method
x= [1 3 2 6 8 5 3 4 2];
n=length(x);
xbar=sum(x)/n;
xd=0;
for i=1:n
xd=(x(i)-xbar)^2+xd;
end
xstd=sqrt((xd)/(n-1))

22
OUTPUT: -

23
EXPERIMENT-6
AIM: - Write a program to find the current I in the given circuit
(a) By using conditional statements.
(b) Without using any conditional statements.

Apparatus: - Matlab 2024Ra


Circuit:1 with conditional statements
clc;
clear all;
% Circuit 1- With conditional statements
V= input('Enter dc voltage source(V): ');
R1= input('Enter series resistance(R1): ');
R2= input('Enter shunt resistance(R2): ');
R3= input('Enter load resistance(R3): ');
if (V)&&(R1~=inf)&&(R2)&&(R3~=inf)
RT=R1+(R3*R2)/(R2+R3);
IT=V/RT;
IL=IT*(R2/(R2+R3)); %Load Current is current in R3
disp(['The current in load branch is: ',num2str(IL)]);
elseif ((~R1)&&(~R3))
disp('The current in load branch is infinite')

24
else
disp('Load Current is equal to zero')
end
Output:-

Circuit 1: without conditional statements


clc;
clear all;
% Circuit 1- Without conditional statements
V= input('Enter dc voltage source(V): ');
R1= input('Enter series resistance(R1): ');
R2= input('Enter shunt resistance(R2): ');
R3= input('Enter load resistance(R3): ');
% Calculate equivalent resistances for open and short-circuited cases
R2_equiv = 1 / (1 / R2 + 1 / R3);
% Calculate the total current passing through the circuit
I_total = V / (R1 + R2_equiv);
% Calculate the current passing through the load resistance
current_in_load = I_total * (R2 / (R2 + R3));
disp(current_in_load)
output:

25
Circuit 1: Simulink

Output:-
Whole circuit

26
R2 Branch

R3 Branch

27
Circuit 2: with conditional statements
clc;
clear all;
% Circuit 2- With conditional statements
Vrms= input('Enter AC RMS voltage source(V): ');
R1= input('Enter series resistance(R1): ');
R2= input('Enter shunt resistance(R2): ');
R3= input('Enter load resistance(R3): ');
C= input('Enter Capacitor Value(C): ');
L= input('Enter Inductor Value(L): ');
f= input('Enter Supply frequency Value(f): ');
if (Vrms)&&(R1~=inf)&&(R2)&&(R3~=inf)
w = 2*pi*f;
Z1 = R2 + 1j*w*L;
Z3 = (Z1*R3)/[Z1 + R3];
Xc = (-1)/(w*C);
Z2 = (R1*(1j*Xc))/(R1+(1j*Xc));
Z = Z2 + Z3; % Total Impedance Z
It = Vrms/Z; % Let Total Current = It
I3 = It*Z1/[R3 + Z1]; % Current in resistance R3 by Current
Division Rule
disp(['The current in load branch is: ',num2str(I3)]);
elseif (~R1)&&(~R3)
disp('The current in load branch is infinite')
else
disp('Load Current is equal to zero')
end

28
output: -

Circuit 2: without conditional statements


V= input('Enter ac voltage source(V): ');
f= input('Enter ac voltage source frequency(f): ');
R1= input('Enter series resistance(R1): ');
C1= input('Enter series capacitance(C1): ');
R2= input('Enter shunt resistance(R2): ');
L1= input('Enter shunt inductance(L1): ');
R3= input('Enter load resistance(R3): ');
w=2*pi*f;
Zl=1i*w*L1;
Zc=-1i/(w*C1);
Z1=(Zc*R1)/(Zc+R1);
Zi=R2+Zl;
Z2=(Zi*R3)/(Zi+R3);
Z=Z1+Z2;
It=V/Z;
Il=(It*Zi)/(Zi+R3);
disp(['The current in load branch is: ',num2str(Il)]);

29
Output: -

Circuit 2: Simulink

30
Output: -
Current in complete circuit:

31
Current in R1 branch: -

32
Current in R2,L1 branch

33
Current in R3 branch:

34
Current in C1 Branch:

35

You might also like