0% found this document useful (0 votes)
111 views45 pages

MATLAB for Engineering Students

The document describes experiments conducted using MATLAB to familiarize with scientific computing tools and functions. In Experiment 1, programs are written to check if a number is prime, add matrices, find transpose of a matrix, and perform element-wise matrix multiplication. The objective is to familiarize with MATLAB. In Experiment 2, functions like abs, complex, exponential forms, sinc, and plotting sine waves are used with examples to familiarize different scientific computing functions. In Experiment 3, arrays, matrices, plotting, and GUI are used to realize voltage-current-power calculations, find roots of quadratic equations, and plot Bode plots of filters - familiarizing with array and matrix operations in MATLAB.

Uploaded by

Adarsh H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views45 pages

MATLAB for Engineering Students

The document describes experiments conducted using MATLAB to familiarize with scientific computing tools and functions. In Experiment 1, programs are written to check if a number is prime, add matrices, find transpose of a matrix, and perform element-wise matrix multiplication. The objective is to familiarize with MATLAB. In Experiment 2, functions like abs, complex, exponential forms, sinc, and plotting sine waves are used with examples to familiarize different scientific computing functions. In Experiment 3, arrays, matrices, plotting, and GUI are used to realize voltage-current-power calculations, find roots of quadratic equations, and plot Bode plots of filters - familiarizing with array and matrix operations in MATLAB.

Uploaded by

Adarsh H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

SCIENTIFIC

COMPUTING
LABORATORY
(With MATLAB)
ECL201

Name:
Reg No:
College of Engineering
Muttathara
EXP NO: 1 DATE:

FAMILIARIZATION OF THE COMPUTING TOOL

OBJECTIVE:
To familiarize with the scientific computing tool using MATLAB or octave.

1. program to check whether a number is prime or not using while loop


program:
clc;
clear all;
close all;
n=input('enter number :
'); i=2;
while i<=n/2
if mod(n,i)==0
flag=1;
break;
end
i=i+1;
end
if n==1
disp('neither prime nor
composite'); else
if flag==1
disp('not
prime'); else
disp('prime');
end
end
output:
enter number : 4
not prime

2. program to add two matrix.


Program:
clc;
clear all;
close all;
a=[1 3;4 5;7 9];
b=[3 4;4 3;5 6];
c=a+b;
disp (c)
output:
4 7
8 8
12 15

3. program to find transpose of a matrix


program:
clc;
clear all;
close all;
a=[1 3;4 5;7 9];
c=a';
disp (c)
output:
1 4 7
3 5 9

4. program to display the second row third column element of a 3x3 matrix as the
output
program:
clc;
clear all;
close all;
a=[1 3 3;4 5 6;7 8 9]; a(2,3)
output:
ans = 6

5. program to do element by element multiplication of 2 matrices


program:
clc;
clear all;
close all;
a=[1 3 3;4 5 6;7 8 9];
b=[1 5 2;2 2 3; 1 2 3];
a.*b
output:
ans =
1 15 6
8 10 18
7 16 27

INFERENCE:
Familiarized with MATLAB which is basically a scientific computing tool
based on simulations.
EXP NO: 2 DATE:

FAMILIARIZATION OF SCIENTIFIC COMPUTING

OBJECTIVE:
To familiarize different functions for scientific computing with examples

1. Generate the follow ing output using the function ‘abs’.

program:
%plot absolute value from -3 to
3 x=[-3;0.1;3] %array fro -3 to 3
y=abs(x); %absolute value in y
subplot(2,2,1);
plot(x,y)
xlabel('x');
ylabel('y');
subplot(2,2,2);
m =-abs(x);
plot(x,m,'c')
xlabel('x');
ylabel('y');
subplot(2,2,3);
n=abs(x);
plot(x,n)
hold on;
b=-abs(x);
plot(x,b);
hold off;
xlabel('x');
ylabel('y');
2. Create a complex number 5+4i, extract the real and imaginary parts and compute
the magnitude of the vector using built in functions.
Program:
z=complex(5,4);
disp('Real Part');
a=real(z)
disp('Imaginary Part');
b=imag(z)
disp('Magnitude');
abs(z)

output:
Real Part = 5
Imaginary Part = 4
Magnitude = 6.4031

3. Represent the complex exponential form epi+ i as rectangular form and find the
real part, magnitude and the angle of the vector (both in degree and radians).
PROGRAM:
z=exp(pi*i);
disp('Complex number in rectangular form');
disp('Real Part');
a=real(z)
disp('Magnitude');
mag=abs(z)
disp('Angle in radians');
ang=angle(z)
disp('Angle in degree');
ang_deg=ang*(180/pi)

output:
Complex number in rectangular
form z = -1.0000e+0.0000i
Real
Part a =
-1
Magnitude
mag = 1
Angle in radians
ang = 3.1416
Angle in degree
ang_deg = 180

4. Plot the follow ing :

program:
x=[-5:0.001:5];
y=sinc(x);
subplot(2,2,1)
plot(x,y)
t=-9:0.01:9;
y=sin(t);
x=cos(t);
subplot(2,2,2)
plot(x,y)
t1=0:0.001:1
y=exp(-5*t1);
subplot(2,2,3)
plot(t1,y);

5. Plot sinew aves of frequency 50 and 100 Hz on the same figure window.
program:
f=50;
t=0:0.0002:0.1;
y=sin(2*pi*f*t);
plot(t,y);
grid on;
hold on;
f=100;
t=0:0.0002:0.1;
y=sin(2*pi*f*t);
plot(t,y);
hold off;

OUTPUT
6. Replace the given code w ith vectorized code for fast
computing (a)This code computes the sine of 1,001 values
ranging from 0 to 10:
clc;
clear all;
close all;
i = 0;
for t = 0:.01:10 i
= i + 1;
y(i) = sin(t);
end
disp(y);
(b)
clc;
clear all;
close all;
r=[1:4];
h=[1:4];
for i=1:4
volume(i) = pi*r(i)*r(i)*h(i); end
disp(volume);

PROGRAM:
(a)
t = 0:.01:10;
y = sin(t);
disp(y);
(b)
clc;
clear all;
close all;
r=[1:4];
h=[1:4];
volume = pi*r.^2.*h;
disp(volume);

7. Execute a script (.m file) to obtain the dot product and the cross product of tw o
vectors a and b, w here a = (1 5 6) and b = (2 3 8).
PROGRAM:
a = [1 5 6];
b = [2 3 8];
dotpro=dot(a,b)
crosspro=cross(a,b)

output:
dotpro = 65
crosspro =
22 4 -7

8. Simplify the expression and express the complex number in rectangular and polar
form (a.) y =0.5+ j 6+3.5 e j0.6+(3+ j 6)e j0.3 pi .
(3+ j 4)(5+ j 2)(2<60)
(b.) z=
(3+ j 6)(1+ j 2)
(a.) PROGRAM:
clc;
clear all;
close all;
Z1 = 0.5;
Z2 =6*j;
Z3 = 3.5*exp(j*0.6); Z4
= 3+6*j;
Z5 = exp(j*0.3*pi);
disp('Z in rectangular form is');
Z_rect = Z1+Z2+Z3+(Z4*Z5);
Z_rect
Z_mag = abs (Z_rect); % magnitude of Z
Z_angle = angle(Z_rect)*(180/pi); % Angle in degrees
disp('complex number Z in polar form, mag, phase');
Z_polar = [Z_mag, Z_angle]

OUTPUT:
Z in rectangular form is
Z_rect =0.2979 +13.9300i
complex number Z in polar form, mag, phase
Z_polar =13.9332 88.7748

(b) PROGRAM
clc;
clear all;
Z1=3+4*j;
Z2=5+2*j;
theta = 60*(pi/180); % angle in radians
Z3 = 2*exp(j*theta);
Z4 = 3+6*j;
Z5 = 1+2*j;
disp('Z in rectangular form is');
Z_rect = Z1*Z2*Z3/(Z4*Z5);
Z_rect
Z_mag = abs (Z_rect); % magnitude of Z
Z_angle = angle(Z_rect)*(180/pi); % Angle in degrees
disp('complex number Z in polar form, mag, phase');
Z_polar = [Z_mag, Z_angle]

OUTPUT:
Z in rectangular form is
Z_rect =3.5546 + 0.5035i
complex number Z in polar form, mag, phase
Z_polar = 3.5901 8.0616

9. The voltage across a capacitor


is v(t)= 10(1−e−0.2t)
Plot voltage v (t), versus time, t, for t = 0 to 50 seconds w ith increment of 5 s. Do not use
loops.
PROGRAM:
clc;
clear all;
close all;
t=0:5:50
v=10*(1-exp(-0.2*t));
plot(t,v)
xlabel('Time')
ylabel('Voltage')
plot:

INFERENCE:
Familiarized with basic arithmetic functions for scientific computing and used
vectorized computing for fast scientific applications.
EXP NO: 3 DATE:

REALIZATION OF ARRAYS AND MATRICES

OBJECTIVE:
To familiarize with the realization of arrays and matrixes and their visualization
using plotting functions and GUI.

1. The voltage, v, across a resistance is given as (Ohm’s Law ), v=iR , w here i is the
current and R the resistance. The pow er 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 w ith increments of 2A, w rite a
MATLAB program to generate a table of current, voltage and pow er 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 follow ing 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 w ith 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 1+ j 1 2− j 2
w= [ ]
3+ j 2 4+ j 3
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 row s 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 amatrixof 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 w hether 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+2x 3=0; 2x1-x 2+3x3 =0; 3x1-5x2+4x3=0; x1+17x2+4x 3 = 0
Verify your answ er 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');
en
d
en
d disp('The system has no solution');
els
e

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, w here 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

(
−2 2 −3
11. For the matrix A = 2 1 −6 Show that the eigen values are the roots of the
−1 −2 0
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 w ith the help of singular value
decomposition of A as:

r
𝐀𝐀^ = ∑ ʎ𝐢𝐢𝐢𝐢𝐢𝐢 𝑽𝑽𝑽𝑽𝑽𝑽
i=0
w here Ui and Vi are the singular vectors and ʎi are the eigen values w ith ʎi<ʎjfor i> j. Plot
N N
the absolute error(ζ) betw een A and A^ as ζ = ∑∑|ai , j−ai , j|2 against r for r = 10, 50,
i=1 j=1
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.find the solution of system of linear equations.
2x-3y+4z =- 2
x+y-3z = 5
y+5z = 12
PROGRAM:
%solution of system of linear equations
eqn1 = input("Enter the coefficent of matrix A :");
if det(eqn1)==0
display("no solution!")
else
eqn2 = input("Enter the coefficent of matrix B :");
sol = inv(eqn1)*eqn2';
sol
en
d

OUTPUT:
Enter the coefficent of matrix A
: [2,-3,4;1,1,-3;0,1,5]
Enter the coefficent of matrix B
: [-2,5,12]
sol =
3.9714
5.1429
1.3714

14.find rank,trace and eigen values of the


matrix A = [ 2,4,6 ; 3,5,7; 1,2,3 ]
PROGRAM:
%find the rank,trace and eigen values of a
matrix mat1 = input("Enter the matrix : ");
rank(mat1)
trace(mat1)
eign = eig(mat1)

OUTPUT:
Enter the matrix : [2,4,6 ; 3,5,7 ; 1,2,3]
ans = 2
ans = 10
eign =
1.0099e+01
-9.9020e-
02 3.8139e-
16
EXP NO: 4 DATE:

NUMERICAL DIFFERENTIATION AND INTEGRATION

OBJECTIVE:
● To perform numerical differentiation and integration

1. plot sin(t), t is from -pi to pi w ith 0.001 increment.


program:
%plot sin from -pi to pi
t = [-pi:0.01:pi]
a = sin(t)
figure;
stem(t,a)
ylabel("sint");
xlabel("t");
figure;
plot(t,a)
ylabel("sint");
xlabel("t");

output:

plot:
stem:

2. plot sin(t), cos(t), sinh(t), cosh(t) . t is from 0 to 10 w ith 0.001 increment.


PROGRAM:
%realize sin(t), cos(t), sinh(t), cosh(t)
%for t from 0 to 10 with increment of
0.01 t = [0:0.01:10];
a = sin(t);
b = cos(t);
plot(t,a,'g',t,b,'r');
figure;
c = sinh(t);
d = cosh(t);
plot(t,c,t,d,'g');

OUTPUT:

plot:
a) sin(t) , cos(t)
b) sinh(t) , cosh(t): both has same graph.

3. Compute the first and second derivatives of these functions using built in tools
such as grad.
PROGRAM:
t=0:0.01:10;
a=sin (t);
subplot(3,3,1)
plot(t,a)
title('sint')
b=cos (t); c=
sinh (t);
d=cosh (t);
da=gradient(a);
subplot(3,3,2)
plot(t,da)
title('first derivative of sint')
d2a=gradient(da);
subplot(3,3,3)
plot(t,d2a)
title('second derivative of
sint') db=gradient(b);
subplot(3,3,4)
plot(t,db)
title('first derivative of cost')
d2b=gradient(db);
subplot(3,3,5)
plot(t,d2b)
title('second derivative of cost')
dc=gradient(c);
subplot(3,3,6)
plot(t,dc)
title('first derivative of sinht')
d2c=gradient(dc);
subplot(3,3,7)
plot(t,d2c)
title('second derivative of sinht')
dd=gradient(d);
subplot(3,3,8)
plot(t,dd)
title('first derivative of
cosht') d2d=gradient(dd);
subplot(3,3,9)
plot(t,d2d)
title('second derivative of cosht')

OUTPUT:

4. Compute the first and second derivatives of sint, cost, sinht, cosht functions using
built in tools such as grad and plot the derivatives over the respective functions for
the vector
t = [-5, 5] w ith increment 0.01.
PROGRAM:
t=-5:0.01:5;
a=sin (t);
subplot(3,3,1)
plot(t,a)
title('sint')
b=cos (t); c=
sinh (t);
d=cosh (t);
da=gradient(a);
subplot(3,3,2)
plot(t,da)
title('first deri of sint')
d2a=gradient(da);
subplot(3,3,3)
plot(t,d2a)
title('second deri of sint')
db=gradient(b);
subplot(3,3,4)
plot(t,db)
title('first deri of cost')
d2b=gradient(db);
subplot(3,3,5)
plot(t,d2b)
title('second deri of cost')
dc=gradient(c);
subplot(3,3,6)
plot(t,dc)
title('first deri of sinht')
d2c=gradient(dc);
subplot(3,3,7)
plot(t,d2c)
title('second deri of sinht')
dd=gradient(d);
subplot(3,3,8)
plot(t,dd)
title('first deri of cosht')
d2d=gradient(dd);
subplot(3,3,9)
plot(t,d2d)
title('second deri of cosht')

OUTPUT:
5. Familiarise numerical integration tools used in matlab
a. Create the function f(x)=e−x (lnx)2. Evaluate integral from 0 to infinity
PROGRAM:
fun = @(x) exp(-x.^2).*log(x).^2; q
= integral(fun,0,Inf)
q = 1.9475

b. Create the function f(x)=1/(x3−2x−c) w ith one parameter, c. Evaluate the


integral from x=0 to x=2 at c=5.
PROGRAM:
fun = @(x,c) 1./(x.^3-2*x-c);
q = integral(@(x) fun(x,5),0,2) q
= -0.4605

c. Create the function f(x)=ln(x). Evaluate the integral from x=0 to x=1
PROGRAM:
fun = @(x)log(x);
q1 = integral(fun,0,1)
q1 = -1.000000

6. Realize the function f(t) =4t 2 +3 and plot it for the vector [-5,5] w ith increment 0.01.
PROGRAM:
t=-5:0.01:5;
y=4*(t.^2)+3;
plot(t,y,'k','linewidth',2)
xlabel't'
ylabel'f(t)'

OUTPUT:
PLOT:

7. Use general integration tool to compute


2
∫ ( t +2)dt
−2
Using general integration tool
PROGRAM:
clc;
clear all;
fun = @(t) t + 2;
q = integral(fun,-2,2)

OUTPUT:
q=8

8. Repeat using trapezoidal method and Simpsons method.


PROGRAM:
t=-2:.5:2;
y=t+2;
q=trapz(t,y)

OUTPUT:
q=8

TRAPEZOIDAL USING LOOP


PROGRAM:
clc;
clear all;
f=@(x)x+2; %Change here for different function
a=input('Enter lower limit a: '); % exmple a=1
b=input('Enter upper limit b: '); % exmple b=2
n=input('Enter the no. of subinterval: '); % exmple n=10
h=(b-a)/n;
sum=0;
for k=1:1:n-1
x(k)=a+k*h;
y(k)=f(x(k));
sum=sum+y(k);
end % Formula: (h/2)*[(y0+yn)+2*(y2+y3+..+yn-1)] answer=h/2*(f(a)+f(b)+2*sum);
fprintf('\n The value of integration is %f',answer);

OUTPUT:
answer=8

SIMPSON USING LOOP


PROGRAM:
clc;
clear all;
f=@(x)x+2; %Change here for different function
a=input('Enter lower limit a: '); % exmple a=-2
b=input('Enter upper limit b: '); % exmple b=2
n=input('Enter the number of sub-intervals n: '); % exmple
n=16 h=(b-a)/n;
for k=1:1:n
x(k)=a+k*h;
y(k)=f(x(k));
end
so=0;
se=0;
for k=1:1:n-1 if
rem(k,2)==1
so=so+y(k);%sum of odd terms
else
se=se+y(k); %sum of even terms
end
terms)] end % Formula: (h/3)*[(y0+yn)+4*(y3+y5+..odd term)+2*(y2+y4+y6+...even

answer=h/3*(f(a)+f(b)+4*so+2*se);
fprintf('\n The value of integration is %f',answer);

1 inf − x2
9. Comput
√2 pi ∫0e using above three methods
2

e
i) PROGRAM:
t=0:.5:1000;
yi=@(t)exp((-t.^2)/2); qi
= integral(yi,0,1000)
answeri=(1/sqrt(2*pi))*qi

OUTPUT:
answeri= 0.500

ii) PROGRAM:
t=0:.5:1000;
y=exp((-t.^2)/2);
q=trapz(t,y);
answer= (1/sqrt(2*pi))*q

OUTPUT:
answer= 0.500

iii) PROGRAM:
clc;
clear all;
f=@(x)exp((-x.^2)/2); %Change here for different function
a=input('Enter lower limit a: '); % exmple a=1 b=input('Enter
upper limit b: '); % exmple b=2
n=input('Enter the number of sub-intervals n: '); % exmple
n=16 h=(b-a)/n;
for k=1:1:n
x(k)=a+k*h;
y(k)=f(x(k));
end
so=0;se=0; for
k=1:1:n-1 if
rem(k,2)==1
so=so+y(k);%sum of odd terms
else
se=se+y(k); %sum of even
terms end
end
% Formula: (h/3)*[(y0+yn)+2*(y3+y5+..odd term)+4*(y2+y4+y6+...even
terms)]
q=h/3*(f(a)+f(b)+4*so+2*se);
answer= (1/sqrt(2*pi))*q
fprintf('\n The value of integration is %f',answer);
OUTPUT:
Enter lower limit a: 0
Enter upper limit b: 1000
Enter the number of sub-intervals n:
1000 answer =
0.4976

10. plot the graph of e^t, e^2t,e^3t.


Program:
%plot e^t, e^2t,e^3t
t=[0:0.001:10];
a = exp(t);
b = exp(2*t);
c = exp(3*t);
subplot(2,2,1)
plot(t,a,'r');
ylabel("e^t");
xlabel("t");
subplot(2,2,2)
plot(t,b,'b');
ylabel("e^2t");
xlabel("t");
subplot(2,2,3)
ylabel("e^3t");
xlabel("t");
plot(t,c,'c');

OUTPUT:
11. Realize the function f(t)=4t 2+3 and plot t = (-5,5) w ith increment 0.01.
program:
t = -5:0.01:5;
f1 = 4.*t.*t+3;
plot(t,f1);
ylabel("f(t)");
xlabel("t");

OUTPUT:
plot:

12. Realize the function f(t)=4t 2+3 and plot ⃗t = (-5,5) w ith increment 0.01, also plot
sin(t) and cos(t) in subplots.

Program:
t = -5:0.01:5;
f1 = 4.*t.*t+3; f2
= sin(t);
f3 = cos(t);
f4 = sinh(t);
subplot(2,2,1);
plot(t,f1,'b');
subplot(2,2,2);
plot(t,f2,'r')
subplot(2,2,3);
plot(t,f3,'g')
subplot(2,2,4);
plot(t,f4)

OUTPUT:
plot:
13. Use general integration tool and find : ∫ f (t ) .dt w here f(t)=3t 2+5.
PROGRAM:
f = @(t) 3.*(t.^2)+5; Q =
integral(f,-2,2)

OUTPUT:
Q = 36.000

1 ∞
− x2
14. compute e 2
dt using general integration tool.
√2 π ∫0
PROGRAM:
f = @(x) exp(-(x.^2)/2); fn
= integral(f,0,inf); sol =
(1/sqrt(2*pi))*fn

SOLUTION:
sol = 0.5000

15. Represent the complex exponential form epi∗i as rectangular form and find
the real part, magnitude and the angle of the vector (both in degree and radians).
program:
%Represent the complex exponential form epi∗i as rectangular form
and find the real part, magnitude and the angle of the vector (both in degree and
radians).
z=exp(pi*i);
disp('Complex number in rectangular form'); z
disp('Real Part'); a=real(z)
disp('Magnitude');
mag=abs(z) disp('Angle
in radians'); ang=angle(z)
disp('Angle in degree');
ang_deg=ang*(180/pi)

output:
Angle in radians
ang = 3.1416 Angle
in degree
ang_deg = 180
EXP NO: 5 DATE:

SOLUTION OF ORDINARY DIFFERENTIAL EQUATION

OBJECTIVE:
● To solve ordinary differential Equation
1. Solve the first order differential equation
dx
+2 x=0
dt
With initial condition x(0)=1
PROGRAM:
syms x(t);
eqn= diff(x,t)+ 2*x ==0;
cond= x(0)==1;
soln= dsolve(eqn,cond)
t=0:0.05:10
s=subs(soln)
plot(s)
title('Response of firt order differential
equation') xlabel('time')

OUTPUT:
soln =exp(-2*t)
2. Solve second order differential equation
d x dx
2
−t
2 +2 +2 x =e
dt dt
PROGRAM:
syms x(t) ;
eqn= diff(x,t,2) + 2*diff(x,t) + 2*x == exp(-t); soln=
dsolve(eqn);
soln=simplify(soln)
OUTPUT
soln =
exp(-t)*(C4*cos(t) + C5*sin(t) + 1)

3. SOLVE THE DIFFERENTIAL EQUATION


d2 y
2 =cos (2∗x )− y
dt
y(0)=1,y’(0)=0
PROGRAM:
syms y(x)
Dy = diff(y);
ode = diff(y,x,2) == cos(2*x)-y; cond1
= y(0) == 1;
cond2 = Dy(0) == 0; conds
= [cond1 cond2]; ySol =
dsolve(ode,conds); ySol =
simplify(ySol)

OUTPUT:
1 – (8*sin(x/2)^4)/3

4. Solve for the current transient through an RC netw ork (w ith RC = 3)that is driven by
(a) 5V DC
(b) The signal 5𝑒𝑒 −𝑡𝑡 U(t) and plot the solutions.
(a) PROGRAM:
clc;
clear all;
close all;
symsi(t);
V=5; R=3;
C=1;
eqn= diff(i,t)+ i/(R*C) ==0; cond=
i(0)==V/R;
soln= dsolve(eqn,cond)
t=0:.05:10;
s=subs(soln); plot(t,s,'k',
'linewidth',2)
title('Current Transient of RC circuit');
xlabel('time');
ylabel('Current through capacitor');

OUTPUT:
soln =

(5*exp(-t/3))/3
PLOT:

(b)PROGRAM:
clc;
clear all;
close all;
syms i(t);
V=5*exp(-t);
R=3;
C=1;
eqn= diff(i,t)+ i/(R*C) ==diff(V,t)/R;
cond= i(0)==5/R;
soln= dsolve(eqn,cond)
t=0:.05:10;
s=subs(soln); plot(t,s,'k',
'linewidth',2)
title('Current Transient of RC circuit');
xlabel('time');
ylabel('Current through capacitor');

OUTPUT:
soln =

-(5*exp(-t)*(exp((2*t)/3) - 3))/6
5. Solve for the voltage across the capacitor of an RC netw ork that is driven by5V
DC, w ith three different time constants.
PROGRAM:
clc;
clear all;
close all;
syms vc(t);
V=5;
R1=3; %RC time constant = 3
C1=1;
TC1=R1*C1;%Time constant
eqn= diff(vc,t)==(V-vc)/(TC1);
cond= vc(0)==0;
soln1= dsolve(eqn,cond) TC2=2*TC1;%2
times the Time constant eqn=
diff(vc,t)==(V-vc)/(TC2);
soln2= dsolve(eqn,cond) TC3=3*TC1;%3
times the Time constant eqn=
diff(vc,t)==(V-vc)/(TC3);
soln3= dsolve(eqn,cond)
t=0:.05:20;
s1=subs(soln1);
s2=subs(soln2);
s3=subs(soln3);
plot(t,s1, 'r', 'linewidth',2);
hold on;
plot(t,s2, 'g', 'linewidth',2);
hold on;
plot(t,s3, 'k', 'linewidth',2);
legend('RC','2*RC','3*RC');
title('Voltage Transient of RC circuit');
xlabel('time');
ylabel('Voltage across capacitor');

OUTPUT:
soln1 =
5 - 5*exp(-t/3)
soln2
5 - 5*exp(-t/6)
=
PLOT: 5 – 5*exp(-t/9)
soln3

6. Solve the current transient through a series RLC circuit w ith R = 9, L = 1H and C =
0.05 F that is driven by
(a) 20 V DC
(b) The signal 20𝑒𝑒 −𝑡𝑡 U(t) and plot the solutions
(a) PROGRAM:
clc;
clear all;
close all;
syms i(t);
V=20;
R1=9;
L1=1;
C1=0.05;
Di=diff(i);
eqn1= diff(i,t,2)+(R1/L1)*diff(i,t)+(1/(L1*C1))* i ==0;
cond1=[i(0)==0, Di(0)==20];
soln1= dsolve(eqn1, cond1);
t=0:.005:5;
s1=subs(soln1); plot(t,s1,'r',
'linewidth',2);
title('Current Transient through RlC circuit');
xlabel('time');
ylabel('Current through the circuit');
OUTPUT:

(b) PROGRAM:
clc;
clear all;
close all;
syms i(t);
V=20*exp(-t);
R1=9;
L1=1; C1=0.05;
Di=diff(i);
eqn1= diff(i,t,2)+(R1/L1)*diff(i,t)+(1/(L1*C1))* i ==(1/L1)*diff(V,t);
cond1=[i(0)==0, Di(0)==20];
soln1= dsolve(eqn1, cond1);
t=0:.005:5;
s1=subs(soln1); plot(t,s1,'r',
'linewidth',2);
title('Current Transient through RlC circuit');
xlabel('time');
ylabel('Current through the circuit');

OUTPUT:
EXP NO: 6 DATE:

SIMPLE DATA VISUALISATION

OBJECTIVE:
● To visualize the data in different ways
1. Plot a cosine signal added w ith random noise using scatter plot
PROGRAM:
x = 0:0.01:10;
len=length(x);
y = cos(x) + rand(1,len); sz
= 25;
c = 0:0.01:10;
scatter(x,y,sz,c,'filled')
title('Scatter plot: Cosine signal added with a random noise')

OUTPUT:

2. Plot some random data using scatter plot


PROGRAM:
x = rand(1000,1); y
= rand(1000,1);
s = scatter(x,y,[],'k');

OUTPUT:
3.Plot the bar plot of the data y= [75 91 105 123.5 131 150 179 203 226 249 281.5] having
values on the x axis as [1900:10:2000].
PROGRAM:
x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x,y)

OUTPUT:
4. Plot the bar plot of the matrix

(112 6
22 32
9

Against x as [1980 1990]


PROGRAM:
x = [1980 1990];
y = [2 6 9;11 22 32];
bar(x,y)

OUTPUT:

5. Plot the bar plot of some random data


PROGRAM:
clc ;
clear all;
close all;
x = rand (1,5) y
= rand (1,5)
bar (x, y);

OUTPUT:
x=
0.3191 0.9861 0.7182 0.4132 0.0986
y=
0.7346 0.6373 0.0738 0.1205 0.9816
6. Plot the histogram of some random data, w ith the number of bins specified
PROGRAM:
x = randn(1000,1);
nbins = 25;
h = histogram(x,nbins)

OUTPUT:
h=
Histogram with properties:
Data: [1000×1 double]
Values: [1 4 14 13 26 34 48 66 76 86 91 109 103 77 79 59 40 28 25 10 3 5 1 1 1]
NumBins: 25
BinEdges: [-2.8000 -2.5500 -2.3000 -2.0500 -1.8000 -1.5500 -1.3000 -1.0500 -
0.8000 -
0.5500 -0.3000 -0.0500 0.2000 0.4500 0.7000 0.9500 1.2000 1.4500 1.7000 1.9500 2.2000
2.4500
2.7000 2.9500 3.2000 3.4500]
BinWidth: 0.2500
BinLimits: [-2.8000 3.4500]
Normalization:
'count' FaceColor:
'auto' EdgeColor: [0
0 0]

PLOT:
7. Plot the data [57, 57, 57, 58, 63, 66, 66, 67, 67, 68, 69, 70, 70, 70, 70, 72, 73, 75, 75, 76,
76,
78,79, 81] using box plot.
PROGRAM:
x=[57, 57, 57, 58, 63, 66, 66, 67, 67, 68, 69, 70, 70, 70, 70, 72, 73, 75, 75, 76, 76,
78, 79, 81];
boxplot(x)

OUTPUT:
8. Define ‘t’ as an array (-10, 10) w ith an increment of 0.01. Plot
(i)cos(t)
(ii) cost cos5t + cos5t
On the same graph. Create legends in plots

PROGRAM:
clc;
clear all;
close all;
t=-10:0.01:10;
y=cos(t);
plot(t,y,'k','linewidth',2)
hold on;
y=(cos(t).*cos(5*t))+cos(5*t);
plot(t,y,'r', 'linewidth',2)
hold on;
legend('cos(t)','cost cos5t + cos5t');

OUTPUT:
EXP NO: 7 DATE:

SIMPLE DATA ANALYSIS

OBJECTIVE:
Simple Data Analysis

1. To plot a find mean and variance of random signal and Plot its histogram w ith
an appropriate bin size.
PROGRAM:
clc
clear all
close all
t=0:1:99;
x=randi(10,100);
subplot(4,1,1) plot(t,x)
title('Random
signal')
mu=mean(x);
stan=std(x);
subplot(4,1,2)
plot(t,mu)
title('Mean of Random signal')
subplot(4,1,3)
plot(t,stan)
title('Standard deviation Random signal')
subplot(4,1,4)
hist(x)
title('Histogram of Random signal')

OUTPUT:
EXP NO: 8 DATE:

COIN TOSS AND THE LEVEL CROSSING PROBLEM

OBJECTIVE:
To study the coin tossing problem and level crossing problem

1. MATLAB code for Coin Tossing Experiments


PROGRAM:
clc
clear all
close all
N=[100, 500,1000, 5000]
for i=1:4 exp=randi(1,N(i));
head=0;
tail=0;
for j=1:N(i)
if(exp(j)==1)
head=head+1;
else tail=tail+1;
end
end
p(i)=head/N(i);
abs_error(i)=p(i)/N(i); end
plot(N,abs_error)

OUTPUT:

2. MATLAB code for level crossing Experiments


PROGRAM:
clc
clear all
close all
exp=randi(10,100);
abov_threshold=0;
for i=1:100
if(exp(i)>2)
abov_threshold=abov_threshold+1;
end
end
below_threshold=100-abov_threshold;
disp('Number of points above threshold=')
disp(abov_threshold)
disp('Number of points below threshold')
disp(below_threshold)

OUTPUT:
Number of points above
threshold= 85
Number of points below
threshold 15

TEXTBOOK EXERCISE

1. silicon sample is doped w ith 1017 arsanic atoms per sq.cm. What is the equavalent
hole concentration p0 at 300k. w hat is the relation betw een Ef and Ei .

Program:
%At room temperature 300k, KT/q = 26mV
KT = 26;
ni = 1.5*10^(10);%for silicon n0
= 10^17;
nd = n0;
p0 = (ni*ni)/n0*10^(-3);
%distance between Ef and Ei
%Ef - Ei = x
x = -KT*log(ni/n0);
x*10^(-3)
solution:
x = 0.4085

You might also like