-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asv
231 lines (168 loc) · 5.16 KB
/
main.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
% Script that merges everything we did for the different parts
% [Part 1]
fprintf("Part 1\n")
fun = @nonlinearFunc;
n = 4;
xstart = zeros(n,1);
options = optimoptions('fsolve','SpecifyObjectiveGradient',true);
[x,fval,exitflag,output] = fsolve(fun,xstart,options);
x
% display the quality of the solution
quality = norm(fval)
% number of function evaluations taken
disp(output.funcCount)
V2 = x(2)
% [Part 2]
fprintf("\n")
fprintf("Part 2\n")
[sol,iterations] = DCsolveRectifierForVin1(10, 1e-6)
% [Part 3]
fprintf("\n")
fprintf("Part 3\n")
% Inputs
clear all;
simulation_time = 0.05; % seconds
tol = 1e-3; % also seconds
ACsolverPlotter1(@inputVoltage, simulation_time, tol);
function [result] = inputVoltage(x)
% 60 Hz sine wave
result = 5 * sin(376.991118 * x);
end
% Plot results
function [none] = ACsolverPlotter1(inputVoltage, simulation_time, tol, dt)
none = 0;
if ~exist('tol','var')
tol = 1e-3;
end
if ~exist('dt','var')
dt = 1e-4;
end
output = ACsolveRectifierForVin1(@inputVoltage, simulation_time, tol);
x = linspace(0,simulation_time,simulation_time/dt);
input = inputVoltage(x);
plot(x, input);
hold on
plot(x,output);
hold off
% Convert x ticks to miliseconds. Found here:
% https://www.mathworks.com/matlabcentral/answers/379882-how-to-multiply-displayed-xvalues-on-plot-by-constant
xticks = get(gca,'xtick');
scaling = 1000;
newlabels = arrayfun(@(x) sprintf('%.1f', scaling * x), xticks, 'un', 0);
set(gca,'xticklabel',newlabels);
legend("Input Voltage","Output Voltage");
xlabel("Time (ms)");
ylabel("Voltage (V)");
end
% Run DC solver many times in quick succession
function [OutputWaveform] = ACsolveRectifierForVin1(Vin,simulation_time,tol)
if ~exist('tol','var')
tol = 1e-3;
end
dt = 1e-4;
output_wave = zeros(1,simulation_time/dt);
sim_length = size(output_wave);
t = 0;
for idx = 1:sim_length(2)
x = Vin(t);
result_vector = DCsolveRectifierForVin1(x,tol);
output_wave(idx) = result_vector(2);
t = t + dt;
end
OutputWaveform = output_wave;
end
% DC solver / AC solver for a single point in time
function [Vout,iterations] = DCsolveRectifierForVin1(Vin,tol)
if ~exist('tol','var')
% third parameter does not exist, so default it to 1e-6
tol = 1e-6;
end
[Xout,iterations] = NewtonRaphson(@nonlinearFunc1, tol, Vin);
Vout = Xout;
end
% We used Newton-Raphson because Broyden Update methods often created
% conditioning problems, even more so than Newton-Raphson. Since the
% saturation current is such a small number, estimating a Jacobian with it
% came with too much unstability to be acceptable. Even when pseudo-Newton
% methods converge, they do so more slowly than Newton-Raphson, which
% will lead to even more problems down the line when we need to simulate a
% sine wave input.
function [Xout, iterations] = NewtonRaphson(func,tol,Vin)
% Initial guess. This was hard to pick because bridge rectifiers can be
% used in any voltage application. We chose initial guess of 0 to
% confrom to the project specifications.
Xguess = [0;0;0;0];
J = eye(size(Xguess,1));
current_guess = Xguess;
iterations = 0;
error_encountered = false;
% To keep the console from being flooded
warning('off');
while true
% Changed the way variables hold things a little bit, for some reason
% improves accuracy
old_guess = current_guess;
[F,J] = func(old_guess, Vin);
current_guess = old_guess - J\F;
% Error measurement
deltaX = current_guess - old_guess;
normDeltaX(iterations + 1) = norm(deltaX);
normresult = norm(func(current_guess, Vin));
% Break from loop upon success
if normresult < tol && normDeltaX(iterations + 1) < tol
break
end
% Also break from loop after a certain threshold of attempts,
% and flag a non-convergence error
iterations = iterations + 1;
if iterations >= 200
error_encountered = true;
break
end
end
% Deal with error flag
if ~(error_encountered)
Xout = current_guess;
else
error("Newton-Raphson method failed to converge in 200 iterations.");
end
end
%% Description of the system
function [F,J] = nonlinearFunc1(X, Vin)
%outputs :
% F is the nonlinear function,
% J is the Jacobian of the F.
%Input
% X is the vector of nodal voltages.
% input source
U = zeros(4,1);
U(4,1) = Vin;
U = U;
% G matrix
G = zeros(4,4);
G(2,2) = 0.02;
G(4,1) = 1;
G(1,4) = 1;
G(4,3) = -1;
G(3,4) = -1;
% g vector
g= zeros(4,1);
Is = 1e-13;
% 300K
Vt = 0.025851997074205;
g(1,1) = Is*( exp( (X(1) - X(2) )/Vt) -1) - Is*( exp(- X(1)/Vt) -1) ;
g(2,1) = -Is*( exp( (X(1) - X(2) )/Vt) -1) - Is*( exp( (X(3)- X(2) )/Vt) -1) ;
g(3,1) = Is*( exp( (X(3) - X(2) )/Vt) -1) - Is*( exp( -X(3)/Vt) -1) ;
%% Set of nonlinear equations
F = G*X+g-U;
%% compute the Jacobian
gdX = zeros(4,4);
gdX(1,1) = (Is/Vt)*( exp( (X(1) - X(2) )/Vt) ) + (Is/Vt)*( exp(- X(1)/Vt) );
gdX(1,2) = -(Is/Vt)*( exp( (X(1) - X(2) )/Vt) ) ;
gdX(2,1) = -(Is/Vt)*( exp( (X(1) - X(2) )/Vt) ) ;
gdX(2,2) = (Is/Vt)*( exp( (X(1)- X(2) )/Vt) ) + (Is/Vt)*( exp( (X(3)- X(2) )/Vt) ) ;
gdX(2,3) = -(Is/Vt)*( exp( (X(3)- X(2) )/Vt)) ;
gdX(3,2) = -(Is/Vt)*( exp( (X(3)- X(2) )/Vt)) ;
gdX(3,3) = (Is/Vt)*( exp( (X(3)- X(2) )/Vt)) + (Is/Vt)*( exp( -X(3)/Vt) );
J = G+gdX;
end