myFirstRCnetwork.m

fileName = 'myFirstRCnetwork'; % file name without extension assigned to the variable: "fileName"

makeNetlist(fileName); % This will create a netlist running LTspice in batch mode

% it will be named: .cir

checkCircuit(fileName); % This check the netlist and creates the MNA equation of the circuit

htmlPage('Circuit data'); % This creates a web page with the circuit data

head2html('Circuit diagram'); % Places a header (level 2) on the web page

img2html([fileName, '_asc.png'], 369); % This places the picture of the circuit on the web page (width=369px)

netlist2html(fileName); % This places the netlist on the web page

elementData2html(); % This places all the properties of all the network elements on the web page

params2html(); % This lists the symbolic and numeric values of all network parameters on the web page

%

% obtain the symbolic MNA matrix equation of the network %

%

simType('symbolic');

gainType('vi');

dataType('matrix');

MNA = execute();

htmlPage('Matrix equations');

matrices2html(MNA);

%

htmlPage('Matrix equation variables'); % Create a new html page to display the separate variables of the matrix equation

MNA_vars = MNA.results(1); % MNA variables

I_v = MNA_vars(1); % Vector with independent variables

M = MNA_vars(2); % Matrix

D_v = MNA_vars(3); % Vector with independent variables

% Display text (including LaTeX) on the active html page.

text2html('The vector $I_v$ with the independent variables equals:');

% Display an equation on the active html page.

eqn2html(sym('I_v'), I_v);

text2html('The matrix $M$ equals:');

eqn2html(sym('M'), M);

text2html('The vector $D_v$ with the dependent variables equals:');

eqn2html(sym('D_v'), D_v);

%

% Obtain the Laplace Transform of the source-to-detector transfer of the network

%

source('V1'); % The independent source 'V1' is assigned as signal source

detector('V_out'); % The voltage between node 'out' and node '0' is assigned as detector voltage

gainType('gain'); % The gain from source to load is the desired transfer

dataType('laplace'); % The Laplace Transform is the data we need

% Note: the simulation type is still 'symbolic', we didn't change it

gainLaplace = execute(); % The execution result (structured array) is assigned to the variable 'gainLaplace'

% We will unpack the structured array since we only need the expression:

gainLaplaceExpr = gainLaplace.results(1);

htmlPage('Laplace Transform'); % Create a new html page.

head2html('Laplace transform of the gain'); % Put a heading on it

text2html('The Laplace Transform of the gain is found as:'); % Put some text on the page

syms('V_out', 'V_1'); % Define symbolic variables for the left hand side of the equation

eqn2html(V_out/V_1, gainLaplaceExpr); % Place the equation on the page.

% Obtain the numeric Laplace Transform of the source-to-detector transfer of the network

%

simType('numeric'); % Set the simulation type to numeric.

%

% Data type is still 'laplace' which is required for plotting frequency characteristics

% Source, detector and gain type have already been defined.

gainLaplaceNumeric = execute();

% Create MATLAB figure objects, later we will place them on an html page.

figMag = plotMag('Magnitude plot of the gain', gainLaplaceNumeric, 10, 1e5, 100);

figdBmag = plotdBmag('dB Magnitude plot of the gain', gainLaplaceNumeric, 10, 1e5, 100);

figPhase = plotPhase('Phase plot of the gain', gainLaplaceNumeric, 10, 1e5, 100);

figDelay = plotDelay('Group delay of the gain', gainLaplaceNumeric, 10, 1e5, 100);

% Display the plots on an html page

%

htmlPage('Plots of the gain'); % Create a new html page

fig2html(figMag, 'figMag.svg', 800) % save figMag as figMag.svg in the 'html/figures' subdirectory

% and place it on the page with a width of 800px

fig2html(figdBmag, 'figdBmag.svg', 800) % As above, but now for figdBmag

fig2html(figPhase, 'figPhase.svg', 800) % As above, but now for figPhase

fig2html(figDelay, 'figDelay.svg', 800) % As above, but now for figDelay

% Calculate the DC gain, the poles and the zeros of the gain using data type 'pz', simulation type 'numeric' and gain type 'gain'.

% The latter two have already been defined correctly.

dataType('pz'); % Define the data type for pole-zero analysis

gainPolesZeros = execute(); % Calculate the DC gain, the poles and the zeros

listPZ(gainPolesZeros); % Display the results in the MATLAB Command Window

htmlPage('Pole-zero analysis'); % Create an html page for displaying the results

pz2html(gainPolesZeros); % Display the results on the html page.

% Generate the plot: x-axis: -1900...100Hz, y-axis: -1000...1000Hz.

figPZ = plotPZ('Pole-zero plot', gainPolesZeros, [-1900, 100], [-1000, 1000]);

head2html('Plot'); % Put a heading on the page

fig2html(figPZ, 'figPZ.svg', 600);% Place the figure on the html page

% Calculate the poles and the zeros of the gain using data type 'poles', simulation type 'numeric' and gain type 'gain'.

% The latter two have already been defined correctly.

dataType('poles'); % Define the data type for pole analysis

gainPoles = execute(); % Calculate the poles

htmlPage('Separate pole and zero analysis'); % Create an html page for displaying the results

pz2html(gainPoles); % Display the results on the html page.

dataType('zeros'); % Define the data type for zero analysis

gainZeros = execute(); % Calculate the zeros

pz2html(gainZeros); % Display the results on the html page.

% Calculate the unit impulse response of the gain.

simType('symbolic'); % Symbolic simulation

dataType('impulse'); % Calculate the unit impulse response of the gain (gain type has already been set)

gainImpulse = execute(); % Assign the execution result to the variable 'gainImpulse'

gainImpulseExpr = gainImpulse.results(1); % Unpack the execution result and assign the expression to 'gainImpulseExpr'

htmlPage('Unit impulse response'); % Start a new html page

head2html('Symbolic expression'); % Put a heading on it

syms('h(t)'); % Define the left hand side of the equation

eqn2html(h(t), gainImpulseExpr); % Write the equation to the html page

% generate the plot of h(t)

simType('numeric'); % This requires numeric simulation

figImpulse = plotTime('Impulse response', execute(), 0, 1e-3, 100); % Make the time-domain plot

head2html('Plot'); % Put a heading on the page

fig2html(figImpulse, 'figImpulse.svg', 800);% Put the plot on the page

% Calculate the unit step response of the gain.

simType('symbolic'); % Symbolic simulation

dataType('step'); % Calculate the unit impulse response of the gain (gain type has already been set)

gainStep= execute(); % Assign the execution result to the variable 'gainStep'

gainStepExpr = gainStep.results(1); % Unpack the execution result and assign the expression to 'gainStepExpr'

htmlPage('Unit step response'); % Start a new html page

head2html('Symbolic expression'); % Put a heading on it

syms('a(t)'); % Define the left hand side of the equation

eqn2html(a(t), gainStepExpr); % Write the equation to the html page

% generate the plot of h(t)

simType('numeric'); % This requires numeric simulation

figStep = plotTime('Step response', execute(), 0, 1e-3, 100); % Make the time-domain plot

head2html('Plot'); % Put a heading on the page

fig2html(figStep, 'figStep.svg', 800); % Put the plot on the page

% Calculate the settling error as a function of time.

syms('n', 't', 'epsilon_s', 'tau_s'); % Define symbolic variables

eps = 1-gainStepExpr; % Calculate the relative settling error

taus = solve(eps-2^-n, t); % Calculate the settling time

htmlPage('Settling time'); % Start a html page

head2html('Settling time to n-bits'); % Put a heading on it

% Put some text on the page and write the equations on the page

text2html('The relative settling error versus time can be founs as:');

eqn2html(epsilon_s, eps);

text2html('The settling time to $n$ bit can be found as the solution of:');

eqn2html(epsilon_s, 2^-n);

text2html('This yields:');

eqn2html(tau_s, taus);

% Derive the design equations for the resistance R and the capacitance C of the network

% for settling to n bit within tau_s seconds.

syms('R', 'C');

Rsol = solve(taus-tau_s, R); % Symbolic design equation for the resistance

Csol = solve(taus-tau_s, C); % Symbolic design equation for the capacitance

head2html('Component design equations'); % Put a heading on the page

% Put some text on the page and write the equations on the page

text2html(['The resistance $R$ can be written as a function of the capacitance ', ...

'$C$ and the settling time $\\tau_s$ to $n$ bit:']);

eqn2html(R, Rsol);

text2html(['The capacitance $C$ can be written as a function of the resistance ', ...

'$R$ and the settling time $\\tau_s$ to $n$ bit:']);

eqn2html(C, Csol);

% Determine the value of the resistance, given the capacitance and the settling time to 10 bit.

% Substitute numeric values for C, tau_s and n in the expression for Rsol

Rnum = subs(Rsol, [C, tau_s, n], [10e-12, 100e-9, 10]);

head2html('Numeric example'); % Put a heading on the page and display text with the result

text2html(['For a settling time of 100ns to 10bit, with a capacitance of 10pF, we need a resistance of: ', ...

num2str(double(Rnum)), '$\\Omega$']);

% Write the complete script to an html file

% This writes the script to an html page and places a link on the circuit's index page.

script2html('myFirstRCnetwork');

stophtml(); % This closes the active html page

Go to main index, Forum

SLiCAP: Symbolic Linear Circuit Analysis Program, Version 0.5 © 2009-2018 Anton Montagne

For documentation, examples, support, updates and courses please visit: analog-electronics.eu