Introduction to Computing
Lab Manual
Subject Code: EP-201
Subject Name: Introduction to Computing
Branch: Engineering Physics, 2nd Year (Third Semester)
Submitted by:
Vansh Gupta
2K23/EP/109
Submitted to:
Dr. Ajeet Kumar
Assistant Professor (Applied Physics)
Delhi Technological University Shahbad Daulatpur,
Main Bawana Road, Delhi-10042
INDEX
S.NO. CONTENT
1. Vision and Mission of DTU
2. Vision and Mission of Applied Physics
3. Experiment 1
Aim: Basics of Matrix operation and Matrix manipulation
4. Experiment 2
Aim: Program for Blackbody radiation and verify Wein's law
5. Experiment 3
Aim: Program to calculate the values of inbuilt defined
trigonometric functions using series solution approach
6. Experiment 4
Aim: Program to study of Gaussian and Lorentian function
7. Experiment 5
Aim: Program to perform Polynomial fitting to find coffecient
8. Experiment 6
Aim: Program to show the propagation of group wave as a
function of time
9. Experiment 7
Aim: Program to plot the intensity distribution of single slit,
double slit and n-slits.
10. Experiment 8
Aim: Program to find roots of equation by Bisection method
11. Experiment 9
Aim: Program to solve the second order Differential
Equation of Pendulum problem
12. Experiment 10
Aim: Program to calculate the maximum number of modes
supported by step index optical fiber
Vision and Mission of DTU
Vision:
To be a world class university through education, innovation, and research for the
service of humanity.
Mission:
To establish Centres of excellence in emerging areas of science, engineering,
technology, management and allied areas.
To foster an ecosystem for incubation, product development, transfer of
technology and entrepreneurship.
To create environment of collaboration, experimentation, imagination and
creativity.
To develop human potential with analytical abilities, ethics and integrity.
To provide environment friendly, reasonable and sustainable solutions for
local and global needs.
Department of Applied Physics
Vision and Mission
Vision:
Consolidating teaching and learning process covering all aspects of pure and applied
physics that promotes research and development leading to creation of new
knowledge, inventions and discoveries fostering institute-industry linkages and
entrepreneurial culture for betterment of all its stake holders and society at large.
Mission:
M1. To establish global and industry standards of excellence by generating new
knowledge in all the endeavours concerned to teaching, learning, research and
consultancy
M2. To help our students in developing human potentials, intellectual interest,
creative abilities and be lifelong learners to meet challenges of the national
and global environment and be true professional leaders.
M3. To stand up to the needs and expectations of our society by equipping and
training our students to be good citizens, aware of their commitments and
responsibilities, to make this world a better place to live
M4. To be a world class centre for education, research and innovation in the
various upcoming fields of Applied Physics
M5. To focus on the development of cutting-edge technologies and to foster an
environment of seamlessness between academia and industry
EXPERIMENT 1
AIM: Basics of Matrix operation and Matrix manipulation
Q1. Create 1D matrix
Code:
>> a = [1 2 3]
Output:
a =
1 2 3
Q2. Create two 2D matrix
Code:
>> a=[1 2 3; 4 5 6; 7 8 9]
>> b= [23 08 05; 30 01 04; 15 01 11]
Output:
a =
1 2 3
4 5 6
7 8 9
b =
23 8 5
30 1 4
15 1 1
Q3. To add matrix
Code:
>> c = b + a
Output:
c =
24 10 8
34 6 10
22 9 20
Q4. To perform Matrix subtraction
Code:
>> d=a-b
Output:
d =
-22 -6 -2
-26 4 2
-8 7 -2
Q5. To perform Matrix multiplication (row by row method)
Code:
>> e=a*b
Output:
e =
128 13 46
332 43 106
536 73 166
Q6. To perform Matrix multiplication (element by element)
Code:
f=a.*b
Output:
f =
23 16 15
120 5 24
105 8 99
Q7. To perform and print 20 values between 0 and 50 with equal
intervals
Code:
>> g=linspace(1,50,20)
Output:
g =
Columns 1 through 10
1.0000 3.5789
6.1579 8.7368 11.3158
13.8947 16.4737 19.0526
21.6316 24.2105
Columns 11 through 20
26.7895 29.3684
31.9474 34.5263 37.1053
39.6842 42.2632 44.8421
47.4211 50.0000
Q8. To perform and print values between 1 and 100 with equal intervals
Code:
>> h=1:5:100
Output:
h =
Columns 1 through 17
1 6 11 16 21 26 31 36 41 46 51
56 61 66
71 76 81
Columns 18 through 20
86 91 96
Q9. To perform Matrix division (row by row method)
Code:
>> i=a/b
Output:
i =
0.2536 -0.2931 0.2640
0.6403 -0.5925 0.4699
1.0270 -0.8919 0.6757
Q10. To perform Matrix division (element by element method)
Code:
>> j=a./b
Output:
j =
0.0435 0.2500 0.6000
0.1333 5.0000 1.5000
0.4667 8.0000 0.8182
Q11. To create zeros matrix of nth order (n=4)
Code:
>> a = zeros(4)
Output:
a =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Q12. To create ones matrix of nth order (n=4)
Code:
>> b = ones(4)
Output:
b =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Q13. To create index matrix of nth order (n=4)
Code:
>> c= eye(4)
Output:
c =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Q14. To create matrix and access the element
Code:
>> d= [1 2 3; 4 5 6; 7 8 9]
>> e= d(2,3)
Output:
d =
1 2 3
4 5 6
7 8 9
e =
6
Q15. To access the rows and column of the matrix
Code:
>> f=d(3,:)
>> g=d(:,2)
Output:
f =
7 8 9
g =
2
5
8
Q16. To access two or more rows and columns
Code:
>> h=d(:,2:3)
>> i = d(2:3,1:2)
Output:
h =
2 3
5 6
8 9
i =
4 5
7 8
Matrix Manipulation
Q17. To Change an element of the matrix
Code:
>>d(2,3)=23
Output:
d =
1 2 3
4 5 23
7 8 9
Q18. To Change the row of the matrix
Code:
>> d(3,:)=[10 11 12]
Output:
d =
1 2 3
4 5 23
10 11 12
Q19. To Change the column of the matrix
Code:
>> d(:,2)=[13; 14; 15;]
Output:
d =
1 13 3
4 14 23
10 15 12
Q20. To Change both the column and row of the matrix
Code:
>> d(:,2:3)=[23 09; 08 17; 05 9]
Output:
d =
1 23 9
4 8 17
10 5 9
Q21. To Change specific elements of rows and columns of the matrix
Code:
>> d(2:3,1:2)=[12 13; 13 12]
Output:
>> d=
1 23 9
12 13 17
13 12 9
EXPERIMENT 2
AIM: Write Matlab program for very famous Blackbody
radiation and verify Wein's displacement law.
Code:
clc
clear all
h = 6.626e-34;
c = 3e8;
k = 1.38e-23;
lamb = 1e-9:50e-9:2000e-9;
hold on
for T =3500:500:5000;
mew =(8 * pi * h* c)./((lamb.^5).*(exp((h * c)./(lamb * k * T))-
1));
[mmax,i] = max(mew);
lambmax = lamb(i);
S = lambmax .* T;
error = abs(S-2.8998e-3);
if error <=0.001
display('verified')
else
display('not verified')
end
plot(lamb,mew);
plot(lambmax, mmax, 'o', 'MarkerSize', 8, 'Markerfacecolor',
'r');
text(lambmax, mmax, ['T=', num2str(T), 'k'], 'Fontsize', 10);
end
xlabel('wavelength(in m)' , 'Fontsize', 10);
ylabel('energy density(J/m^3)' , 'Fontsize', 10);
title('Blackbody Radiation Energy versus Wavelength')
Output:
MATLAB Command Window
verified
verified
verified
verified
Graph:
EXPERIMENT 3
AIM: Write Matlab program to calculate the values of inbuilt defined
trigonometric functions using series solution approach. Compare the
results with inbuilt functions.
A. Code for sine function:
clc
clear all
x= input('Enter angle in radian: ');
fprintf(['value of sinx by inbuilt function: ',
num2str(sin(x)),'\n']);
sum_sin=0;
for n=1:1:5
sin_func = (((-1)^(n-1))*(x^(2*n-1)))/factorial(2*n-1);
sum_sin=sum_sin+sin_func;
end
fprintf(['value of sinx by series function: ',
num2str(sum_sin),'\n']);
error = abs(sum_sin-sin(x));
if error <=0.001
display('verified')
else
display('not verified')
end
Output:
B. Code for cosine function:
clc
clear all
x= input('Enter angle in radian: ');
fprintf(['value of cosx by inbuilt function: ',
num2str(cos(x)),'\n']);
sum_cos=0;
for n=1:1:5
cos_func = (((-1)^(n-1))*(x^(2*n-2)))/factorial(2*n-2);
sum_cos=sum_cos+cos_func;
end
fprintf(['value of cosx by series function: ',
num2str(sum_cos),'\n']);
error = abs(sum_cos-cos(x));
if error <=0.001
display('verified')
else
display('not verified')
end
Output:
EXPERIMENT 4
AIM: Write Matlab program to study of the behavior of Gaussian and
Lorentian function using all appropriate inbuilt 2d and 3d
plotting commands.
A. Code for Gaussian curve
(i) 2-dimension
clc
clear all
x= linspace(-2,2,100);
z=(1/(2*pi)*exp(-(x.^2/2)));
plot(x,z);
xlabel('x');
ylabel('z');
title('Gaussian curve');
Output:
(ii) 3-dimension
clc
clear all
x= linspace(-2,2,100);
y= linspace(-2,2,100);
[X,Y]= meshgrid(x,y);
Z=(1/(2*pi)*exp(-((X.^2+Y.^2)/2)));
surf(X,Y,Z);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Gaussian curve - 3d');
Output:
B. Code for Lorentz curve
(i) 2-dimension
clc
clear all
x= linspace(-2,2,100);
gamma=1;
z=(1/(pi))*(gamma./((x.^2) + (gamma^2)));
plot(x,z);
xlabel('x');
ylabel('z');
title('Lorentz curve');
Output:
(ii) 3-dimension
clc
clear all
x= linspace(-2,2,100);
y= linspace(-2,2,100);
[X,Y]= meshgrid(x,y);
gamma=1;
Z=(1/(pi))*(gamma./((X.^2) + (gamma^2)+(Y.^2)));
surf(X,Y,Z);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Lorentz curve - 3d');
Output:
EXPERIMENT 5
AIM: Write Matlab program to find out the unknown coefficients by
Polynomial fitting.
A. Code for Polynomial fitting at degree 2:
clc
clear all
format long e
x = [1 2 3 4 5 6 7 8];
y = [1 4 12 16 25 30 49 64];
degree = 2;
coefficients = polyfit(x,y,degree)
xi = linspace(1,8,1000);
yi = polyval(coefficients,xi);
plot(x,y,'o', 'Markersize',8,'Displayname', 'Data Points');
hold on;
plot(xi,yi,'-r', 'Linewidth',2, 'Displayname', 'Fitted Curve');
legend('show');
title(['Polynomial Fitting at degree ', num2str(degree)]);
xlabel('X');
ylabel('Y');
Output:
B. Code for Polynomial fitting at degree 3:
clc
clear all
format long e
x = [1 2 3 4 5 6 7 8];
y = [1 4 12 16 25 30 49 64];
degree = 3;
coefficients = polyfit(x,y,degree)
xi = linspace(1,8,1000);
yi = polyval(coefficients,xi);
plot(x,y,'o', 'Markersize',8,'Displayname', 'Data Points');
hold on;
plot(xi,yi,'-r', 'Linewidth',2, 'Displayname', 'Fitted Curve');
legend('show');
title(['Polynomial Fitting at degree ', num2str(degree)]);
xlabel('X');
ylabel('Y');
Output:
C. Code for Polynomial fitting at degree 5:
clc
clear all
format long e
x = [1 2 3 4 5 6 7 8];
y = [1 4 12 16 25 30 49 64];
degree = 5;
coefficients = polyfit(x,y,degree)
xi = linspace(1,8,1000);
yi = polyval(coefficients,xi);
plot(x,y,'o', 'Markersize',8,'Displayname', 'Data Points');
hold on;
plot(xi,yi,'-r', 'Linewidth',2, 'Displayname', 'Fitted Curve');
legend('show');
title(['Polynomial Fitting at degree ', num2str(degree)]);
xlabel('X');
ylabel('Y');
Output:
EXPERIMENT 6
AIM: Write a Matlab program to show the propagation of group wave
as a function of time
A. Code for position of Group Wave at 5 seconds:
clc
clear all
A=0.05;
w=2;
delw=0.002;
k=0.2;
delk=0.01;
t=5;
x=-500:1:500;
u=2*A*cos((w*t)-(k.*x)).*cos((delw*t)-(delk.*x));
plot(x,u);
xlabel('Displacement','FontSize',10,'FontWeight','Bold');
ylabel('Amplitude','FontName','Times New Roman');
title('Position of Group Wave at t=5s');
Output:
B. Code for position of Group Wave at 5, 10, 15 and 20 seconds:
clc
clear all
A=0.05;
w=2;
delw=0.002;
k=0.2;
delk=0.01;
x=-500:1:500;
t1=5;
t2=10;
t3=15;
t4=20;
u1=2*A*cos((w*t1)-(k.*x)).*cos((delw*t1)-(delk.*x));
u2=2*A*cos((w*t2)-(k.*x)).*cos((delw*t2)-(delk.*x));
u3=2*A*cos((w*t3)-(k.*x)).*cos((delw*t3)-(delk.*x));
u4=2*A*cos((w*t4)-(k.*x)).*cos((delw*t4)-(delk.*x));
subplot(2,2,1);
plot(x,u1);
xlabel('Displacement','FontSize',10,'FontWeight','Bold');
ylabel('Amplitude','FontName','Times New Roman');
title('Position of Group Wave at t=5s');
subplot(2,2,2);
plot(x,u2);
xlabel('Displacement','FontSize',10,'FontWeight','Bold');
ylabel('Amplitude','FontName','Times New Roman');
title('Position of Group Wave at t=10s');
subplot(2,2,3);
plot(x,u3);
xlabel('Displacement','FontSize',10,'FontWeight','Bold');
ylabel('Amplitude','FontName','Times New Roman');
title('Position of Group Wave at t=15s');
subplot(2,2,4);
plot(x,u4);
xlabel('Displacement','FontSize',10,'FontWeight','Bold');
ylabel('Amplitude','FontName','Times New Roman');
title('Position of Group Wave at t=20s');
sgtitle('Position of Group Wave at different Times');
Output:
C. Code for position of Group Wave at 5, 10, 15 and 20 seconds:
clc
clear all
A=0.05;
w=2;
delw=0.002;
k=0.2;
delk=0.01;
x=-500:1:500;
for t=0:1:20
u=2*A*cos((w*t)-(k.*x)).*cos((delw*t)-(delk.*x));
plot(x,u);
xlabel('Displacement','FontSize',10,'FontWeight','Bold');
ylabel('Amplitude','FontName','Times New Roman');
title(['Position of Group Wave at t=',num2str(t)]);
pause(0.2);
end
Output:
EXPERIMENT 7
AIM: Write a Matlab program to plot the intensity distribution of single
slit, double slit and n-slits.
A. Code for Intensity Distribution in Single Slit:
clc;
clear all;
e = 0.14 * 10^(-3);
A = 1;
theta = linspace(-60,60,100);
lamb = 5500 * 10^(-10);
alpha = (pi * e .*sind(theta))./lamb;
I = (A .* (sind(alpha)).^2)./alpha.^2;
plot(theta, I);
xlabel('Theta', 'Fontsize', 12,'FontName','times new roman');
ylabel('Intensity', 'Fontsize', 12,'FontName','times new
roman');
title('Intensity Distibution in Single Slit', 'FontSize', 16);
Output:
B. Code for Intensity Distribution in Double Slit:
clc;
clear all;
e = 0.14 * 10^(-3);
d = 3*e;
A = 1;
theta = linspace(-60,60,1000);
lamb = 5500 * 10^(-10);
alpha = (pi * e .*sind(theta))./lamb;
beta = (pi * (e+d).*sind(theta))./(lamb);
I = (A .* (sind(alpha)).^2).*((cosd(beta).^2)./(alpha).^2);
plot(theta, I);
xlabel('Theta', 'Fontsize', 12,'FontName','times new roman');
ylabel('Intensity', 'Fontsize', 12,'FontName','times new
roman');
title('Intensity Distibution in Double Slit', 'FontSize', 16);
Output:
C. Code for missing orders in Single Slit and Double Slit:
clc;
clear all;
e = 0.14 * 10^(-3);
d = 3*e;
A = 1;
theta = linspace(-60,60,1000);
lamb = 5500 * 10^(-10);
alpha = (pi * e .*sind(theta))./lamb;
I = (A .* (sind(alpha)).^2)./alpha.^2;
plot(theta, I);
hold on;
beta = (pi * (e+d).*sind(theta))./(lamb);
I2 = (A .* (sind(alpha)).^2).*((cosd(beta).^2)./(alpha).^2);
plot(theta, I2);
xlabel('Theta', 'Fontsize', 12,'FontName','times new roman');
ylabel('Intensity', 'Fontsize', 12,'FontName','times new
roman');
title('Intensity Distibution in Single & Double Slit',
'FontSize', 16);
gtext('Missing Order is 4m');
gtext('Missing Order is 4m');
Output:
D. Code for Intensity Distribution in N Slit:
clc;
clear all;
e = 0.14 * 10^(-3);
d = 3*e;
A = 1;
n=4;
theta = linspace(-60,60,1000);
lamb = 5500 * 10^(-10);
alpha = (pi * e .*sind(theta))./lamb;
beta = (pi * (e+d).*sind(theta))./(lamb);
I= (A .*
(sind(alpha)).^2).*(sind(n*beta).^2)./(((alpha).^2).*(sind(beta)
.^2));
plot(theta, I);
xlabel('Theta', 'Fontsize', 12,'FontName','times new roman');
ylabel('Intensity', 'Fontsize', 12,'FontName','times new
roman');
title('Intensity Distibution in n Slit', 'FontSize', 16);
Output:
E. Analysing the change in diffraction pattern by increasing the slit
width by ten/hundred times:
When we decrease the width of slit in case of double slit diffraction,
then the diffraction stars to minimize and interference starts to dominate,
hence the intensity distribution takes the form of interference pattern.
Width of the fringe becomes equal in interference, while in
diffraction, they are not equal.
The intensity of all positions of maxima are of the same intensity
whereas in diffraction they do vary.
The observation in intensity distribution has shown below.
EXPERIMENT 8
AIM: Write a Matlab program to find the roots of the equation by
Bisection method
A. Code for Use of Inbuilt function “roots”:
clc
clear all
A=[3 -4 3 2 -2 5];
b = roots(A)
Output:
B. Code for Use of Inbuilt function “fzero”:
clc
clear all
fx = @(x)(x^3)-(5*x)+1;
z = fzero(fx,[0,1])
Output:
C. Code to find Roots of a Polynomial Equation using Bisection
Method:
clc
clear all
a = @(x) (x^3)-8*(x^2)+19*x-12;
% It creates a polynomial equation in x
minval = 1;
maxval = 10;
nosteps = 1000;
if(a(minval)*a(maxval) > 0)
display('no roots')
else
for i = 1:nosteps
midval = (minval+ maxval)/2;
if(a(midval)*a(maxval)>=0)
maxval = midval;
else
minval = midval;
end
end
end
fprintf("The root is %f\n",midval)
Output:
D. Code to find Roots of a Transcendental Equation using Bisection
Method:
clear all
clc
a= @(x) tan(x)-(x);
minval= input('enter the minimun value');
maxval= input('enter the maximun value');
nosteps=1000;
if(a(minval)*a(maxval)>0)
disp("no roots")
else
for i=1:nosteps
midval=(minval+maxval)/2;
if ((a(midval)*a(maxval))>=0)
maxval=midval;
else
minval=midval;
end
end
end
fprintf ("the root is%f\n",midval)
Output:
E. Code to find Multiple Roots of a given Equation using
Bisection method:
clc
clear all
a = @(x) (x^3)-8*(x^2)+19*x-12;
minval = input("Enter the Minimum value\n");
maxval = input("Enter the Maximum value\n");
nosteps = 100000;
incre = (maxval-minval)/nosteps;
for i =1:nosteps
if(a(minval)*a(minval+incre))<0
midval = ((minval)+(minval+incre))/2;
fprintf("The Root is %f\n",midval);
else
end
minval = minval+incre;
end
Output:
EXPERIMENT 9
AIM: Write a Matlab program to solve the second order Differential
Equation of Pendulum problem
A. Code for First Order Differential Equation:
%FUNCTION FILE
function z = firstorder(x,t)
z=x+t;
end
%CODE
clc
clear all
tspan=[0,2];
x0=0;
[t,x] = ode23("firstorder", tspan, x0);
plot(t,x);
xlabel('x', 'Fontsize', 12,'FontName','times new roman');
ylabel('Time', 'Fontsize', 12,'FontName','times new roman');
title('Ordinary Differential Equation', 'Fontsize',
18,'FontName','times new roman');
Output:
B. Code for Simultaneous Differential Equation:
%FUNCTION FILE
function zdot = simul_func(t,z)
udot = -2*z(1) + 4*z(2);
vdot = 4*z(1) - 3*z(2);
zdot = [udot; vdot];
end
%CODE
clc
clear all
tspan =[0,2];
u0 = 0.5;
v0 = 1;
z0 = [u0 ; v0];
[t,z] = ode23("simul_func", tspan, z0);
plot(t,z(:,1), t,z(:,2));
xlabel('z', 'Fontsize', 12,'FontName','times new roman');
ylabel('Time', 'Fontsize', 12,'FontName','times new roman');
title('Simultaneous Differential Equation', 'Fontsize',
18,'FontName','times new roman');
Output:
C. Code for Simultaneous Differential Equation of Pendulum
Problem:
%FUNCTION FILE
function z_dot = pendulum_func(t,z) % Creates a function file
l = 1;
theta_dot = z(2);
phi_dot = -(9.8/l)*sin(z(1));
z_dot = [theta_dot;phi_dot];
end
%CODE
clc;
clear all;
tspan = [0,10];
theta_0 = pi/4;
phi_0 = 0;
z_0 = [theta_0,phi_0];
[t,z] = ode23("pendulum_func",tspan,z_0);
plot(t,z(:,1),t,z(:,2))
xlabel('angle (radian)', 'Fontsize', 12,'FontName','times new
roman');
ylabel('Amplitude', 'Fontsize', 12,'FontName','times new
roman');
title('Differential Equation of Pendulum', 'Fontsize',
16,'FontName','times new roman');
Output:
EXPERIMENT 10
AIM: Write a Matlab program to calculate the maximum number of
modes supported by step index optical fiber
A. Code for the maximum number of modes
clc;
clear all;
a_um = input('Enter the core radius of the fiber (in
micrometers): ');
lambda_um = input('Enter the input wavelength (in micrometers):
');
n_core = input('Enter the refractive index of the core: ');
n_cladding = input('Enter the refractive index of the cladding:
');
a = a_um * 1e-6;
lambda = lambda_um * 1e-6;
V = (2 * pi * a / lambda) * sqrt(n_core^2 - n_cladding^2);
fprintf('The V-parameter of the optical fiber is: %.3f\n', V);
if V < 2.405
fprintf('The fiber operates in single-mode.\n');
M = 1
else
fprintf('The fiber operates in multimode.\n');
M = V^2 / 2;
fprintf('The number of modes supported by the fiber is
approximately: %.2f\n', M);
end
Output: