% Write a Live Script to calculate the total impedance, current through and voltage across each component of an RLC circuit.   First write two functions to calculate the reactance of a capacitor and inductor. Next, write a function to calculate the total impedance of the three components.  Then write a the function to calculate the total current.  Using the current and its phase, write three more functions to calculate the voltage across the resistor, capacitor, and inductor.
% Replace the question marks with variables in the functions.
%

%This is the script part of the Live Script
V = 9;
R = 0.1; %Change this value for your circuit
C = 2.2e-6; %Change this value for your circuit
L = 0.2e-3; %Change this value for your circuit
f = 800; %Change this value for your circuit
Xc = CReactance(f,C)
XL = LReactance(f,L)
[Zs,phaseS] = imped_series(R,Xc,XL)
plot(f,Zs)
xlabel('frequency')
ylabel('impedance')
[Itot,Iphasetot] = tot_current(V,Zs,phaseS)
[VRs,VRphaseS] = Rvoltage(R,Itot,Iphasetot)
[VCs,VCphaseS] = Cvoltage(Xc,Itot,Iphasetot)
[VLs,VLphaseS] = Lvoltage(XL,Itot,Iphasetot)
plot(f,VCs,'r',f,VLs,'b')%compare the voltages at the 
%BASS and HIGH frequencies for the Cap for the BASS circuit
%and the Ind for the HIGH circuit at 800 Hz and 5000 Hz

%
% Below are the six functions we use in the script above.
%
%
%

function Xc = CReactance(??,??)
    Xc = 1./(??*??*??*??);
end

%

function XL = LReactance(??,??)
    XL = ??*??*??*??;
end

%

function [Zs, phaseS] = imped_series(??,??,??)
    Zimag = ?? - ??;
    Zreal = R;
    Zs = sqrt(??.^2 + ??.^2);
    if R == 0
        if Zs>0
            phaseS = ??;
        else
            phaseS = ??;
        end
    else
        phaseS = atand(??./??);
    end
end

%

function [Itot,Iphasetot] = tot_current(??,??,??)  
    Itot = V./Zs;
    Iphasetot = 0 - ??;
end

%

function [VRs,VRphaseS] = Rvoltage(??,??,??)  
    VRs = ??*??;
    VRphaseS = ??;
end

%

function [VCs,VCphaseS] = Cvoltage(??,??,??)  
    VCs = ??.*??;
    VCphaseS = Iphasetot-??;
end

%

function [VLs,VLphaseS] = Lvoltage(??,??,??)  
    VLs = ??.*??;
    VLphaseS = Iphasetot+??;
end