You are on page 1of 64

‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX1: Use loop analysis to write equations for the circuit shown in figure ,

Determine the current I using MATLAB .

R1

6Ω R4
R5


R2
V1
10 V 2Ω
R3 I R6
8Ω 15Ω

----------------------------------------------------------

Solution:

The equations:-

)R1 +R4+R3)i1 – R4 i2 – R3 i3 = 10 ---------------(1)

(R4+R5+R2)i2 – R2 i3 – R4 i1 = 0 -----------------(2)

(R2+R6+R3) i3 – R2 i2 – R3 i1 = 0 -----------------(3)

I = i3 – i2 ----------------------------------- (4)
----------------------------------------------------------
Matlab program

clc
clear
R1=6; R2=2; R3=8; R4=4; R5=6; R6=15;

a=[R1+R4+R3 -R4 -R3


-R4 R5+R4+R2 -R2
-R3 -R2 R2+R6+R3];
b=[10;0;0];
i=inv(a)*b;
i1=i(1);
i2=i(2);
i3=i(3);
I=i3-i2
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX2: Use nodal analysis to solve for the nodal voltages for the circuit shown
in figure ,using MATLAB .

V2

R6
I1 R3 6Ω
3 A 5Ω

R1 R5
V1 V3 V4
2Ω 3Ω

I3 I2
R2 4 A 6 A

R4

----------------------------------------------------------

Solution:

V1/R1+V1/R2 – V3/R1 = I1 ----------------------------------------------------- 1

V2/R3+V2/R6 – V3/R3 – V4/R6 =–I1 -------------------------------------------- 2

V3/R1+V3/R3+V3/R5– V1/R1 – V2/R3 – V4/R5 = I3 ------------------------- 3

V4/R6+V4/R5 – V2/R6 – V3/R5 = I2 ------------------------------------------ 4

V5/R4 =–I2 ------------------------------------------------------------------------- 5


‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Matlab program

clc
clear
R1=2; R2=4; R3=5; R4=8; R5=3; R6=6;
i1=3; i2=6; i3=4;

a=[(1/R1)+(1/R2) 0 -1/R1 0
0 (1/R3)+(1/R6) -1/R3 -1/R6
-1/R1 -1/R3 (1/R1)+(1/R3)+(1/R5) -1/R5
0 -1/R6 -1/R5 (1/R6)+(1/R5)];

b=[i1;-i1;i3;i2];

V=inv(a)*b;
V1=V(1)
V2=V(2)
V3=V(3)
V4=V(4)
V5=-i2*R4
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX3: Use loop analysis and MATLAB to find the loop currents and the power
supplied by the sources:

R1 R5

4Ω 3Ω
V1
12 V
I1 R4 I2
R2 2Ω

R3 R6

2Ω 2Ω

V2 I3 R8 I4
12 V 2Ω

R9 R7

3Ω 4Ω

----------------------------------------------------------

Solution:

clc
clear
R1=4; R2=4; R3=2; R4=2; R5=3; R6=2; R7=4; R8=2; R9=3;
V1=12; V2=12;

a=[R1+R2+R3+R4 -R4 -R3 0


-R4 R5+R4+R6 0 -R6
-R3 0 R8+R9+R3 -R8
0 -R6 -R8 R6+R7+R8 ];

b=[0;-V1;V2;0];
i=inv(a)*b;
i1=i(1);
i2=i(2);
i3=i(3);
i4=i(4);

p1=V1*i2
p2=V2*i3
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX4: For the circuit shown below , At t=0 the switch moved to position (a)
and remaained for 2 sec after that, the switch moved to position (b) and
remaained for 1 sec, then the switch moved to position (c) Where it
remaained indefinitely.sketch the current flowing through the inductor for
time from 0 to 10 sec:

S
RS a L

100Ω 50mH
b c
VS
50 Vrms R1 R2 R3
50 Hz 50Ω 25Ω 25Ω

----------------------------------------------------------

Solution:

clc
clear
VS=50; RS=100; R1=50; R2=25; R3=25; L=50*10^-3;
Io=VS/(RS+R3); T1=L/(R3+RS); T2=L/(R3+R1);
T3=L/(R3+R2);
t=-.1:10;
for k=1:length(t)
if t(k)>0 & t(k)<2
IL(k)=Io*(1-exp(-t(k)/T1));
else if t(k)>=2 & t(k)<3

IL(k)=Io*exp((-t(k)-2)/T2);
else
IL(k)=Io*exp((-t(k)-3)/T3);
end
end

end
plot(t,IL,'linewidth',3)
grid on
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

----------------------------------------------------------

Solution:

clc
clear
vs=40;
RS=50; R1=50; R2=150; L=200;
T1=L/(RS+R1); T2=L/(RS+R2);
t=linspace(-.00001,1.2,10);
for k=1:length(t)

if t(k)<=0
i(k)=0;

elseif t(k)>0 & t(k)<=1


im=40/100;
i(k)=im*(1-exp(-t(k)/T1));
else
im=i(1);

i(k)=im*(exp(-(t(k)-1))/T2);
end

end
plot(t,i,'linewidth',3)
grid on
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX5: For the circuit shown :

Using switch command,write a MATLAB program to :-

1-Find the current.

2-Plot the Voltage and current waveforms for two period using subplots

S
2

V1
230 Vrms R1 L1 C1
50 Hz 100Ω 300mH 100µF

----------------------------------------------------------

Solution:

clc
clear
% Given data :-
F=50; % F in Hertz
v= 230; % v in Voltt
R= 100 ; % in Ohm
L= 300*10^-3; % L in Hinry
C= 100*10^-6; % C in Farad
% From Given data :-

XL= 2 * pi * F * L * j ; % XL in Ohm
XC = 1 /(2 * pi * F * C * j); % XC in Ohm
T=1/F ; % T in (s)
w= 2 * pi * F ;
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

% The program :-

S=input('‫)'ادخم رقم انمفتاح‬


switch S

case 1
Z=R;

case 2
Z=XL;
case 3
Z=XC;
otherwise
disp(' ‫)' انقيمة غير معرفة‬
end
i=v/Z
an=angle(i);
t=linspace(0,2*T);
Vm = sqrt(2)* v * cos(w*t) ;
Im = sqrt(2)* abs(i) * cos((w*t)+ angle(i));
disp('Im(t) = sqrt(2)* abs(i) * cos((w*t)+
angle(i))')

subplot(3,1,1)

plot(t,Im,'linewidth',2)
xlabel('Time (s)')
ylabel('current(A)')
title('Current Curve')

grid on
subplot(3,1,2)
plot(t,Vm,'linewidth',2)
xlabel('Time (s)')
ylabel('Voltag(V)')
title('Voltag Curve')
grid on

subplot(3,1,3)
plot(t,10*Im,t,Vm,'linewidth',2)
xlabel('Time (s)')
title('Voltag and Current Curve')
grid on
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX6: A battery charging circuit shown ,Write a MATLAB program to:-

1-Sketch the input voltage for two period.

2-Plot the current flowing through the diode for two period.

3-calculate the condution angle of the diode.

4- calculate the peak current .

R D

200Ω DIODE_VIRTUAL
VS
18Sin(100πt) VB
18 Vpk
12 V
1kHz

----------------------------------------------------------

Solution:

clc
clear
VB=12; R=200; w=100*pi; f=w/(2*pi); T=1/f ;
Vm=18;
t=0:T/50:2*T;
for i=1:length(t)

Vs(i)= Vm*sin(100*pi*t(i));
if Vs(i)< VB
ID(i)=0;

else
ID(i)=(Vs(i)- VB)/R;
end

end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

subplot(211)
plot(t,Vs,'linewidth',2)
title('Input Voltage')
xlabel('Time')
ylabel('Voltage')
grid on

subplot(212)
plot(t,ID,'linewidth',2)
title('Diode current')
xlabel('Time')
ylabel('current')
grid on

Th1=asin(VB/Vm);
Th2=pi-Th1;
cond_angle=Th2-Th1

ID_Peak=(Vm*sin(pi/2)-VB)/R
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX7:For the circuit shown , Write a MATLAB program to Sketch the output
voltage and the current flowing through the RL for two period.

RS

200Ω
D4 D1

- VO +
DIODE_VIRTUAL
DIODE_VIRTUAL

V1 RL
20 Vpk 100Ω
18Sin(100πt)
50 Hz
D3 D2

----------------------------------------------------------

Solution:

clc
clear
R=200; w=100*pi; f=w/(2*pi); T=1/f ; Vm=20;
RL=100;
t=0:T/50:2*T;
Vs= Vm*sin(100*pi*t);
Vo=abs(Vs)
IL=Vo/RL
subplot(211)
plot(t,Vo)
title('Output Voltage ')
grid on

subplot(212)
plot(t,IL)
title('RL current ')
grid on
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX8: For the circuit shown :

Using switch command,write a MATLAB program to :-

1-Find the current.

2-Plot the Voltage and current waveforms for two period

R2 D1
4
50Ω DIODE_VIRTUAL

VS 1
V1
230 Vrms
230Sin(100πt) L C R1
50 Hz VB
30mH 300µF 25Ω 6V

----------------------------------------------------------

Solution:

clc
clear
Vm=230;
w=100*pi; f=w/(2*pi); R1=25; R2=50; VB=6;
T=1/f;l=30*10^-3; xl=w*l*i; c=300*10^-6; xc=-
i/(w*c)
t=linspace(0,2*T); VS=Vm*sin(w*t);

s=input('‫)'ادخم رقم انمفتاح‬


switch s
case 1
im=Vm/xl;
i=abs(im)*sin((w*t)+angle(im));
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

case 2
im=Vm/xc;
i=abs(im)*sin((w*t)+angle(im));
case 3
i=VS/R1;
case 4
for k=1:length(t)

if VS(k)< VB
i(k)=0;
else
i(k)=(VS(k)-VB)/R2;
end
end
otherwise
disp(' ‫)' انقيمة غير معرفة‬
end

plot(t,i,t,VS)
grid on
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Polynomials Analysis
EX: Find the second order least square regression modele to predict value of
y for x=8

x 1 2 3 4 5 6 7
y 2 8 26 50 140 224 350
Then estimate:-

1-The first derivative value of the polynomial at x=3

2- The second derivative value of the polynomial at x=2

3- The inteqral equation

4-plot two figures in the same frame and compare between first and second
results

----------------------------------------------------------

Solution:

clc
clear
x = 1:1:7;
y= [2 8 26 50 140 224 350];
%1- ‫درجة انمعادنة هي عدد انحددود‬
n=6;%‫درجة انمعادنة‬
v=polyfit(x,y,n)% ‫نتكىيه انمعادنة‬

a= polyval(v,8) ; %X=8‫قيمة انمعادنة عىد‬


%‫انمطهب االول‬
y1= polyder(v)
Y1=polyval(y1,3);
%‫انمطهب انثاوي‬
y2= polyder(y1);
Y2= polyval(y2,2);
%‫انمطهب انثانث‬
b= polyint(v);
%‫انمطهب انرابع‬
h= polyval(v,x)
plot(x,y)
hold on
plot (x,h,':r')
grid on

‫) نالحظ الفرق في الرسم‬6( ‫) الل من‬n( ‫عند اختيار ليمة‬


‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: For the curve shown in fig , Write a MATLAB program to find :

1- The equation of the curve at x=4.5

2-The slop of the curve at x=5

3-The integration of the curve

----------------------------------------------------------

Solution:

clc
clear
x=0:7;
y=[7 5 4 0 3 6 5 7];

n=6 %‫مه انرسم‬


y=polyfit(x,y,n);
y1=polyval(y,4.5)
y2=polyder(y);
slope=polyval(y2,5)
y3=polyint(y)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Function file

EX:Write a function file to solve the equivalent resistance of series


connected resistors,R1,R2,R3….Rn

----------------------------------------------------------

Solution:

function Reg=eq_sr(R)

n= length(R);

Reg= sum(R);
end

EX:Write a function file to solve the equivalent resistance of parallel


connected resistors,R1,R2,R3….Rn

----------------------------------------------------------

Solution:

function Reg=eq_p(R)

n= length(R);

s=0;

for k=1:n

s=s+(1/R(k));

end

Reg= 1/s;

end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX:Write a function to find the equivalent of N-shunt impedances, then use


this function to find and draw the current in the circuit shown below:-

----------------------------------------------------------

Solution:

clc
clear
z1=5+10i ; z2=10+10i; z3=10-5i; z4=5; rs=5;
f=50; v=220*exp(30*pi/180*i);
z=[z1 z2 z3 z4];
zt=eq_p(z);
R=[rs zt];
Zeq=eq_sr(R)
i=v/Zeq;
w=2*pi*f;
T=1/f;
t=linspace(0,2*T);
V=sqrt(2)*abs(v)*sin((w*t)+angle(v));
I=sqrt(2)*abs(i)*sin((w*t)+angle(i));
plot(t,V,t,I)
grid on
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX:Write a function file to obtain the roots of the quadratic equation:-

aX^2 +bX+c = 0

----------------------------------------------------------

Solution:

function R=roots(f)

a= f(1);
b= f(2);
c= f(3);
m=b^2-(4*a*c);
if m>0
disp('the roots is Rial')
x1= (-b + sqrt(m))/(2*a)
x2= (-b - sqrt(m))/(2*a)

elseif m<0
disp('the roots is imag')
x1= (-b + sqrt(m))/(2*a)
x2= (-b - sqrt(m))/(2*a)

elseif m==0
disp('the roots is Rial and egule')
x1= (-b + sqrt(m))/(2*a)
x2= (-b - sqrt(m))/(2*a)

end

end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: For the shape shown in fig below, Write a MATLAB program to plot the
curve y=f(x) using function:-

1 = ‫الميل‬

----------------------------------------------------------

Solution:

function y= ramp1(t,to) function y=uns(t,to)


for k=1:length(t)
if t(k)<to for k=1:length(t)
if t(k)<to
y(k)= 0 ;
y(k)=0;
else
else
y(k)= t(k)-to; y(k)=1;

end end

end end
end
end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

clc
clear
t=0:.01:6;
to=0

v1= ramp1(t,0)-ramp1(t,2)

v2= uns(t,0)-uns(t,4)
v3=v1.*v2
plot(t,v3,'linewidth',3)
grid on

EX: For the shape shown in fig below, Write a MATLAB program to plot the
curve y=f(x) using function:-

1 = ‫الميل‬

----------------------------------------------------------

Solution: )‫) السابك‬function( ‫(باستخدام نفس‬


clc
clear
t=0:.01:14;
to=0
v1= ramp1(t,12)-ramp1(t,4)
v2= ramp1(t,2)-ramp1(t,10)
v3=(v1+v2)
plot(t,v3,'linewidth',3)
grid on
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: For the shape shown in fig below, Write a MATLAB program to plot the
curve y=f(x) using function Then Find the area under the curve

1 = ‫الميل‬

----------------------------------------------------------

Solution: )‫) السابك‬function( ‫(باستخدام نفس‬

clc
clear
t=0:.01:6;
v1= ramp1(t,2)-ramp1(t,3);
v2= ramp1(t,4)-ramp1(t,3);
v3=(v1+v2)*5;
plot(t,v3,'linewidth',3)
grid on
area=trapz(t,v3)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: For the shape shown in fig below, Write a MATLAB program to plot the
curve y=f(x) using function Then Find the area under the curve

1 = ‫الميل‬

----------------------------------------------------------

Solution: )‫) السابك‬function( ‫(باستخدام نفس‬

clc
clear
t=0:.01:6;
to=0
v1= ramp1(t,0)-ramp1(t,5)
v2= uns(t,0)-uns(t,5)
v3=v1.*v2
plot(t,v3,'linewidth',3)
grid on
area=trapz(t,v3)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: Write a MATLAB program to plot the shape shown in fig below using
function

----------------------------------------------------------

Solution:

clear
clc
t=-10:.01:10;
v1=ramp1(t,-8)-ramp1(t,0);
v2=ramp1(t, 8)-ramp1(t,0);
V1=v1+v2;
v3=-1*v1;
v4=-1*v2;
V2=v4+v3;
fill(t,V1,'r',t,V2,'b')

EX: Write a MATLAB program to plot the shape shown in fig below using
function

----------------------------------------------------------

Solution:

clc
clear
t=0:.001:5;
v1=uns(t,0.01)-uns(t,5)
v2=(uns(t,.01)-uns(t,5))*2
v3=(uns(t,.01)-uns(t,5))*3
fill(t,v3,'r')
hold on
fill(t,v2,'w')
fill(t,v1,'k')
text(2.4,1.5,'Alah Akbar')
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: For the shape shown in fig below, Write a MATLAB program to plot the
curve y=f(x) using function Then Find the area under the curve

----------------------------------------------------------

Solution: )‫) السابك‬function( ‫(باستخدام نفس‬

clc
clear
t=0:1:20;
hold on
a=0
area=0;
while a<20

f1=(ramp1(t,a)-ramp1(t,a+1))*.5;
f2=(ramp1(t,a+2)-ramp1(t,a+1))*.5;
f=f1+f2;
fill(t,f,a)
area=area+trapz(t,f)
a=a+4;

end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: For the shape shown in fig below, Write a MATLAB program to plot the
curve y=f(x) using function

----------------------------------------------------------

Solution: )‫) السابك‬function( ‫(باستخدام نفس‬

clc
clear
t=0:1:20;
hold on
a=0;
b=10;
c=0;
while a<20
f1=(ramp1(t,a)-ramp1(t,a+1))*.5;
f2=(ramp1(t,a+2)-ramp1(t,a+1))*.5;
f=f1+f2;
f3=(ramp1(t,a+2)-ramp1(t,a+3))*.5;
f4=(ramp1(t,a+4)-ramp1(t,a+3))*.5;
f5=f3+f4;
plot(t,+f);
plot(t,-f5);
a=a+4;
c=c+2;
b=b+2;
end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: For the shape shown in fig below, Write a MATLAB program to plot the
curve y=f(x) using function Then Find the area under the curve

Y=mx+c

m=2/3

at x=4, y=2
2
then c=-2/3

----------------------------------------------------------

Solution:

function y=rp(t,to)
for k=1:length(t) The program:-
m=2/3;
c=-2/3 clc
if t(k)<to clear
y(k)=0 t=0:.1:10
else f1=rp(t,1)-rp(t,4)

y(k)=((m*t(k))+c)- f2=rp(t,9)-rp(t,6)
((m*to)+c) f=(f1+f2)
end plot(t,f,'linewidth',3)
grid on
end area=trapz(t,f)

end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: Write a MATLAB program to plot the shape shown in fig below

----------------------------------------------------------

Solution:

clc
clear
for k= 25:-5:1
t=0:pi/50:2*pi;
x= k*sin(t);
y= k*cos(t);

fill(x,y,k)
hold on
% pause(.2)

end
% ‫نرسم دائرة انمركس‬
fill(sin(t),cos(t),'g')
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Symbolic Expression

----------------------------------------------------------

Solution:

clc
clear
syms x
y= ((3*x^2)+(6*x)-1 )/ (x^2+x-3);
d1=diff(y)
d2 = diff(d1)
in=int(y)
pretty(y)

----------------------------------------------------------

Solution:

clc
clear
syms x
y=symsum((x^2+x)/2,0,10)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

----------------------------------------------------------

Solution:
clc
clear
syms x
f=dsolve('(x^2*D2y)+ (7*x*Dy)+(5*y)=10-(4/x)','Dy(1)=0','y(1)=1','x')
ezplot(f)

EX: if f(t)=t* exp(-t^2) find F.T

----------------------------------------------------------

Solution:

clc
clear
syms t
f=t*exp(-t^2)
FT=fourier(f)

EX: if f(t)=t* exp(-t^2) find L.T

----------------------------------------------------------

Solution:

clc
clear
syms t
f=t*exp(-t^2)
FT=laplace(f)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Frequency Response
EX: For the system with the following transfer function :

𝑽𝒐(𝒔) 𝟒𝒔𝟐 + 𝟔𝑺
H(s)= =
𝑽𝒔(𝒔) 𝟔𝒔𝟑 + 𝟐𝟓𝒔𝟐 +𝟑𝟎𝒔+𝟗

Write a MATLAB program to :

1-find the poles and zeros of the system .

2-plot the frequency response (magnitude in dB and phase in degree versus frequency) for
(w) varies from 0.1 to 70000 rad/sec.

3-if Vs(t)=10exp(-3t) cos(2t+40) find Vo(t).

4-convert the network function to the partial faction form .

----------------------------------------------------------

Solution:

clc % ‫ انفرع انثانث‬c


clear s=-3+2i;
% ‫انفرع االول وانثاوي‬ vs=10*exp(i*40*pi/180);
num=[4 6 0]; n=polyval(num,s);
dnum=[6 25 30 9]; dn=polyval(dnum,s);
poles=roots(num) vo=vs*n/dn
zeros=roots(dnum)
w=logspace(-1,4.845); % ‫ انفرع انرابع‬d
f=w/(2*pi); [r,p,k]= residue(num,dnum)
Hs=freqs(num,dnum,f);
mag=20*log10(abs(Hs));
an=angle(Hs)*180/pi;
subplot(2,1,1)
semilogx(f,mag)
grid on
xlabel('freq in Hz')
ylabel('mag')
title('freq vs mag')
subplot(2,1,2)
semilogx(f,an)
grid on
xlabel('freq in Hz')
ylabel('angle in deg')
title('freq vs angle')
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: For the RLC circuit shown in fig if transfer function as :


𝑹
𝑽𝒐(𝒔) ( 𝑳 )𝒔
H(s)= = 𝑹 𝟏
𝑽𝒔(𝒔) 𝒔𝟐 +( 𝑳 )𝒔+ 𝑳𝑪

L C
Plot the frequency response for (w) varies
from 1 to 10000 rad/sec and find Vo(t) 5H
1.12µF
when :
Vi 230 Vrms R
1-R=10000Ω 2-R=100Ω 5025Ω
Hz

----------------------------------------------------------

Solution:

clear subplot(222)
clc semilogx(f,m2)
L=5; C=1.12*10^-6; R1=10000; R2=100; ylabel('mag')
xlabel('freq')
num1=[R1/L 0]; dnum1=[1 R1/L 1/L*C]; title('mag vs freq at R=100 ohm')
num2=[R2/L 0]; dnum2=[1 R2/L 1/L*C];
subplot(223)
zeros1=roots(num1) semilogx(f,th1)
zeros2=roots(num2) ylabel('Theta')
xlabel('freq')
title('Theta vs freq at R=10000 ohm')
poles1=roots(dnum1)
poles2=roots(dnum2)
subplot(224)
semilogx(f,th2)
%-------------------------------------- ylabel('Theta')
w=logspace(log10(1),log10(10000)); xlabel('freq')
f=w/(2*pi); title('Theta vs freq at R=100 ohm')
%------------------------------------------
h1=freqs(num1,dnum1,f); s=10i;
h2=freqs(num2,dnum2,f); vi=10*exp(i*30*pi/180)
n1=polyval(num1,s)
m1=20*log10(abs(h1)); n2=polyval(num2,s)
m2=20*log10(abs(h2));
dn1=polyval(dnum1,s)
th1=angle(h1)*180/pi; dn2=polyval(dnum2,s)
th2=angle(h2)*180/pi;
vo1=vi*n1/dn1
subplot(221) vo2=vi*n2/dn2
semilogx(f,m1) %------------------------------------------
ylabel('mag')
xlabel('freq') [r1,p1,k1]=residue(num1,dnum1)
title('mag vs freq at R=10000 ohm')
[r2,p2,k2]=residue(num2,dnum2)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: For the filter shown in fig Write a MATLAB program to analyse the circuit
and to:

1-plot the phasor diagram of VS,VR and VO .

2-find the transfer function Vo(w)/Vs(w) then plot the absolute (in dB) and
the phase (in degree) of it versus alogorithm scale with titles and labels .

2kΩ

C
VS 230 Vrms
50 Hz 2µF
0.1 sin(1570800t) 0°

----------------------------------------------------------

Solution:

clc
clear
R=2000; C=2*10^-6 ; Vs=0.1; S=1570800i;
XC=1/(C*S);
Vo=Vs*XC/(R+XC);
Vr=Vs-Vo;
VS=[0 Vs];
VO=[0 Vo];
VR=[0 Vr];
subplot(311)
plot(real(VS),imag(VS),real(VO),imag(VO),real(VR)
,imag(VR),'linewidth',4)
grid on
title('Phasor diagram')
axis([-.01,.15,-2*10^-5,2*10^-5])

% H(S)=1/(RC S + 1)
num=[1/(R*C)]; dnum=[1 1/(R*C) ];
w=logspace(log10(.1),log10(1570800));
f=w/(2*pi);
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

HS=freqs(num,dnum,f);
mag=abs(HS);
Ph=angle(HS)*180/pi;
subplot(312)
plot(f,mag)
grid on
title('absolute value in dB')
xlabel('frequency')
ylabel('mag')
subplot(313)
plot(f,Ph)
grid on
title('Phase in degree')
xlabel('frequency')
ylabel('Phase')
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

fzero
Ex: For a capacitor smoothing circuit of Figure, if R=10 KΩ, C=100
μF, and vs(t)=120 𝟐 sin(120πt ) , use MATLAB programming to
calculate the times t2, t3 of the circuit and compare the capacitor
discharge time with period of the input signal.

D1
VO

R C
VS 230 Vrms 10kΩ
DIODE_VIRTUAL 100µF
50 Hz

----------------------------------------------------------

Solution:
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

function y=sinexpf1(t)
f0=60; t2=1/(4*f0); tp=1/f0;
RC=10e3*100e-6;
y = sin(120*pi*(t))-exp(-(t-t2)/RC);
end

Clc
clear

% Capacitance discharge time for smoothing circuit


vm=120*sqrt(2);
vt2=vm;
f0=60; R=10e3; C=100e-6;
t2=1/(4*f0);
tp=1/f0;
% to find the t3:
RC=R*C;
t3=fzero('sinexpf1',4.5*t2);
tdis_cap=t3-t2;
fprintf('The value of t2 is %9.5f s\n', t2)
fprintf('The value of t3 is %9.5f s\n', t3)
fprintf('The capacitor discharge time is %9.5f s\n', tdis_cap)
fprintf('The period of input signal is %9.5f s\n', tp)

vt3=vm*sin(120*pi*(t3))
% or :
vt33=vm*exp(-(t3-t2)/RC)

V_ripple=vt2-vt3
V_ripple_approximate=vm/(f0*RC)
Vrms=V_ripple/(2*sqrt(3))
Vdc=vm-V_ripple/2

% Plot the Vo
t=0:tp/200:1.5*tp;
for i=1:length(t)
if t(i)<t2||t(i)>t3
v(i)=120*sqrt(2)*sin(120*pi*t(i));
else
v(i)=120*sqrt(2)*exp(-(t(i)-t2)/RC);
end
end
plot(t,v)
grid on
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Results:
The value of t2 is 0.00417 s
The value of t3 is 0.02036 s
The capacitor discharge time is 0.01619 s
The period of input signal is 0.01667 s

vt3=166.9801
vt33=166.9801
V_ripple =2.7255
V_ripple_approximate=2.8284
Vrms=0.7868
Vdc=168.3429

180

160

140

120

100

80

60

40

20

0
0 0.005 0.01 0.015 0.02 0.025
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Ex: For a capacitor smoothing circuit of Figure, if R=10 KΩ, C=100


μF, and vs(t)=311sin(100πt ) , use MATLAB program to:

1-Plot the input voltage and output voltage with respect to time for
1.5 period.

2-Find discharge time of the capacitor .

3-Find peak to peak ripple voltage of the output .

4-Find the approximately D.C voltage of the output waveform.

D1
VO

R C
VS 230 Vrms 10kΩ
DIODE_VIRTUAL 100µF
50 Hz

----------------------------------------------------------

Solution:

The function:-

function y=ff(t)
r=10000;
c=100*10^-6;
vm=311;
w=100*pi;
t2=1/200;
fo=50;
T=1/fo;
RC=r*c;

y=sin(w*t)-exp(-(t-t2)/RC);

end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

The program:-

clc
clear
r=10000; c=100*10^-6; fo=50; T=1/fo; t2=1/200;
RC=r*c; vm=311; w=100*pi;

t3=fzero('ff',4.5*t2);
t=0:T/200:1.5*T;
% To plot input voltage and output voltage for 1.5 period
vs=-vm*sin(w*t);
subplot(221)
plot(t,vs,'linewidth',1.5)

title('input voltage')
xlabel('Time')
ylabel('vin')
grid on

for k=1:length(t)
if t(k)<t2 ||t(k)>t3

vo(k)=-vm*sin(w*t(k));
else
vo(k)=-vm*exp(-(t(k)-t2)/RC);
end

end
subplot(222)
plot(t,vo,'linewidth',1.5)
grid on
title('output voltage')
xlabel('Time')
ylabel('vout')

subplot(212)
plot(t,vs,':r','linewidth',1.5)
hold on
plot(t,vo,'linewidth',1.5)
grid on
xlabel('Time')
ylabel('vin and vout')

title('input and output voltage ')


‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

% To find dis charge time of the capacitor

tdischarge=t3-t2

% To find peak to peak ripple voltage

vt2=vm*sin(w*t2);
% or
% vt2=vm
vt3=vm*sin(w*t3);
% or
%vm*exp(-(t3-t2)/RC);
vrp_p=vt2-vt3

% To find approximately D.C voltage

vr_approximate = vm/(RC*fo)

% To find Vrms

vrms=vrp_p/(2*sqrt(3))
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

ODE23
Ex: For the circuit shown below, find Vo(t) for interval 0 to 0.5sec

1-Using a numerical solution to the differential equation .

2-Using analytical solution .

Assuming Vo(0)=0 .

R
Solution: Vo(t)
10kΩ
ic=ir
𝒅𝑽𝒐(𝒕) 𝑽𝑺−𝑽𝒐(𝒕)
C = VS C
𝒅𝒕 𝑹
10 V 10µF
𝒅𝑽𝒐(𝒕) 𝑽𝑺 𝑽𝒐(𝒕)
= -
𝒅𝒕 𝑹𝑪 𝑹𝑪

The analytical solution :-


−𝒕
Vo(t)=10( 1 - exp( ) )
𝑹𝑪

The function The programe


function dVo=dif(t,Vo)
VS=10; R=10000; clc
C=10*10^-6 ; RC=R*C; clear
dVo=(VS/RC)- (Vo/RC); to=0; tf=1; Voo=0; R=10000;
end
C=10*10^-6 ; RC=R*C;
% The numerical solution
[t,Vo]=ode23('dif',to,tf,Voo);
subplot(211)
plot(t,Vo,'linewidth',3)
grid on
title('numerical solution ')
xlabel('Time')
ylabel('Outout Voltage')
axis([0,2,0,15])
% The analytical solution
Vo_analytical=10*(1-exp(-t/RC));
subplot(212)
plot(t,Vo_analytical,'linewidth',3)
grid on
title('analytical solution ')
xlabel('Time')
ylabel('Outout Voltage')
axis([0,2,0,15])
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Ex: For the circuit shown below, if the switch is opened at t=0 plot Vo(t)
between the time interval 0 to 5sec .

Solution: R1 R3
VO(t)
20kΩ 10kΩ
ic=ir
𝒅𝑽𝒐(𝒕) 𝑽𝑺−𝑽𝒐(𝒕) VS R2 C
C = 30 V 40kΩ
𝒅𝒕 𝑹 1µF

𝒅𝑽𝒐(𝒕) 𝑽𝒐(𝒕)
=-
𝒅𝒕 𝑹𝑪

The function The program


function dVo=df(t,Vo) clc
R2=40000; clear
R3=10000; VS=30;
C=10*10^-6; R1=20000;
taw=(R3+R2)*C; R2=40000;
dVo=-Vo/taw; R3=10000;
end C=10*10^-6;
taw=(R3+R2)*C;
Voo=VS*(R2/(R2+R1))
ts=0;
tf=5;
% numerical soltion
[t,Vo]=ode23('df',ts,tf,Voo);
subplot(211)
plot(t,Vo,'linewidth',3)
grid on
title('numerical soltion')
xlabel('time')
ylabel('Output Voltage')
axis([0,5,-5,5+Voo])
% analytical soltion
Vo_analy=Voo*exp(-t/taw);
subplot(212)
plot(t,Vo_analy,'linewidth',3)
grid on
title('analytical soltion')
xlabel('time')
ylabel('Output Voltage')
axis([0,5,-5,5+Voo])
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Ex: For the circuit shown below, if the switch is closed at t=0.5 and
remaained for 1.5 sec after that the switch is opened at t=2 plot IL(t)
between the time interval 0 to 5sec .Assuming IL(2)=1.8A

R3
IL(t)
16Ω

R1 R2
8Ω 8Ω L
I
9A 4H

----------------------------------------------------------

Solution:

At .5<t<2 At 2<t<5
𝑹𝟏∗𝑹𝟐 Rt= R2
Rt=𝑹𝟏+𝑹𝟐

VL=VRt – VR3 VL=VRt – VR3


𝒅𝑰(𝒕)
L
𝒅𝑰(𝒕)
=VRt-Vr3 L =VRt-Vr3
𝒅𝒕
𝒅𝒕
𝒅𝑰(𝒕) 𝑽𝑹𝟑 𝑽𝑹𝒕
𝒅𝑰(𝒕)
=
𝑽𝑹𝒕
-
𝑽𝑹𝟑 = -
𝒅𝒕 𝑳 𝑳
𝒅𝒕 𝑳 𝑳
𝑹𝟑
VRt=𝑰 ∗ 𝑹𝒕+𝑹𝟑 *Rt
𝑹𝟑 VRt=𝑰 ∗ 𝑹𝒕+𝑹𝟑 *Rt

VR3= R3 *I VR3= R3 *I
Function for (.5<t<2) ‫الكهربائية‬Function
‫لسم الهندسة‬ ‫أحمد رافع شريف‬
for (2<t<5)

function dIL=dff1(t,IL) function dIL=dff1(t,IL)


R1=8; R2=8; R3=16; L=4; I=9; R1=8; R2=8; R3=16; L=4; I=9;
Rt=(R1*R2)/(R1+R2); Rt=R2;
VRt= (I*Rt*R3)/(Rt+R3); VRt= (I*Rt*R3)/(Rt+R3);
VR3=R3*IL VR3=R3*IL

dIL=(VRt/L) - (VR3/L); dIL=(VRt/L) - (VR3/L);

end end

clc
clear
[t,IL]=ode23('dff1',.5,2,3);
plot(t,IL,'linewidth',3)
grid on
hold on
t=[0 .5]; IL=[3 3];
plot(t,IL,'linewidth',3)
hold on
[t,IL]=ode23('dff2',2,5,1.8);
plot(t,IL,'linewidth',3)
title('Inductor current')
xlabel('Time')
Ylabel('IL(t)')
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Three-phase

Ex: For the circuit shown below find VAN ,VBN and VCN .

V1=110
V1
L0 I1

A N
Z1=1+j1 Ω ZA=5+j12
V2=110V2 L -120 I2 Ω ohm
B
Z2=1-j2 Ω ZB=5+j4Ω
110 Vrms
110 Vrms
V3=110V3 L -240 I3 ohm
50 Hz C
50
110Hz
0° Vrms

50 Hz Z3=1-j0.5 Zc=5-j12Ω

Ω ohm
IN

----------------------------------------------------------

Solution:

clc
clear
V1=110 ; V2=110*exp(-i*120*pi/180); V3=110*exp(-i*240*pi/180);
Z1=1+1i; Z2=1-2i; Z3=1-0.5i;
ZA=5+12i; ZB=5+4i; ZC=5-12i;
I1=V1/(Z1+ZA) ; VAN=I1*ZA
I2=V2/(Z2+ZB); VBN=I1*ZB
I3=V3/(Z3+ZC); VAC=I1*ZC
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Ex: For the circuit shown below if V1=120 ZA=10+j5 , ZB=15+j7 and

ZC=12-j3 find I1 ,I2 and I3

V1
R1


I1 ZA

V2
I3
R2 110
110Vrms
Vrms
ZC
2Ω 5050HzHz
0°0°
I2 ZB
V3
R3

----------------------------------------------------------

Solution:

clc
clear
V1=120 ; V2=120*exp(-i*120*pi/180); V3=120*exp(-i*240*pi/180);
R1=1; R2=2; R3=1;
ZA=10+5i; ZB=15+7i; ZC=12-3i;
Z=[R1+ZA+R2 -R2 -ZA
-R2 R2+ZB+R3 -ZB
-ZA -ZB ZA+ZB+ZC ];
V=[V1;V2;V3];
I=inv(Z)*V;
I1=I(1)
I2=I(2)
I3=I(3)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Image Processing

clc
clear
a=imread('peppers.png');
imshow(a)

clc
clear
a=imread('peppers.png');
a(:,:,1)=0;
imshow(a)

clc
clear
a=imread('peppers.png');
a(:,:,2)=0;
imshow(a)

clc
clear
a=imread('peppers.png');
a(:,:,3)=0;
imshow(a)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

clc
clear
a=imread('peppers.png');
[x,y,z]=size(a);
a(1:50,1:50,:)=0;
a(1:50,y-50:y,:)=0;
a(x-50:x,y-50:y,:)=0;
a(x-50:x,1:50,:)=0;
imshow(a)

clc
clear
a=imread('peppers.png');
[x,y,z]=size(a)
for i=1:x
for j=1:y
if (i+j)>60 && (i+j)<90
b(i,j,:)=0;
else
b(i,j,:)=a(i,j,:);
end
end
end
imshow(b)

clc
clear
a=imread('peppers.png');
[x,y,z]=size(a);
for k=1:30:x
a(k:k+20,:,2)=256;
end
imshow(a)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

a=imread('peppers.png');
b=imresize(a,[400 400]);
[x,y,z]=size(b);
for k=1:25:x+25
b(:,k:k+1,2)=256;
end
for i=1:25:y+25
b(i:i+1,:,2)=256;
end
imshow(b)

clc
clear
b(1:400,1:400,:)=0;
[x,y,z]=size(b);
for j=1:100:x
for m=1:100:y
b(j:j+50,m:m+50,:)=256;
end
end
for k=50:100:x
for i=50:100:y
b(k:k+50,i:i+50,:)=256;
end
end
imshow(b)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬
clc
clear
b=imread('peppers.png');
c=imresize(b,[800 800]);
a=imresize(b,[600 600]);
c(101:700,101:700,:)=a(1:600
,1:600,:);
c(1:50,:,:)=0;
c(750:800,:,:)=0;
c(:,750:800,:)=0;
c(:,1:50,:)=0;
c(51:750,51:100,:)=0;
c(51:750,51:100,:)=256;
c(51:750,700:750,:)=0;
c(51:750,700:750,:)=256;
c(51:100,50:750,:)=0;
c(51:100,50:750,:)=256;
c(700:750,50:750,:)=0;
c(700:750,50:750,:)=256;
imshow(c)

clc
clear
x=imread('peppers.png');
a=imresize(x,[300 300]);
b=a;
a(:,:,2)=0;
b(:,:,3)=0;
for k=1:3

c1(:,:,k)=triu(a(:,:,k));
c2(:,:,k)=tril(b(:,:,k));
end
c=c1+c2;
imshow(c)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬
clc
clear
a=imread('peppers.png');
aa=imresize(a,[600 600]);
b=imresize(a,[400 400]);
aa(101:500,101:500,:)=b(1:40
0,1:400,:);
% ‫االطار االسىد‬
aa(1:26,:,:)=0;
aa(575:600,:,:)=0;
aa(:,1:26,:)=0;
aa(:,575:600,:)=0;
% ‫االطار االحمر‬
aa(26:50,51:550,:)=0;
aa(26:50,51:550,1)=256;
aa(551:575,51:550,:)=0;
aa(551:575,51:550,1)=256;
aa(26:575,26:50,:)=0;
aa(26:575,26:50,1)=256;
aa(26:575,551:575,:)=0;
aa(26:575,551:575,1)=256;
% ‫االطار االخضر‬
aa(51:75,51:551,:)=0;
aa(51:75,51:551,2)=256;
aa(526:550,51:551,:)=0;
aa(526:550,51:551,2)=256;
aa(76:525,51:75,:)=0;
aa(76:525,51:75,2)=256;
aa(76:525,526:550,:)=0;
aa(76:525,526:550,2)=256;
% ‫االطار االزرق‬
aa(76:100,76:525,:)=0;
aa(76:100,76:525,3)=256;
aa(501:525,76:525,:)=0;
aa(501:525,76:525,3)=256;
aa(101:500,76:101,:)=0;
aa(101:500,76:101,3)=256;
aa(101:500,501:525,:)=0;
aa(101:500,501:525,3)=256;
imshow(aa)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

)2013 ‫(امتحان الفصل الثاني‬

clear
clc
[x,map]=imread('trees.tif');
yy=ind2rgb(x,map)
y=imresize(yy,[300 300]);
z(1:600,1:600,1:3)=0;
z(1:300,1:300,:)=y(1:300,1:300,:);
z(1:300,301:600,1)=y(1:300,1:300,1);
z(301:600,1:300,2)=y(1:300,1:300,2);
z(301:600,301:600,3)=y(1:300,1:300,3);
imshow(z);
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

)2013 ‫(االمتحان النهائي‬

-: ‫مطلوب دمج الصورتين كما موضح في الشكل ادناه‬

clc
clear
A=imread('ngc6543a.jpg');
a=imresize(A,[200 200]);
B=imread('peppers.png');
BB=imresize(B,[400 400]);
Bb=imrotate(BB,45);
b=imresize(Bb,[400 400]);
b1(1:200,1:200,:)=b(1:200,1:200,:);
b2(1:200,1:200,:)=b(201:400,1:200,:);
b3(1:200,1:200,:)=b(1:200,201:400,:);
b4(1:200,1:200,:)=b(201:400,201:400,:);
for i=1:3
c1(:,:,i)=triu(b2(:,:,i));
c2(:,:,i)=tril(a(:,:,i));
end
f1=c1+c2;
for j=1:3
e1(:,:,j)=tril(b3(:,:,j));
e2(:,:,j)=triu(a(:,:,j));
end
f2=e1+e2;
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

for k=1:200
for l=1:200
if (k+l)>1 &&(k+l)<=200
z1(k,l,:)=a(k,l,:);
else
z1(k,l,:)=b1(k,l,:);
end
end
end
for r=1:200
for t=1:200
if (t+r)>2 &&(t+r)<=200
z2(r,t,:)=b4(r,t,:);
else
z2(r,t,:)=a(r,t,:);
end
end
end
b(1:200,201:400,:)=f2(1:200,1:200,:);
b(201:400,1:200,:)=f1(1:200,1:200,:);
b(201:400,201:400,:)=z2(1:200,1:200,:);
b(1:200,1:200,:)=z1(1:200,1:200,:);
imshow(b)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

3D Graphics in MATLAB

clc
clear
[x,y,z]= sphere
surf(x,y,z)

clc
clear
[x,y,z]= cylinder
surf(x,y,z)

clc
clear
[x1,y1,z1]= sphere
[x2,y2,z2]= cylinder
surf(3*x1,3*y1,z1+2)
hold on
surf(x2,y2,4*z2)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(2+cos(t));
surf(X,Y,Z)
axis square

t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(2+sin(t));
surf(X,Y,Z)
axis square

clc
clear
t=0:pi/200:2*pi
plot3(sin(10*t),cos(10*t),t)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

clc
clear
t=0:pi/200:2*pi
plot3(exp(-t/10).*sin(10*t),exp(-t/10).*cos(10*t),t)

clc
clear
t=0:pi/200:2*pi
plot3(exp(t/10).*sin(10*t),exp(t/10).*cos(10*t),t)
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Animations in MATLAB
EX: Write a MATLAB program to plot animated sine wave :

----------------------------------------------------------

Solution

clc
clear
t=0:pi/200:2*pi;
y=sin(t);
h=plot(t,y);
set(h,'Ydatasource','y')
for k=1:.1:10
y=sin(t.*k);
refreshdata(h)
drawnow;
pause(.1)
end

% %‫طريقة ثاوية‬
% t=0:pi/200:2*pi;
% for k=1:.1:10
% y=sin(k*t);
% plot(t,y)
% pause(.1)
%
% end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: Write a MATLAB program to roll a wire around the z-axis (like
a helix):
----------------------------------------------------------

Solution

clc
clear
t=0:pi/200:2*pi;
x=sin(t);
y=cos(t);
h=plot3(x,y,t);
set(h,'xdatasource','x','Ydatasource','y');
for k=1:.05:10
x=sin(t.*k);
y=cos(t.*k);
refreshdata(h)
drawnow;
pause(.1)

end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: Write a MATLAB program to roll a wire around the z-axis (like
a snail):
----------------------------------------------------------

Solution

clc
clear
t=0:pi/200:2*pi;
x=sin(t)
y=cos(t)
h=plot3(x,y,t);
set(h,'xdatasource','x','Ydatasource','y');
for k=1:.05:10
x=sin(t.*k).*exp(-t/5);
y=cos(t.*k).*exp(-t/5);
refreshdata(h)
drawnow;
pause(.1)

end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: Write a MATLAB program to compress and expand a snail


----------------------------------------------------------

Solution

clc
clear
t=0:pi/200:20*pi;
x=sin(t).*exp(-t/100)
y=cos(t).*exp(-t/100)
plot3(x,y,t)

for k=1:.05:5

plot3(x,y,t*k)
pause(.1)
end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: Write a MATLAB program to plot and animate a sphere along


z-axes …
----------------------------------------------------------

Solution

clc
clear
[x y z]= sphere

for k=1:pi/10:5*pi
surf(x,y,z+sin(k));
pause(.1)
end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

EX: Write a MATLAB program to rotate a sphere around


another constant sphere
)2013/ ‫(االمتحان انىهائي‬
----------------------------------------------------------

Solution

clc
clear
[x,y,z]=sphere;
surf(5*x,5*y,5*z);
set(gca,'nextplot','replacechildren');
axis tight
for k=1:1500
surf(x,y,z)
hold on
a=5*sin(k/50);
b=5*cos(k/50);
surf(a+x,b+y,z)
set(gca,'nextplot','replacechildren');
f(k)=getframe
end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

) ‫ كرة تنزل في سلة‬/ ‫(سؤال من اسئلة االعوام‬

clc
clear
a=1;
b=1;
c=1;
hold on
for i=1:10:140
[a b c]=sphere
surf(a,b/.5+i,c+(10*sin(i/40))+2)
pause(5)
[x y z]=cylinder(11)
surf(x,y+87,z+9.5)

end
‫لسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

You might also like