Basic Matlab Codes

Q1. The voltage across a discharging capacitor is
Using Matlab generate a table of voltage v(t) versus time t, for t = 0 to 50 seconds with increment of 5 s.

function []=q1()

t=0:5:50
v=10*(1-exp(-0.2*t))

end


Q2. Use Matlab to evaluate a complex number
function []=q2()

z1= [(3+6*j)*(6+j*4)]/[(2+1*j)*(2*j)]
z=z1+7+10*j

end


Q3. Write a function-file in Matlab that can be used to calculate the equivalent resistance of 4 parallel connected resistors. Take the resistances values as 1ohm, 2ohm, 3ohm, 4ohm.

function [req]=q3(r1,r2,r3,r4)

a = (1/r1)+ (1/r2) + (1/r3) + (1/r4);
req = 1/a;
end


Q4. Use Matlab to simplify the expression
function []=q4()

a = 0.5 + 6*j + 3.5*exp(0.6*j) + (3+6*j)*exp(0.3*pi)


end


Q5. The voltage v and current I of a certain diode are related by the expression i= Isexp[v/(nVT)]. If Is= 1.0 X 10-14A, n=2 & VT= 26mV, Plot the current versus voltage curve of the diode for diode voltage between 0 to 0.6V.

function []=q5()

IS = 1*[10^(-14)]
n=2
VT= .026
v = 0:.01:.6
i= IS*(exp(v/(n*VT)))
plot(v,i)
xlabel('Volgate in volts')
ylabel('Current in mA')
title('V-I Characteristic of Forward Bias Diode')

end


Q6. The current flowing through a drain of a field effect transistor during saturation is given as IDS = k(VGS-Vt)^2. If Vt= 1V, k= 2.5 mA/V2. Plot IDS for VGS: 1.5, 2,2.5………5V

function []=q6()

Vt = 1
k= 2.5
VGS = 1.5:.5:5
IDS = k*((VGS - Vt).^2)
plot(VGS,IDS)
xlabel('Volgate in volts')
ylabel('Current in A')
title('V-I Characteristic of FET')

end


Q7. Plot the voltage across a parallel RLC circuit given as v1(t)= 5e^(2t)sin(10πt) & v2(t)= 5e^(2t)cos(10πt) in same graph.

function []=q7()
t=0:.01:1;
a= 5*(exp(2*t))
v1= a.*(sin(10*pi*t));

v2 = a.*(cos(10*pi*t));
plot(t,v1,'*',t,v2,'o');
xlabel('Volgate in volts')
ylabel('Current in A')
title('RLC Circuit')

end


Q8. Obtain the polar plot of z= r^(-n)e^(jnθ) for r = 1.2, θ=15 degree & n=1 to 20.

function []=q8()

r = 1.2
theta = (15*pi)/180;
n = 1:20;
z = (r.^(-n)).*(exp(n.*theta*j));
polar(n,z);

title('Polar Plot')

end


Q9. A message signal m(t) and the carrier signal c(t) of a communication system are, respectively: m(t) = 4cos(1200πt). A double-sideband suppressed carrier s(t) is given as s(t) = m(t)c(t). Plot m(t), c(t) & s(t) using subplot.

function []=q9()

t = 0:.0000001:.01;
c = 10*cos(10000*pi*t);
m = 4*cos(1200*pi*t);
s = m.*c;
subplot(3,1,1)
plot(t,c)
title('Carrier Signal')
subplot(3,1,2)
plot(t,m)
title('Message Signal')
subplot(3,1,3)
plot(t,s)
title('DSB-SC Modulated Signal')
end


Q10. Write a MATLAB program to add all the even numbers from 0 to 100.

function [sum]=q10()

sum = 0;
for n= 0:2:100
    sum = sum + n;
end

end


Q11. Add all the terms in the series
until the sum exceeds 1.995. Print out the sum and the number of terms needed to just exceed the sum of 1.995.

function []=q11()
sum=0;
for n = 0:10000
    sum = sum + 1/(2^n);
    if(sum>1.995)
        break;
    end
end
sum
n

end


Q12. The Fibonacci sequence is given as 1 1 2 3 5 8 13 21 34 ….. Write a MATLAB program to generate the Fibonacci sequence up to the twelfth term. Print out the results.

function []=q12(n)
a(1)=1
a(2)=1
for i=3:n
    a(i)= a(i-1)+a(i-2);
end
a
end


Q13. Write a function-file to obtain the dot product and the vector product of two vectors a & b. Use the function to evaluate the dot and vector products of vectors x and y, where x = (1 5 6) & y = (2 3 8).

function []=q13()

a = [1 5 6];
b= [2 3 8];
dot= a.*b
cross(1)= a(2)*b(3)- b(2)*a(3);
cross(2)= -(a(1)*b(3)-b(1)*a(3));
cross(3)= b(2)*a(1)- b(1)*a(2);
cross
end