%  This script evaluate integrals symbolically to compute RMS values
%  for non sinusoidal waveforms.  The integration intervals and terms
%  were derived from a power analysis problem for a diode circuit
%  presented in the lecture note for Lab 3 in EE462G
%
%  written by Kevin D. Donohue (donohue@engr.uky.edu) Sept 2005


%define symbol for equation analysis
syms t
% Compute intersection of sine with the 1.4 voltage amplitude
% to find integration regions
t1 = solve('5*sqrt(2)*sin(2*pi*1e3*t) = 1.4', t)
%  define period of signal
tp = 1e-3
% Compute other intersection point of sine with the 1.4 volt level 
t2 = tp/2 - t1
%  Define expression for source voltage as function of t
v1 = 5*sqrt(2)*sin(2*pi*1e3*t)
%  RMS source voltage
v1rms = 5
%  Integrate for rms currents
i1rms = sqrt(int(((2*v1-.7)/3e3)^2,t,t1,t2)/tp + int((v1/2e3)^2,t,t2,tp+t1)/tp)
i2rms = sqrt(int(((v1+0.7)/3e3)^2,t,t1,t2)/tp + int((v1/2e3)^2,t,t2,tp+t1)/tp)
i3rms = sqrt(int(((v1-1.4)/3e3)^2,t,t1,t2)/tp)
%  Power delivered by source
pavsource = (int(v1*(2*v1-.7)/3e3,t,t1,t2)/tp +int(v1*v1/2e3,t,t2,tp+t1)/tp)%  This integral matches the
                                                 %  voltage and current
                                                 %  waveforms for correct
                                                 %  average power
% pavsource = v1rms*i1rms   %  This is not accurate becuase it does not match the
                       %   the voltage and currents with proper timing
                       %   but it is approximate and corresponds to what 
                       %   you measure in the lab.
% Compute powers in all components (all resistors were 1e3 ohms)
pavr1 = i1rms^2*1e3
pavdiode = (.7*int(((v1-1.4)/3e3),t,t1,t2)/tp)   %  This integral matches the
                                                 %  voltage and current
                                                 %  waveforms for correct
                                                 %  average power
%pavdiode = i3rms*.7   %  This is not accurate becuase it does not match the
                       %   the voltage and currents with proper timing
                       %   but it is approximate and corresponds to what 
                       %   you measure in the lab.
pavr3 = i3rms^2*1e3
pavr2 = i2rms^2*1e3
%  by conservation of power all absorbing components should add up to power
%  delivered
pavabsorbed = pavr1+pavr2+pavr3+pavdiode
disp(['Compare power absorbed: ' num2str(double(pavabsorbed)) 'W to power supplied: ' num2str(double(pavsource)) 'W'])
