E11 Phasors (w/ Matlab) Matlab for Phasor -- E11................................................................................................. 1 Describe input voltage as a phasor.................................................................................. 1 Define impedances.......................................................................................................... 1 Find current..................................................................................................................... 2 Plot current and voltage as function of time ................................................................... 2 Calculate other phasors ................................................................................................... 3 Plot phasor for total voltage............................................................................................ 3 Plot other phasors............................................................................................................ 4 Demonstrate how phasors add ........................................................................................ 5 Plot inductor voltage ....................................................................................................... 5 Add resistor voltage ........................................................................................................ 5 Add capacitor voltage ..................................................................................................... 6 Add plot for total voltage................................................................................................ 6 Show how voltage add in time domain........................................................................... 7

Matlab for Phasor -- E11 clear all clc

Describe input voltage as a phasor Input amp=50, freq=60Hz, phase=30 degrees. Vmag=50; w=60*2*pi; %Convert Hz frequency to radians/second Vphase=30*pi/180; %Convert phase angle to radians V=Vmag*exp(j*Vphase); fprintf('Voltage: Mag=%g, Phase=%g rad (%g deg)\n',... Vmag,Vphase,Vphase*180/pi);

Voltage: Mag=50, Phase=0.523599 rad (30 deg)

Define impedances. C=10 uF, R=250 Ohm, L=300 mH. C=10E-6; R=250; L=300E-3; Zc=1/(j*w*C) Zr=R Zl=j*w*L Z=Zc+Zr+Zl Zmag=abs(Z); Zphase=angle(Z); fprintf('Impedance: Mag=%g, Phase=%g rad (%g deg)\n',... Zmag,Zphase,Zphase*180/pi);

Zc = 0 -2.6526e+002i Zr = 250 Zl = 0 +1.1310e+002i

Page 1 of 10

Z = 2.5000e+002 -1.5216e+002i Impedance: Mag=292.665, Phase=-0.546751 rad (-31.3265 deg)

Find current I=V/Z; Imag=abs(I); Iphase=angle(I); fprintf('Current: Mag=%g, Phase=%g rad (%g deg)\n',... Imag,Iphase,Iphase*180/pi);

Current: Mag=0.170844, Phase=1.07035 rad (61.3265 deg)

Plot current and voltage as function of time t=linspace(0,0.03); subplot(211); plot(t,Vmag*cos(w*t+Vphase)); title('Voltage vs. time'); ylabel('Volts'); grid subplot(212); plot(t,Imag*cos(w*t+Iphase)); title('Current vs. time'); xlabel('Time (seconds)'); ylabel('Amps');

grid

Note phase difference between voltage and current (also magnitude is different).

Page 2 of 10

Calculate other phasors Vc=Zc*I Vr=Zr*I Vl=Zl*I

%Voltage across capacitor %Voltage across resistor %Voltage across inductor

Vc = 39.7603 -21.7442i Vr = 20.4934 +37.4732i Vl = -16.9525 + 9.2710i

Plot phasor for total voltage clf %Clear figure %Plot total voltage phasor (Matlab doesn't do arrows, so mark point with %diamond. plot([0 real(V)],[0 imag(V)],'r','LineWidth',2); %phasor w/o mark at end. hold on; %Don't erase before next plot plot(real(V),imag(V),'rd','MarkerFaceColor','r'); %Add filled diamond. axis([-60 60 -60 60]); grid

Page 3 of 10

Plot other phasors %Plot capacitor voltage phasor plot([0 real(Vc)],[0 imag(Vc)],'g','LineWidth',2); %phasor w/o mark at end. plot(real(Vc),imag(Vc),'gd','MarkerFaceColor','g'); %Add filled diamond. %Plot resistor voltage phasor plot([0 real(Vr)],[0 imag(Vr)],'b','LineWidth',2); %phasor w/o mark at end. plot(real(Vr),imag(Vr),'bd','MarkerFaceColor','b'); %Add filled diamond. %Plot inductor voltage phasor plot([0 real(Vl)],[0 imag(Vl)],'m','LineWidth',2); %phasor w/o mark at end. plot(real(Vl),imag(Vl),'md','MarkerFaceColor','m'); %Add filled diamond. title('Voltage Phasors'); legend('Total',' ','Capacitor',' ','Resistor',' ','Inductor',... 'location','SouthWest'); hold off

Page 4 of 10

Demonstrate how phasors add Plot inductor voltage x0=0; y0=0; x1=real(Vl); y1=imag(Vl); plot([x0 x1],[y0 y1],'m','LineWidth',2); %phasor w/o mark at end. hold on; axis([-60 60 -60 60]); grid; plot(x1,y1,'md','MarkerFaceColor','m'); %Add filled diamond. legend('Inductor','location','SouthWest'); title('Inductor phasor');

Add resistor voltage x0=x1; y0=y1; x1=x0+real(Vr); y1=y0+imag(Vr); plot([x0 x1],[y0 y1],'b','LineWidth',2); %phasor w/o mark at end. plot(x1,y1,'bd','MarkerFaceColor','b'); %Add filled diamond. legend('Inductor',' ','Resistor','location','SouthWest'); title('Inductor + Resistor phasor');

Page 5 of 10

Add capacitor voltage x0=x1; y0=y1; x1=x0+real(Vc); y1=y0+imag(Vc); plot([x0 x1],[y0 y1],'g','LineWidth',2); %phasor w/o mark at end. plot(x1,y1,'gd','MarkerFaceColor','g'); %Add filled diamond. legend('Inductor',' ','Resistor',' ','Capacitor','location','SouthWest'); title('Inductor + Resistor + Capacitor phasor');

Add plot for total voltage plot([0 real(V)],[0 imag(V)],'r','LineWidth',2); %phasor w/o mark at end. plot(real(V),imag(V),'rd','MarkerFaceColor','r'); %Add filled diamond. legend('Inductor',' ','Resistor',' ','Capacitor',' ','Total',... 'location','SouthWest'); title('Inductor + Resistor + Capacitor phasor and the total Voltage'); hold off

Page 6 of 10

Show how voltage add in time domain v=abs(V)*cos(w*t+angle(V)); vc=abs(Vc)*cos(w*t+angle(Vc)); vr=abs(Vr)*cos(w*t+angle(Vr)); vl=abs(Vl)*cos(w*t+angle(Vl)); plot(t,v,'r',t,vc,'g',t,vr,'b',t,vl,'m'); grid; title('Voltages vs. time'); xlabel('Time (S)'); ylabel('Voltage (V)'); legend('Total','Capacitor','Resistor','Inductor');

Page 7 of 10

Page 8 of 10

Matlab Code %% Matlab for Phasor -- E11 clear all clc %% Describe input voltage as a phasor % Input amp=50, freq=40Hz, phase=30 degrees. Vmag=50; w=60*2*pi; %Convert Hz frequency to radians/second Vphase=30*pi/180; %Convert phase angle to radians V=Vmag*exp(j*Vphase); fprintf('Voltage: Mag=%g, Phase=%g rad (%g deg)\n',... Vmag,Vphase,Vphase*180/pi); %% Define impedances. % C=10 uF, R=250 Ohm, L=300 mH. C=10E-6; R=250; L=300E-3; Zc=1/(j*w*C) Zr=R Zl=j*w*L Z=Zc+Zr+Zl Zmag=abs(Z); Zphase=angle(Z); fprintf('Impedance: Mag=%g, Phase=%g rad (%g deg)\n',... Zmag,Zphase,Zphase*180/pi); %% Find current I=V/Z; Imag=abs(I); Iphase=angle(I); fprintf('Current: Mag=%g, Phase=%g rad (%g deg)\n',... Imag,Iphase,Iphase*180/pi); %% Plot current and voltage as function of time t=linspace(0,0.03); subplot(211); plot(t,Vmag*cos(w*t+Vphase)); title('Voltage vs. time'); ylabel('Volts'); grid subplot(212); plot(t,Imag*cos(w*t+Iphase)); title('Current vs. time'); xlabel('Time (seconds)'); ylabel('Amps');

grid

%% % Note phase difference between voltage and current (also magnitude is % different). %% Calculate other phasors Vc=Zc*I %Voltage across capacitor Vr=Zr*I %Voltage across resistor Vl=Zl*I %Voltage across inductor %% Plot phasor for total voltage clf %Clear figure %Plot total voltage phasor (Matlab doesn't do arrows, so mark point with %diamond. plot([0 real(V)],[0 imag(V)],'r','LineWidth',2); %phasor w/o mark at end. hold on; %Don't erase before next plot plot(real(V),imag(V),'rd','MarkerFaceColor','r'); %Add filled diamond. axis([-60 60 -60 60]);

Page 9 of 10

grid %% Plot other phasors %Plot capacitor voltage phasor plot([0 real(Vc)],[0 imag(Vc)],'g','LineWidth',2); %phasor w/o mark at end. plot(real(Vc),imag(Vc),'gd','MarkerFaceColor','g'); %Add filled diamond. %Plot resistor voltage phasor plot([0 real(Vr)],[0 imag(Vr)],'b','LineWidth',2); %phasor w/o mark at end. plot(real(Vr),imag(Vr),'bd','MarkerFaceColor','b'); %Add filled diamond. %Plot inductor voltage phasor plot([0 real(Vl)],[0 imag(Vl)],'m','LineWidth',2); %phasor w/o mark at end. plot(real(Vl),imag(Vl),'md','MarkerFaceColor','m'); %Add filled diamond. title('Voltage Phasors'); legend('Total',' ','Capacitor',' ','Resistor',' ','Inductor',... 'location','SouthWest'); hold off %% Demonstrate how phasors add %% Plot inductor voltage x0=0; y0=0; x1=real(Vl); y1=imag(Vl); plot([x0 x1],[y0 y1],'m','LineWidth',2); %phasor w/o mark at end. hold on; axis([-60 60 -60 60]); grid; plot(x1,y1,'md','MarkerFaceColor','m'); %Add filled diamond. legend('Inductor','location','SouthWest'); title('Inductor phasor'); %% Add resistor voltage x0=x1; y0=y1; x1=x0+real(Vr); y1=y0+imag(Vr); plot([x0 x1],[y0 y1],'b','LineWidth',2); %phasor w/o mark at end. plot(x1,y1,'bd','MarkerFaceColor','b'); %Add filled diamond. legend('Inductor',' ','Resistor','location','SouthWest'); title('Inductor + Resistor phasor'); %% Add capacitor voltage x0=x1; y0=y1; x1=x0+real(Vc); y1=y0+imag(Vc); plot([x0 x1],[y0 y1],'g','LineWidth',2); %phasor w/o mark at end. plot(x1,y1,'gd','MarkerFaceColor','g'); %Add filled diamond. legend('Inductor',' ','Resistor',' ','Capacitor','location','SouthWest'); title('Inductor + Resistor + Capacitor phasor'); %% Add plot for total voltage plot([0 real(V)],[0 imag(V)],'r','LineWidth',2); %phasor w/o mark at end. plot(real(V),imag(V),'rd','MarkerFaceColor','r'); %Add filled diamond. legend('Inductor',' ','Resistor',' ','Capacitor',' ','Total',... 'location','SouthWest'); title('Inductor + Resistor + Capacitor phasor and the total Voltage'); hold off %% Show how voltage add in time domain v=abs(V)*cos(w*t+angle(V)); vc=abs(Vc)*cos(w*t+angle(Vc)); vr=abs(Vr)*cos(w*t+angle(Vr)); vl=abs(Vl)*cos(w*t+angle(Vl)); plot(t,v,'r',t,vc,'g',t,vr,'b',t,vl,'m'); grid; title('Voltages vs. time'); xlabel('Time (S)'); ylabel('Voltage (V)'); legend('Total','Capacitor','Resistor','Inductor');

Page 10 of 10