-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams_C.m
More file actions
259 lines (226 loc) · 7.61 KB
/
params_C.m
File metadata and controls
259 lines (226 loc) · 7.61 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
%------------------------------------------------------------------------%
% [K_1, Lc_1, Rc_1] = params_C(J_sol, L_sol, R1_sol, R2_sol)
% solves for the cylinder parameters for 1 cylinder using the equations
% defined by case C
% case C equations [b1; b3; Bz];
%
% INPUTS
% J_sol: J value for solenoid
% L_sol: length of solenoid
% R_1: inner radius of solenoid
% R_2: outer radius of solenoid
%
% OUTPUTS
% K_1: current for cylinder 1
% Lc_1: length of cylinder 1
% Rc_1: radius of cylinder 1
%
% REMARKS
% case C = [b1; b3; Bz];
% The optimal paramters of solved by using an LM algorithum
%
% AUTHOR(S): Paige Husa
%
% v1.0 4/29/2019
% ----------------------------------------------------------------------- %
function [K_1, Lc_1, Rc_1, soln_found] = params_C(J_sol, L_sol, R1_sol, R2_sol)
format long;
%% solenoid parameters
realS_J = J_sol;
realS_L = L_sol;
realS_R1 = R1_sol;
realS_R2 = R2_sol;
delta_R = realS_R2 - realS_R1;
real_z = 0;
real_mu = 1;%(4*pi)*1e-7;
%% solenoid side of equation
c = c_vector_C(realS_J, realS_L, realS_R1, realS_R2, real_mu, real_z);
%% inputs to LM function
ravg = (realS_R2+realS_R1)/2;
rdiff = (realS_R2-realS_R1)/2;
%initial guess the optimal paramters
cur_scale = c(1)/(pi*realS_L*(ravg)^2) ;
gammaGuess = [1; realS_L; ravg];
% parameters K_1,Lc_1,Rc_1
tol = 1e-5;
%is basically going to run forever unless you stop the algorithum or
%solution is found
maxIterations = 500;
maxTime = 60*5;
dispVals = 0;
maxChange = [];
%% LM method
[ currentEstimate, cost, soln_found ] = LM_LeastSquares( gammaGuess, @(x)costF(x,c,cur_scale), tol, maxIterations, maxTime, dispVals, maxChange );
if( any(currentEstimate <0 ))
warning('Solution Found is non-physical. Resolving...');
[ currentEstimate, cost, soln_found ] = LM_LeastSquares( abs(currentEstimate), @(x)costF(x,c,cur_scale), tol, maxIterations, maxTime, dispVals, maxChange );
currentEstimate = abs(currentEstimate);
end
% currentEstimate
% cost
soln_found = cost < 1e-3;
%% final optimal values
K_1 = currentEstimate(1)*cur_scale;
Lc_1 = currentEstimate(2);
Rc_1 = currentEstimate(3);
end
%% cost function
function [error, Jacobian] = costF(gammaGuess,c,cur_scale)
real_z = 0;
real_mu = 1;%(4*pi)*1e-7;
f = f_vector_C(gammaGuess(1)*cur_scale, gammaGuess(2), gammaGuess(3), real_mu, real_z);
J_gamma = jacobian_matrix_C(gammaGuess(1)*cur_scale, gammaGuess(2), gammaGuess(3), real_mu, real_z);
J_gamma(:,1) = J_gamma(:,1)*cur_scale;
Jacobian = J_gamma;
% error = Jacobian*gammaGuess;
error = f -c;
error = error/norm(c);
Jacobian = Jacobian/norm(c);
%scale the jabocian appropriately so don't get a singularity
for i = 1:length(error)
sc = norm(Jacobian(i,:));
error(i) = error(i)/sc;
Jacobian(i,:) = Jacobian(i,:)/sc;
end
end
%------------------------------------------------------------------------%
% c_1 = c_vector_C(J,Lsol,R1,R2,mu,z)
% constants in equation (solenoid side of system of equations) for case (C) where the
% optimal solution is 1 cylindrical shell
%
% INPUTS
% J: J value for solenoid
% Lsol: length of solenoid
% R1: inner radius of solenoid
% R2: outer radius of solenoid
% mu: magnetic permeability
% z: z location in solenoid (0 if at center - origin is at center)
%
% OUTPUTS
% c_1 = 3x1 vector of constants based on solenoid paramters for the 3
% equations used in case C
%
% REMARKS
% equation solving for C case [b1; b3; Bz;];
%
% AUTHOR(S): Paige Husa
%
% MODIFICATIONS:
% v1.0 4/29/2019
% ----------------------------------------------------------------------- %
function c_1 = c_vector_C(J,Lsol,R1,R2,mu,z)
%C_VECTOR_C
% C_1 = C_VECTOR_C(J,LSOL,R1,R2,MU,Z)
% This function was generated by the Symbolic Math Toolbox version 8.2.
% 29-Apr-2019 19:53:38
t2 = R1.^2;
t3 = R2.^2;
t4 = Lsol.^2;
t5 = t2.^2;
t6 = t3.^2;
t7 = Lsol.*z;
t8 = t4./4.0;
t9 = z.^2;
t10 = t2+t7+t8+t9;
t11 = sqrt(t10);
t12 = R1+t11;
t13 = log(t12);
t14 = t3+t7+t8+t9;
t15 = sqrt(t14);
t16 = R2+t15;
t17 = log(t16);
t18 = t13-t17;
t19 = t2-t7+t8+t9;
t20 = sqrt(t19);
t21 = R1+t20;
t22 = log(t21);
t23 = t3-t7+t8+t9;
t24 = sqrt(t23);
t25 = R2+t24;
t26 = log(t25);
t27 = t22-t26;
c_1 = [J.*Lsol.*pi.*(R1.*t2-R2.*t3).*(-1.0./3.0);J.*Lsol.*R1.*t5.*pi.*(-3.0./4.0e1)+J.*Lsol.*R2.*t6.*pi.*(3.0./4.0e1)+(J.*Lsol.*R1.*t2.*t4.*pi)./2.4e1-(J.*Lsol.*R2.*t3.*t4.*pi)./2.4e1;(mu.*(J.*Lsol.*t18.*pi+J.*Lsol.*t27.*pi+J.*t18.*z.*pi.*2.0-J.*t27.*z.*pi.*2.0).*(-1.0./4.0))./pi];
end
%------------------------------------------------------------------------%
% f_1 = f_vector_C(K_1,Lc_1,Rc_1,mu,z)
% approx geometry side of system of equations with paramters for 1 cyclinder (case C)
%
% INPUTS
% K_1: K value for cylinder
% Lc_1: length of cylinder
% Rc_1: radius of cylinder
% mu: magnetic permeability
% z: z location in cylinders (0 if at center - origin is at center)
%
% OUTPUTS
% f1 = 3x1 vector of equations based on cylinder paramters for the 3
% equations used in case C
%
% REMARKS
% equations solving for C case [b1; b3; Bz];
%
% AUTHOR(S): Paige Husa
%
% MODIFICATIONS:
% v1.0 4/29/2019
% ----------------------------------------------------------------------- %
function f_1 = f_vector_C(K_1,Lc_1,Rc_1,mu,z)
%F_VECTOR_C
% F_1 = F_VECTOR_C(K_1,LC_1,RC_1,MU,Z)
% This function was generated by the Symbolic Math Toolbox version 8.2.
% 29-Apr-2019 19:53:38
t2 = Rc_1.^2;
t3 = Lc_1.^2;
t4 = Lc_1./2.0;
t5 = t3./4.0;
t6 = z.^2;
f_1 = [K_1.*Lc_1.*t2.*pi;(K_1.*Lc_1.*t2.*pi.*(t2.*3.0-t3))./8.0;(mu.*(K_1.*pi.*(t4+z).*1.0./sqrt(t2+t5+t6+Lc_1.*z).*2.0+K_1.*pi.*(t4-z).*1.0./sqrt(t2+t5+t6-Lc_1.*z).*2.0))./(pi.*4.0)];
end
%------------------------------------------------------------------------%
% Jacob1 = jacobian_matrix_C(K_1,Lc_1,Rc_1,mu,z)
% equations for jacobian martix for case C
%
% INPUTS
% K_1: K value for cylinder
% Lc_1: length of cylinder
% Rc_1: radius of cylinder
% mu: magnetic permeability
% z: z location in cylinders (0 if at center - origin is at center)
%
% OUTPUTS
% Jacob1 = 3x3 matrix of equations based on cylinder paramters for the 3
% equations used in case C in f_vector and derivatives with respect to the
% 3 parameters
%
% REMARKS
% case C equations [b1; b3; Bz];
% parameter order [K_1, Lc_1, Rc_1];
%
% AUTHOR(S): Paige Husa
%
% MODIFICATIONS:
% v1.0 4/29/2019
% ----------------------------------------------------------------------- %
function Jacob1 = jacobian_matrix_C(K_1,Lc_1,Rc_1,mu,z)
%JACOBIAN_MATRIX_C
% JACOB1 = JACOBIAN_MATRIX_C(K_1,LC_1,RC_1,MU,Z)
% This function was generated by the Symbolic Math Toolbox version 8.2.
% 29-Apr-2019 19:53:38
t2 = Rc_1.^2;
t3 = Lc_1.^2;
t4 = t2.*3.0;
t5 = t3-t4;
t6 = Lc_1./2.0;
t7 = t3./4.0;
t8 = z.^2;
t9 = 1.0./pi;
t10 = Lc_1.*z;
t11 = t2+t7+t8+t10;
t12 = 1.0./sqrt(t11);
t13 = t6+z;
t14 = t6-z;
t15 = t2+t7+t8-t10;
t16 = 1.0./t11.^(3.0./2.0);
t17 = 1.0./t15.^(3.0./2.0);
Jacob1 = reshape([Lc_1.*t2.*pi,Lc_1.*t2.*t5.*pi.*(-1.0./8.0),(mu.*t9.*(t14.*pi.*1.0./sqrt(t2+t7+t8-Lc_1.*z).*2.0+t12.*t13.*pi.*2.0))./4.0,K_1.*t2.*pi,K_1.*t2.*t3.*pi.*(-1.0./4.0)-(K_1.*t2.*t5.*pi)./8.0,(mu.*t9.*(K_1.*t12.*pi+K_1.*1.0./sqrt(t15).*pi-K_1.*t13.^2.*t16.*pi-K_1.*t14.^2.*t17.*pi))./4.0,K_1.*Lc_1.*Rc_1.*pi.*2.0,K_1.*Lc_1.*Rc_1.*t2.*pi.*(3.0./4.0)-(K_1.*Lc_1.*Rc_1.*t5.*pi)./4.0,mu.*t9.*(K_1.*Rc_1.*t13.*t16.*pi.*2.0+K_1.*Rc_1.*t14.*t17.*pi.*2.0).*(-1.0./4.0)],[3,3]);
end