Engineering 77 Lab 2: Spicey
Aron Dobos, Tyler Strombom
19 September 2005
Abstract
In this lab SPICE netlists and MATLAB programs were written to simulate a variety of non-linear diode circuits. The Matlab approach involved performing a modified nodal analysis and using the Newton-Raphsen method for iteratively solving the non-linear equations. The netlists, code, and results for the various tasks are documented below.
Task 1
The goal of task 1 was to plot the voltages at nodes 1, 2, and 3 for the circuit below using SPICE and a Matlab program using the modified nodal analysis technique that we learned in class.
Figure 1. Circuit for Task 1
The following is the SPICE deck and Matlab program followed by the graphs of the voltages at nodes 1, 2, and 3.
SPICE Netlist for Task 1
*Control section determines type of analysis
.control
destroy all
echo
TRAN 0.01 2
plot V(1) V(2) V(3)
.endc
*These next lines give circuit layout (netlist)
R1 1 2 1k
R2 2 3 4k
R3 3 0 1k
*form of sinusoid -- SIN(VO VA FREQ TD THETA)
VVin 1 0 SIN(0 4 1 0 0)
.end
Matlab Code for Task 1
% Set Circuit Values
R1=1000;
R2=4000;
R3=1000;
DT = 1E-6; %Time Increment
t=0:DT:0.002; %Time Vector
Vin=4*sin(2*pi*1000*t);
% Allocate memory for arrays
V1=zeros(size(t)); V2=zeros(size(t)); V3=zeros(size(t));
% Initial Conditions
V1(1)=0; V2(1)=0; V3(1)=0;
A=[1/R1 -1/R2 0 1;
-1/R1 1/R1+1/R2 -1/R2 0;
0 -1/R2 1/R2+1/R3 0;
1 0 0 0];
invA=inv(A);
for i=2:length(t),
b=[0 0 0 Vin(i)]';
x=invA*b;
V1(i)=x(1);
V2(i)=x(2);
V3(i)=x(3);
end
plot(t*1000, V1, 'r',t*1000,V2,'g',t*1000,V3,'b');
axis([0 2 -4 4]);
grid on
title('Task 1');
xlabel('Time (mS)');
Figure 2. Plot of Voltages at Nodes 1, 2, and 3 using SPICE
Figure 3. Plot of Voltages at nodes 1, 2, and 3 using MATLAB
Task 2
The goal of task 2 was to plot the voltages at nodes 1, 2, and 3 for the circuit below using SPICE and a Matlab program using the modified nodal analysis technique that we learned in class.
Figure 4. Circuit for Task 2
The following is the SPICE deck and Matlab program followed by the graphs of the voltages at nodes 1, 2, and 3.
SPICE NetList for Task 2
*Control section determines type of analysis
.control
destroy all
echo
TRAN 0.01MS 2MS
plot V(1) V(2) V(3)
.endc
*These next lines give circuit layout (netlist)
R1 1 2 1k
R2 2 3 1k
R3 3 0 1k
C1 2 3 1u
*form of pulse -- PULSE(V1 V2 TD TR TF PW PER)
VVin 1 0 PULSE(0 5 0 0 0 .5m 1m)
.end
Matlab Code for Part 2
% Task 2
% Set Circuit Values
R1=1000;
R2=1000;
R3=1000;
C1=1E-6;
DT=1E-6; %Time increment
t=0:DT:0.002; %Time vector
Vin=5*(1+square(t*1000*2*pi))/2; %5V, 1kHz square wave for Vin.
% Allocate memory for arrays.
V1=zeros(size(t)); V2=zeros(size(t)); V3=zeros(size(t));
%Initial conditions
V1(1)=0; V2(1)=0; V3(1)=0;
Rc = DT/C1;
A = [-1/R1 1/R1 0 1;
1/R1 (-1/R1-1/R2-1/Rc) (1/R2+1/Rc) 0;
0 (1/Rc+1/R2) (-1/Rc-1/R2-1/R3) 0;
1 0 0 0 ]
invA=inv(A);
for i=2:length(t),
Vc_t0=V2(i-1)-V3(i-1);
IC = Vc_t0*C1/DT;
b = [0 -IC IC Vin(i)]';
x=invA*b;
V1(i) = x(1);
V2(i) = x(2);
V3(i) = x(3);
end
plot(t*1000,V1,'r',t*1000,V2,'g',t*1000,V3,'b');
axis([0 2 -5 5]);
grid on
title('E77 Lab 2 Part 2');
xlabel('Time (mS)');
ylabel('Voltage (V)');
legend('V1', 'V2', 'V3');
Figure 5. Plot of Voltages at Nodes 1, 2, and 3 using SPICE
Figure 6. Plot of Voltages at Nodes 1, 2, and 3 using MATLAB
Task 3
The goal of task 3 was to plot the voltages at nodes 1, 2, and 3 for the circuit below using SPICE and a Matlab program using the modified nodal analysis technique that we learned in class.
Figure 7. Circuit for Task 3
The following is the SPICE deck and Matlab program followed by the graphs of the voltages at nodes 1, 2, and 3.
SPICE NetList for Task 3
*Control section determines type of analysis
.control
destroy all
echo
TRAN 0.01MS 2MS
plot V(1) V(2) V(3)
.endc
*These next lines give circuit layout (netlist)
R1 1 2 1k
R2 2 3 4k
R3 3 0 1k
D1 2 1 DMOD
D2 2 3 DMOD
*form of sinusoid -- SIN(VO VA FREQ TD THETA)
VVin 1 0 SIN(0 4 1000 0 0)
.MODEL DMOD D Is=1.0E-15 n=1
.end
Matlab Code for Part 3
% Task 3
R1=1000;
R2=4000;
R3=1000;
Is=1E-15;
VT=25e-3;
DT=1E-6;
t=0:DT:0.002;
V3=4*sin(2*pi*1000*t);
%Allocate Memory for Arrays
V2=zeros(size(t));
V1=zeros(size(t));
iter=0;
err=1; %Initialize error to something large to start loop
V0=[0.5 0.5]'; %Initial guess for node voltage
for i=1:length(V2),
while err>0.0001,
iter=iter+1; %Count iterations
e1=exp((V0(1)-V3(i))/VT);
e2=exp((V0(1)-V0(2))/VT);
fv1=(V0(1)-V3(i))/R1+(V0(1)-V0(2))/R2+Is*(e1-1)+Is*(e2-1);
fv2=(V0(2)-V0(1))/R2+V0(2)/R3-Is*(e2-1);
dfv1dv1=1/R1+1/R2+Is*e1/VT+Is*e2/VT;
dfv1dv2=-1/R1-Is*e2/VT;
dfv2dv1=-1/R2-Is*e2/VT;
dfv2dv2=1/R2+1/R3+Is*e2/VT;
F=[fv1 fv2]'; %Function we wish to find zero of
J=[dfv1dv1 dfv1dv2; dfv2dv1 dfv2dv2]; %Jacobian
Vnew=V0-inv(J)*F;
err=sum(abs(Vnew-V0)./abs(Vnew+V0)); %error
V0=Vnew; %New guess
end
V2(i)=V0(2);
V1(i)=V0(1);
err=1;
end
iter
plot(t,V1,'g',t,V2,'b',t,V3,'r')
xlabel('Time(ms)');
ylabel('Voltage');
title('Task 3');
grid;
Figure 8. Plot of Voltages at Nodes 1, 2, and 3 using SPICE
Figure 9. Plot of Voltages at Nodes 1, 2, and 3 using MATLAB
Task 4
The goal of task 4 was to plot the voltages at nodes 1, 2, and 3 for the circuit below using SPICE and a matlab program using the modified nodal analysis technique that we learned in class.
Figure 10. Circuit for Task 4
The following is the SPICE deck and Matlab program followed by the graphs of the voltages at nodes 1, 2, and 3.
SPICE NetList for Task 4
*Control section determines type of analysis
.control
destroy all
echo
TRAN 0.01MS 2MS
plot V(1) V(2)
.endc
*These next lines give circuit layout (netlist)
R1 2 0 1k
C1 2 0 1u
D1 1 2 DMOD
*form of sinusoid -- SIN(VO VA FREQ TD THETA)
VVin 1 0 SIN(0 4 1k 0 0)
.MODEL DMOD D Is=1.0E-15 n=1
.end
Matlab Code for Task 4
%% E77 Lab 2 part 4
%% half-wave rectifier circuit, 1uF reservoir, 1k load
clc
DT=1e-6;
t=0:DT:.002;
Is=1e-15;
n=1;
C = 1e-6;
R = 1000;
VT = 0.025;
Vin = 4*sin(2*pi*1000*t);
V2 = zeros(size(t));
Vd = zeros(size(t));
Id = zeros(size(t));
V2(1) = 0; % initial condition on the capacitor
for i=2:length(t),
Ic = C/DT*V2(i-1);
Rc = DT/C;
Vd(i) = part4_newton(Vin(i), R, Ic, Rc, Is, n, -10, 10, Vin(i)-V2(i-1));
Id(i) = Is*(exp(Vd(i)/(n*VT))-1);
Gdiode = Is/(n*VT)*exp(Vd(i)/(n*VT));
V2(i) = (Vin(i)*Gdiode + Id(i) + Ic)/(Gdiode + 1/Rc + 1/R);
end
plot(t*1000,Vin, 'r', t*1000, V2, 'g')
grid
title('E77 Lab 2 Part 4: Half-wave rectifier, 4Vp-p, 1uF reservoir, 1k load');
xlabel('Time (ms)');
ylabel('Voltage (V)');
legend('Vin', 'V2');
function [Vd] = part4_newton(VIN, R, IC, RC, IS, N, vmin, vmax, guess)
% newton's method for diode dc op
% example: Vdiode = part4_newton(vin, R, Ic, Rc, Is, n, -2, 2);
threshold = 0.00001; % accuracy of Vd result
max_iter = 50; % maximum number of iterations
Vd = guess; % start with a reasonable guess
VT = 0.025; % thermal voltage
for i=1:max_iter,
f = IS*(exp(Vd/(N*VT))-1) + IC - (VIN-Vd)/RC - (VIN-Vd)/R;
df = IS/(N*VT)*exp(Vd/(N*VT)) +1/RC + 1/R;
dx = f/df;
Vd = Vd - dx; % x1 = x - f(x)/df(x)
if (Vd < vmin || Vd > vmax)
disp('newton: exceeded bounds');
return;
end
% if change from previous Vd value was smaller than
% the accuracy threshold, we're done, so return.
if (abs(dx) < threshold)
return;
end
end
disp('newton: failed to converge in 50 iterations');
return;
Figure 11. Plot of Voltages at Node 2 using SPICE
Figure 12. Plot of Voltages at Node 2 using MATLAB