function draw_resistor(t,a,A,B) 
% To plot a resistor, t= turns in the resistor
% a= wideness of the turns 
% A and B denotes start and stop (x,y) coordinates 

% Shape of the resistor:
Dx = 1/(t*2);
x = [Dx - 1:3*Dx:1]; 
[m1, n1] = size(1:length(x)); 
y = a*[0, ( -ones(1,n1-2)).^(1:n1-2), 0];
x = [-1,x,1];
y = [0,y,0];
% For plotting:
[xx,yy] = transform_coordinates(x,y,A,B) ; 
plot (xx,yy) ;


			function draw_line(A,B) 
% to plot connecting lines for the circuit components
% A and B are the x and y start and stop positions, respectively 

plot([A(1),B(1)],[A(2),B(2)]);


			function draw_inductor(n,q,A,B) 
% To plot an inductor, n=number of loops q=width of loops 
% A and B are starting and ending (x,y) coordinates 
% For the inductor, A is defined for [-1,0], B for [1,0] 
%'transform_coordinates' is used to transform coordinates to correspond 
% to the the starting and ending points of the inductor 

x = -1 :0.01:1; 
t = (x+1)*pi*(n+0.5);   % to plot the form of the inductor...

y = -q*cos(t); x = x +0.4*(-sin(t)+1); 
a= x(1); b=x(length(x));
x= 2*(x-a)/(b-a) -1 ; 

[xx,yy] = transform_coordinates(x,y,A,B); 
plot(xx,yy); 


			function draw_capacitor(t, g, A, B)
% To draw a capacitor, its basically 4 line segments 
% connected to form the shape of a capacitor t=width, g= height
% The form of the capacitor. Each x,y pair draws a segment
x1=[-1,-t]; x2=[-t,-t] ; x3=[t,t] ; x4=[t,1];
y1=[0,0];y2=[-g*0.7,g*0.7]; y3=[-g*0.7,g*0.7]; y4=[0,0];

[xx1,yy1]= transform_coordinates(x1,y1,A,B) ;  % to define the spatial basis for transformation
[xx2,yy2] = transform_coordinates(x2,y2,A,B); 
[xx3,yy3]= transform_coordinates(x3,y3,A,B);
[xx4,yy4]= transform_coordinates(x4,y4,A,B);
% So the entire inductor is plotted:  
if (ishold == 0) 
    hold on; 
end 
           
% to plot: 
 plot(xx1,yy1); plot(xx2,yy2); 
 plot(xx3,yy3); plot(xx4,yy4);           
           


			Complete

function circuit
rv = [3,4]; % the ordinal number(s) of vert edge(s) (rezistor)
rh = [2,1,1,2]; % index(es) of horz edge(s): row, col
cv = [1,3]; % the ordinal number(s) of vert edge(s) (capacitor)
ch = [2,3,2,1]; % index(es) of horz edge(s): row, col
sz = 1.8; % 1.8 recommended; sz of [1.4, 1.9]
nc = 3 ; % number of contours: 1, 2, 3, 4.
x = (0:7)';
y = [0,1.1, - 1.1,1.1, - 1.1,1.1, - 1.1,0]'*sz; % resistor

contr(nc,sz) % drawing contours
cm = sz/20;
cm1 = sz/100;
ss2 = sz/10;
sh = cm1*nc;
b = [0 1;1 0]; % rotation

if all(cv)
for j = cv
aa = sz /nc*(j - 1);
ss = [4, - .5 + aa/cm]; % vertical & horizontal
ss1 = ss(1)*cm;
c = ([.5,0; .5,1; 1,1; 1,0] + ss)*cm; % capacitor
mm = max(c(3:4,:) - c(1:2,:));
ww = [aa,mm(1); aa,mm(2)] + [ss1,0;ss1,0]*b;
cr(1,ww,c*b,0,0)
end
end

if all(ch)
for j = 1:2:numel(ch)
aa = sz /nc*(ch(j + 1) - 1);
aj = cm/nc*(j - 1);
ss = [4 + aa/cm, - .5]; % vertical & horizontal
ss1 = ss(1)*cm - aj;
c = ([.5,0; .5,1; 1,1; 1,0] + ss)*cm; % capacitor
mm = max(c(3:4,:) - c(1:2,:)) + aj;
ww = [0,mm(1); 0,mm(2)]*b + [ss1,0; ss1,0];
cr(1,ww,c,2 - ch(j),sh)
end
end

if all(rv)
for j = rv
aa = sz/nc*(j - 1);
wi = [ss2,aa];
ww = [wi; ss2 + max(x)*cm1,aa]*b;
c = [x,y]*b*cm1 + [aa,ss2];
cr(0,ww,c,0,0)
end
end
if all(rh)
for j = 1:2:numel(rh)
aa = sz/nc*(rh(j + 1) - 1);
wi = [ss2 + aa,0];
ww = [wi; ss2 + max(x)*cm1 + aa,0];
c = [x,y]*cm1 + [0,ss2 + aa]*b;
cr(0,ww,c,2 - rh(j),sh)
end
end
set(gcf,'color','w')
axis off
hold off
end

function cr(fl,ww,c,vh,sh)
plot(ww(:,1) - sh,ww(:,2) + vh,'w')
if fl
plot(c(1:2,1) - sh,c(1:2,2) + vh,'b',...
c(3:4,1) - sh,c(3:4,2) + vh,'b')
else
plot(c(:,1) - sh,c(:,2) + vh,'b')
end
end

function contr(nc,sz)
yr = [0,0,1,1,0]; xr = [0,sz ,sz ,0,0]; % contour
plot(xr,yr,'b'); axis([ -.1,1.95, -.2,1.2]); axis equal
hold on

for i = 2:nc
t = plot([sz,sz]/nc*(i - 1),[0,1],'b.-'); % internal vertical edges
set(t,'markersize',12)
end
end

Two n-by-n matrices A and B are called similar if there exists an invertible n-by-n matrix S such that