Code covered by the BSD License

# Analytic Circuit Solver

### Raj Sodhi (view profile)

Reads in text netlist and uses symbolic toolbox to provide transfer function in analytic form.

transfer_func_calc(file1)
```function xsoln = transfer_func_calc(file1)
% function xsoln = transfer_func_calc(file1)
%
% A sample file might be:
% % % %    +   -  out
% % opamp1 vi e1 vo
% % r1 e1 0
% % r2 e1 vo

allnodes = [n1 n2 n3] ;

% separate opamp components from passives
i = find(~strncmp('opamp',names,5)) ; % finds all the components that are not opamps
opi = find(strncmp('opamp',names,5)) ; % finds all the components that are opamps

% find current through each passive element
nodes = allnodes(i,:) ;
nodes(:,3) = [] ;
passives = names(i) ;
for i = 1:length(passives)
switch passives{i}(1)
case {'r','R','z','Z'}
cond{i} = sprintf('1/%s',passives{i}) ;
case {'g','G','y','Y'}
cond{i} = sprintf('%s',passives{i}) ;
case {'c','C'}
cond{i} = sprintf('s*%s',passives{i}) ;
case {'l','L'}
cond{i} = sprintf('1/s/%s',passives{i}) ;
end
end

avoid_kcl_nodes = allnodes(opi,3)' ; % don't do KCL at an output node of an oamp.
kcl_nodes = setdiff(unique(nodes),{'' 'vi' '0' avoid_kcl_nodes{:}}) ;

for kcl_i = 1:length(kcl_nodes)
passive_Is = find(sum(strcmp(kcl_nodes(kcl_i),nodes),2)) ;
% This has index of passive attached to kcl node
clear term1 ;
for pi = 1:length(passive_Is)
other_node = setdiff(nodes(passive_Is(pi),:),kcl_nodes(kcl_i)) ;
other_node(find(strcmp(other_node,''))) = [] ;
term1{pi} = sprintf('(%s - %s) * ',other_node{:},kcl_nodes{kcl_i}) ;
term1{pi} = sprintf('%s%s',term1{pi},cond{passive_Is(pi)}) ;
end
txt0 = sprintf('%s + ',term1{:}) ;
txt0 = txt0(1:end-3) ;
eqn(kcl_i,1) = sym(txt0) ;
end

% for all elements == to opamp
for i = 1:length(opi)
Avar{i} = sprintf('A%s',setdiff(names{opi(i)},'opamp')) ;
txt0 = sprintf('%s - %s*(%s - %s)',...
allnodes{opi(i),3},Avar{i},allnodes{opi(i),1},allnodes{opi(i),2}) ;
eqn(kcl_i+i,1) = sym(txt0) ;
end
eqn(end+1,1) = 'vi' ;

x = setdiff(unique(allnodes),{'' '0'}) ;
for i = 1:length(eqn)
for i2 = 1:length(x)
z = zeros(1,length(x)) ;
z(i2) = 1 ;
zcell = num2cell(z) ;
A(i,i2) = diff(eqn(i),x{i2}) ;
end
end
b = zeros(length(x),1) ; b(end) = 1 ;
xsoln = A^(-1)*b ;
if exist('Avar')
for i = 1:length(Avar)
xsoln = limit(xsoln,sym(Avar{i}),inf) ;
end
end
xsoln = simple(xsoln) ;

fprintf(1,'For input file:\n') ;
type(file1)
fprintf(1,'The solutions are:\n')
for i = 1:length(x)
fprintf(1,'%s = %s\n',x{i},char(xsoln(i)))
end
```