You are on page 1of 9

MATHEMATICAL SIMULATION LAB II SEM EC

Mathematical Simulation LAB


(As per revised C-15 Syllabus by DTE, E&CE Programme)

Practice Programs
2.Program Compute area and circumference of a circle
clc
disp('Enter the radius r')
r=5
disp('Area of the Circle is =')
Area = pi*r^2
Circum = 2*pi*r

3. Program to Calculate simple interest


clc
P =input('Principal_amt\n')
R = input('Rate of interest\n')
T = input('Period in years\n')
disp('Interest= ')
SI = (P*R*T)/100

4.Program to Calculate compound interest


% A = P*(1+R/100)^N
% CI = A-P
disp('Principal amt=')
P = 1000
disp('Interest rate =')
R = 9
disp('Period in years =')
N = 3
disp('Amount = ')
A = P*(1+R/100)^N
disp('Compound interest is = ')
CI = A-P

5.Program for exchange of data between two variables


clc
disp('Before data exchange')
X = input('X = ')
Y = input('Y = ')
X = X + Y;
Y = X - Y;
X = X - Y;
disp('After data exchange')
fprintf('X is = %d\n',X)
fprintf('Y is = %d\n',Y)

6.Program to find factorial of a number


clear all;
clc
fact = 1;
n = input('Enter the number n = ')

DEPT OF E&C,GPT KGF Page 1


MATHEMATICAL SIMULATION LAB II SEM EC

while(n>1)
fact = fact*n;
n=n-1;
end
fprintf('factorial is = %d\n', fact)

7.Program for absolute value of a number


x = input('Enter the number x = \n')
disp('Absolute value is y =')
y = abs(x)

8.Program to find largest of three numbers


clear all
clc
a = input('Enter the first number a = \n')
b = input('Enter the second number b = \n')
c = input('Enter the third number c = \n')
if (a > b)
if (a > c)
fprintf('The Largest is a = %d \n',a);
else
fprintf('The Largest is c = %d \n',c);
end
else
if (b > c)
fprintf('The Largest is b = %d \n',b);
else
fprintf('The Largest is c = %d \n',c);
end
end

9. Program to know logarithm of a number


X = input('Enter the number X = \n')
disp('The Common log value of X is Y ')
Y = log10(X)
disp('The Natural log value of X is Z ')
Z = log(X)

10.Program to manipulate trignometric functions


theta1 = 60
theta2 = 30
Y1 = sind(theta1 + theta2)
Y2 = cosd(theta1 - theta2)
disp('Sum of sine and cosine waves is ')
Y = Y1 + Y2

12.program to find average of 3x4 matrix


disp('The given matrix is')
A = [1 2 3 4;5 6 7 8;9 10 11 12]
disp('Mean of columns is')
B = mean(A)
disp('Mean of rows is')

DEPT OF E&C,GPT KGF Page 2


MATHEMATICAL SIMULATION LAB II SEM EC

C = mean(A,2)
disp('Mean value of C')
D = mean(C)

13. Program for discharge voltage in capacitor


% V_out = V_in * exp(-t/(R * C))
V_in = input('Enter the input voltage V_in = ')
t = 0:0.01:1;
R = 100;
C = 1000*10^(-6);
V_out = V_in * exp(-t /(R * C));
plot(t,V_out,'m')
grid on
xlabel('Time in sec ----->')
ylabel('V_o Output voltage------>')
title('Discharge graph of a capacitor')

DEPT OF E&C,GPT KGF Page 3


MATHEMATICAL SIMULATION LAB II SEM EC

Graded Exercise
1.Program to manipulate matrix
A = [1 2 3;4 5 6;7 8 9]
B = [3 3 3;3 3 3;3 3 3]
C = A + B
D = A - B
E = A * B
F = A.* B
G = A / 2
H = A ./ B

2.Program to check a matrix for singular/


non-singular, and find inverse of matrix
clc
clear all
A = input('Enter a 3x3 matrix A = \n')
B = det(A);
disp('Determinant of Matrix A is B =')
disp(B)
if(B==0)
disp('Matrix A = singular')
else
disp('Matrix A = non-singular')
C = inv(A);
disp('Inverse of Matrix A is C =')
disp(C)
disp('The product of Matrix A and its inverse is identity matrix')
I = C*A
end

3.Program to sort and search an array


clc
clear all
A = input('Enter the array A = \n')
disp('Ascending order of array A is')
ASCEND = sort(A)
disp('Descending order of array A is')
DESCEND = sort(A,'descend')
X = input('Enter the element to be SEARCHED in array A is \n')
if (find(A==X))
disp('Good,element is found')
disp(X)
else
disp('Sorry,element is not found')
end

5.b.Program for charging of capacitor


% Formula is V_out = V_in * (1 - exp(-t/RC))
V_in = 5;
R = 100*10^(3);
C = 0.01*10^(-6);
t = linspace(0,0.01,100);
V_out = V_in * (1 - exp(-t/(R*C)));
plot(t,V_out,'*')
xlabel('Time in sec---->')

DEPT OF E&C,GPT KGF Page 4


MATHEMATICAL SIMULATION LAB II SEM EC

ylabel('Output voltage in volts----->')


title('Plot for Charging of capacitor')
grid on

6.a.Plot a straight line for Y = m*X + C


clc
X = -10:1:10;
m = input('Enter the slope of the st. line m = \n')
C = input('Enter the constant value C = \n')
Y = m*X+C;
plot(X,Y,'r')
xlabel('X values')
ylabel('Y values')
title('Straight Line')
legend('X , Y in cms')
grid on

5.a.Program to know common logarithm manipulations


clc
clear all
A = input('Enter the value of A=\n')
B = input('Enter the value of B=\n')
disp('Formula is: log10(A*B) = log10(A) + log10(B)')
X1 = log10(A*B)
Y1 = log10(A) + log10(B)
disp('Formula is: log10(A/B) = log10(A) - log10(B)')
X2 = log10(A/B)
Y2 = log10(A)-log10(B)

6.b.Differentiate & integrate y = mx + c and plot


clc
clear all
m = 0.5;
c = 4;
syms x
y = m*x+c
y1 = diff(y)
y2 = int(y)
subplot(2,1,1)
ezplot(y2)
subplot(2,1,2)
ezplot(y1)
title('Integration & Differentiation of y=mx+c')
7.Program to plot sin, cos and sum waves in time and freqcy
domains
clc
clear all
A = 1.2;
B = 1.5;
t = 0:0.01:1;
f1 = 4;
f2 = 6;
y1 = A*sin(2*pi*f1*t);
y2 = B*cos(2*pi*f2*t);
z = y1 + y2;
subplot(321)
plot(t,y1)

DEPT OF E&C,GPT KGF Page 5


MATHEMATICAL SIMULATION LAB II SEM EC

title('Sinewave function y1')


xlabel('Time,Sec')
ylabel('Amplitude')
grid on
subplot(322)
y1_freqdomain = fft(y1);
stem(abs(y1_freqdomain));
xlabel('Samples')
ylabel('Amplitude')
title('Sinewave in freqcy domain')
grid on

subplot(323)
plot(t,y2,'r')
title('coswave function y1')
xlabel('Time,Sec')
ylabel('Amplitude')
grid on
subplot(324)
y2_freqdomain = fft(y2);
stem(abs(y2_freqdomain),'r');
xlabel('Samples')
ylabel('Amplitude')
title('coswave in freqcy domain')
grid on

subplot(325)
plot(t,z,'g')
title('sum of Sin & cos function z')
xlabel('Time,Sec')
ylabel('Amplitude')
grid on
subplot(326)
z_freqdomain = fft(z);
stem(abs(z_freqdomain),'g');
xlabel('Samples')
ylabel('Amplitude')
title('Sum wave in freqcy domain')
grid on

8.Program to diff & int sin function and plot


clc
clear all
x = 0:0.01:8*pi;
y = sin(x);
subplot(311)
plot(x,y,'r')
title('Graph for y = sin(x)')
xlabel('Time in sec')
ylabel('Amp in volts')
grid on

syms x
z = sin(2*x);
f1 = diff(z);
subplot(312)
ezplot(f1)

DEPT OF E&C,GPT KGF Page 6


MATHEMATICAL SIMULATION LAB II SEM EC

title('Graph for Diff of y = sin(x)')


xlabel('Time in sec')
ylabel('Amp in volts')
grid on

f2 = int(z);
subplot(313)
ezplot(f2)
title('Graph for int of y = sin(x)')
xlabel('Time in sec')
ylabel('Amp in volts')
grid on

9.Program to demo INT{f(x) + g(x)} = INT{f(x)+ INT{g(x)}


% Let f(x)= x^2 and g(x)= 2*x

clc
clear all
syms x
f = x^2
g = 2*x
y1 = int(f)
y2 = int(g)
disp('Sum of integrals is = ')
y = y1 + y2
disp('Integration of sum 0f functions is = ')
z = int(f + g)
if(y==z)
disp('Hence integration of sum = sum of integrals')
else
disp('False')
end

10.Program to calculate mean,median,SD and variance


% Using general statitics for odd series
clc
clear all
A = input('Enter the array elements of A =\n')
len = length(A);
disp('Mean calculated using formula is = ')
avg_cal = sum(A)/len
disp('Mean calculated using Built-in function is = ')
avg_fn = mean(A)

%Median calculation
B = sort(A,'descend')
disp('median by cal and build_in fn is = ')
median_cal = B(round(len/2))
median_fn = median(A)

%Standard deviation
c = 0;
for i = 1:1:(len)
c = c + (A(i)-avg_fn)^2;
end
disp('Std deviation by cal and build_in fn is = ')
std_cal = sqrt(c/(len-1))

DEPT OF E&C,GPT KGF Page 7


MATHEMATICAL SIMULATION LAB II SEM EC

std_fn = std(A)

%Variance calculation
disp('variance by cal and build in fn is = ')
variance_cal = std_fn ^ 2
variance_fn = var(A)

11.Program to find even & prime numbers in a range


clc
clear all
min = input('Enter the minimum range value =\n ')
max = input('Enter the maximum range value = \n')
if(min>max)||(min<0)||(max<0)
error('ERROR: invalid input')
end
fprintf('List of even numbers between %d to %d are = \n',min,max)
for x = min:max
if mod(x,2)==0
disp(x)
end
end
fprintf('Prime numbers between %d to %d is = \n',min,max)
for j = 0:(max-min)
prime = 1;
for i=2:((min+j)/2)
if mod((min+j),i)==0
prime = 0;
end
end
if prime ==1
disp(min+j);
end
end

12.Program for image process


figure(1),imshow trees.tif
I = imcrop;
figure(2),imshow(I);
J = imread('trees.tif');
figure(1), imshow(J);
J2 =histeq(J);
figure(2), imshow(J2);

14. General quadratic equation solution


% (a*x^2 + b*x + c = 0)
clc
clear all
a = input('Enter value, a = \n')
b = input('Enter value, b = \n')
c = input('Enter value, c = \n')
disc = (b^2-4*a*c);
if disc>0
fprintf('The equation has two roots\n')
r1 = -b/(2*a)+ sqrt(disc)/(2*a)
r1 = -b/(2*a)- sqrt(disc)/(2*a)
else

DEPT OF E&C,GPT KGF Page 8


MATHEMATICAL SIMULATION LAB II SEM EC

if disc ==0
fprintf('The equation has a root\n')
r = -b/(2*a)
else
fprintf('The equation has no real roots\n')
end
end

Open ended Expt: diode forward response


% INITIAL VALUES
clc
clear all
i0 = 20*10^(-6); %leakage current in amp
k = 1.38*10^(-23); %Boltzmann constant (J/K)
q = 1.602*10^(-19);
vd = -0.2:0.01:0.40;
T =100;
id=i0.*(exp((q*vd)/(k*T))-1);
plot(vd,id,'r'); %plot lines in various ways
legend('T deg')
grid on
title('plot of Diode voltage Vs Diode current')
xlabel('Voltage, v_{D}----->')
ylabel('Current, i_{D}----->')

DEPT OF E&C,GPT KGF Page 9

You might also like