You are on page 1of 63

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

, 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Ω
I R3 R6
8Ω 15Ω

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

:Solution

-:The equations

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

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

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

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
V4 V3 V1
2Ω 3Ω

I3 I2
R2 4 A 6A

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
R4
I2 I1
R2 2Ω

R3 R6

2Ω 2Ω

V2 R8
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;
I4 I3
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
a L
RS

100Ω 50mH
c b
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

.Find the current -1

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

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

.Sketch the input voltage for two period -1

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

.calculate the condution angle of the diode -3

. calculate the peak current -4

R D

200Ω DIODE_VIRTUAL
VS
VB
18 Vpk 18Sin(100πt)
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Ω
50 Hz 18Sin(100πt)
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

.Find the current -1

Plot the Voltage and current waveforms for two period -2

R2 D1
4
50Ω DIODE_VIRTUAL

V1
230 Vrms 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;1 R2=50; VB=6; VS
T=1/f;l=30*10^-3; xl=w*l*i; c=300*10^-6; xc=-
i/(w*c) 230Sin(100πt)
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

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

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

The inteqral equation -3

plot two figures in the same frame and compare between first and second -4
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( ‫ قيمة‬C‫عند اختيار‬

: EX: For the curve shown in fig , Write a MATLAB program to find
‫قسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

The equation of the curve at x=4.5 -1

The slop of the curve at x=5 -2

The integration of the curve-3

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

: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=uns(t,to) function y= ramp1(t,to)


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

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 = ‫الميل‬

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

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


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 = ‫الميل‬

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

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

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 = ‫الميل‬

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

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

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

2
at x=4, y=2

then c=-2/3

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

:Solution

function y=rp(t,to)
The program:- for k=1:length(t)
m=2/3;
clc c=-2/3
clear if t(k)<to
t=0:.1:10 y(k)=0
f1=rp(t,1)-rp(t,4) else
y(k)=((m*t(k))
f2=rp(t,9)-rp(t,6) +c)-((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

4 s 2+ 6 S oV (s)
6 s 3+52 s 2+03 s+ 9
=sV(s)
=H(s)

: Write a MATLAB program to

. find the poles and zeros of the system -1


% ‫ الفرع الثالث‬c clc
s=-3+2i;
plot the frequency response (magnitude in dB andclear
phase in degree versus frequency) for -2
vs=10*exp(i*40*pi/180);
.(w) varies from 0.1 to 70000 rad/sec % ‫الفرع االول والثاني‬
n=polyval(num,s); num=[4 6 0];
.if Vs(t)=10exp(-3t) cos(2t+40) find Vo(t) -3
dn=polyval(dnum,s); dnum=[6 25 30 9];
vo=vs*n/dn poles=roots(num)
. convert the network function to the partial faction form-4
zeros=roots(dnum)
% ‫ الفرع الرابع‬d ----------------------------------------------------------
w=logspace(-1,4.845);
[r,p,k]= residue(num,dnum) f=w/(2*pi);
:Solution 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

R
( )s
L oV (s)
R 1 =sV (s) =H(s)
s 2+ ( )s+
L CL
L C

subplot(222) 5H clear
semilogx(f,m2) 1.12µF clc
ylabel('mag') L=5; C=1.12*10^-6; R1=10000; R2=100;
xlabel('freq')
Vi R
230 Vrms
title('mag vs freq50at
25Ω
Hz
R=100 ohm') num1=[R1/L 0]; dnum1=[1 R1/L 1/L*C];
0° num2=[R2/L 0]; dnum2=[1 R2/L 1/L*C];
subplot(223)
semilogx(f,th1) zeros1=roots(num1)
ylabel('Theta') zeros2=roots(num2)
xlabel('freq')
title('Theta vs freq at R=10000 ohm') poles1=roots(dnum1)
----------------------------------------------------------
poles2=roots(dnum2)
subplot(224)
:Solution
semilogx(f,th2) %--------------------------------------
ylabel('Theta') w=logspace(log10(1),log10(10000));
xlabel('freq') f=w/(2*pi);
Plot the frequency response for (w) varies
title('Theta vs freq at R=100 ohm')
%------------------------------------------ from 1 to 10000 rad/sec and find Vo(t)
h1=freqs(num1,dnum1,f);
s=10i; : when
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) R=10000Ω 2-R=100Ω-1
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

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

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

2kΩ

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

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

: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√ 2 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

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

VO
----------------------------------------------------------

: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

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

. Find discharge time of the capacitor-2

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

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

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

. Using a numerical solution to the differential equation -1

. Using analytical solution -2

. Assuming Vo(0)=0

R
Vo(t) :Solution
10kΩ
ic=ir

SV −oV ( t) oVd ( t)
VS C = C
R td
10 V 10µF
oV (t) SV oVd ( t)
-
CR
=
CR td

-: The analytical solution

−t
Vo(t)=10( 1 - exp( ) )
RC

The programe The function


function dVo=dif(t,Vo)
clc VS=10; R=10000;
clear C=10*10^-6 ; RC=R*C;
to=0; tf=1; Voo=0; R=10000; dVo=(VS/RC)- (Vo/RC);
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

R1 R3
VO(t)
:Solution
20kΩ 10kΩ
ic=ir

VS R2 C SV −oV ( t) oVd ( t)
30 V 40kΩ = C
1µF R td

oV ( t) oVd ( t)
-=
CR td

The program The function


clc function dVo=df(t,Vo)
clear R2=40000;
VS=30; R3=10000;
R1=20000; C=10*10^-6;
R2=40000; taw=(R3+R2)*C;
R3=10000; dVo=-Vo/taw;
C=10*10^-6; end
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 2<t<5 At .5<t<2

Rt= R2 R 1∗ R 2
=Rt
R 1+R 2
VL=VRt – VR3
VL=VRt – VR3
dI (t)
L =VRt-Vr3 dI (t)
dt L =VRt-Vr3
dt
tRV RV 3 Id ( t)
- = RV 3 tRV
L L td Id ( t)
- =
L L td
I∗R 3
VRt= *Rt I∗R 3
Rt + R 3
VRt= *Rt
Rt + R 3
VR3= R3 *I
VR3= R3 *I
‫قسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

Function for (2<t<5) Function for (.5<t<2)

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=R2; Rt=(R1*R2)/(R1+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

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

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


ZA I1
V2
I3 R2 110
110Vrms
Vrms
2Ω 5050HzHz
0°0°
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
clc clear
clear a=imread('peppers.png');
a=imread('peppers.png'); imshow(a)
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');
clc
[x,y,z]=size(a);
clear
a(1:50,1:50,:)=0;
a=imread('peppers.png');
a(1:50,y-50:y,:)=0;
[x,y,z]=size(a)
a(x-50:x,y-50:y,:)=0;
for i=1:x
a(x-50:x,1:50,:)=0;
for j=1:y
imshow(a)
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
clc
imshow(b)
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;
clc
c(:,1:50,:)=0;
clear
c(51:750,51:100,:)=0;
x=imread('peppers.png');
c(51:750,51:100,:)=256;
a=imresize(x,[300 300]);
c(51:750,700:750,:)=0;
b=a;
c(51:750,700:750,:)=256;
a(:,:,2)=0;
c(51:100,50:750,:)=0;
b(:,:,3)=0;
c(51:100,50:750,:)=256;
for k=1:3
c(700:750,50:750,:)=0;
c1(:,:,k)=triu(a(:,:,k));
c(700:750,50:750,:)=256;
c2(:,:,k)=tril(b(:,:,k));
imshow(c)
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:400,
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 t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(2+cos(t));
surf(x2,y2,4*z2)
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(exp(-t/10).*sin(10*t),exp(-t/10).*cos(10*t),t)

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)
‫قسم الهندسة الكهربائية‬ ‫أحمد رافع شريف‬

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