saksham MATLAB
saksham MATLAB
saksham MATLAB
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.
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
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.
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
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
disp('Result of w = z ≥ y:');
disp(w);
16
disp(u);
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;
figure;
plot(x1, sinc_x1, 'b-', 'LineWidth', 2);
title('sinc(x) = sin(x) / x');
xlabel('x');
ylabel('sinc(x)');
grid on;
hold off;
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;
figure;
plot(x1, sinc_x1, 'b-', 'LineWidth', 2);
title('sinc(x) = sin(x) / x');
xlabel('x');
ylabel('sinc(x)');
grid on;
hold off;
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;
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
% 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.
24
else
disp('Load Current is equal to zero')
end
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: -
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