Mercurial > octave-nkf
comparison scripts/ode/odeset.m @ 20581:e368ce72a844
maint: Use Octave coding conventions for ode* functions.
Use '!' instead of '~' for logical not.
Use '##' only for comments which standalone on a line.
Use double quotes instead of single quotes where possible.
Wrap long lines to < 80 characters.
* ode45.m, odeget.m, odeset.m: Use Octave coding conventions.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 03 Oct 2015 22:10:45 -0700 |
parents | 25623ef2ff4f |
children | 45151de7423f |
comparison
equal
deleted
inserted
replaced
20580:25623ef2ff4f | 20581:e368ce72a844 |
---|---|
14 ## General Public License for more details. | 14 ## General Public License for more details. |
15 ## | 15 ## |
16 ## You should have received a copy of the GNU General Public License | 16 ## You should have received a copy of the GNU General Public License |
17 ## along with Octave; see the file COPYING. If not, see | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | 18 ## <http://www.gnu.org/licenses/>. |
19 | |
20 | 19 |
21 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
22 ## @deftypefn {Function File} {} odeset () | 21 ## @deftypefn {Function File} {} odeset () |
23 ## @deftypefnx {Function File} {@var{odestruct} =} odeset (@var{"field1"}, @var{value1}, @var{"field2"}, @var{value2}, @dots{}) | 22 ## @deftypefnx {Function File} {@var{odestruct} =} odeset (@var{"field1"}, @var{value1}, @var{"field2"}, @var{value2}, @dots{}) |
24 ## @deftypefnx {Function File} {@var{odestruct} =} odeset (@var{oldstruct}, @var{"field1"}, @var{value1}, @var{"field2"}, @var{value2}, @dots{}) | 23 ## @deftypefnx {Function File} {@var{odestruct} =} odeset (@var{oldstruct}, @var{"field1"}, @var{value1}, @var{"field2"}, @var{value2}, @dots{}) |
51 ## @end deftypefn | 50 ## @end deftypefn |
52 | 51 |
53 function opt = odeset (varargin) | 52 function opt = odeset (varargin) |
54 | 53 |
55 ## Check number and types of all input arguments | 54 ## Check number and types of all input arguments |
56 if ((nargin == 0) | 55 if (nargin == 0 && nargout == 0) |
57 && (nargout == 0)) | |
58 print_options; | 56 print_options; |
59 return | 57 return; |
60 endif | 58 endif |
61 | 59 |
62 ## Creating a vector of OdePkg possible fields | 60 ## Creating a vector of OdePkg possible fields |
63 fields = ["AbsTol"; "Algorithm"; "BDF"; "Choice"; "Eta"; "Events"; | 61 fields = ["AbsTol"; "Algorithm"; "BDF"; "Choice"; "Eta"; "Events"; |
64 "Explicit"; "InexactSolver"; "InitialSlope"; "InitialStep"; | 62 "Explicit"; "InexactSolver"; "InitialSlope"; "InitialStep"; |
79 endfor | 77 endfor |
80 | 78 |
81 opt.Refine = 0; | 79 opt.Refine = 0; |
82 opt.OutputSave = 1; | 80 opt.OutputSave = 1; |
83 | 81 |
84 if ((nargin == 0) | 82 if (nargin == 0 && nargout == 1) |
85 && (nargout == 1)) | 83 return; |
86 return | |
87 endif | 84 endif |
88 | 85 |
89 ode_fields = fieldnames (opt); | 86 ode_fields = fieldnames (opt); |
90 | 87 |
91 if (isstruct (varargin{1})) | 88 if (isstruct (varargin{1})) |
100 | 97 |
101 while (1) | 98 while (1) |
102 pos = fuzzy_compare (name, fields); | 99 pos = fuzzy_compare (name, fields); |
103 if (size (pos, 1) == 0) | 100 if (size (pos, 1) == 0) |
104 warning ("OdePkg:InvalidArgument", | 101 warning ("OdePkg:InvalidArgument", |
105 "no property found with name ''%s''", name); | 102 "no property found with name '%s'", name); |
106 endif | 103 endif |
107 | 104 |
108 if (size (pos, 1) == 1) | 105 if (size (pos, 1) == 1) |
109 if (! strcmp (lower (deblank (name)), | 106 if (! strcmp (lower (deblank (name)), |
110 lower (deblank (fields(pos,:))))) | 107 lower (deblank (fields(pos,:))))) |
111 warning ("OdePkg:InvalidArgument", "no exact matching for ", | 108 warning ("OdePkg:InvalidArgument", "no exact matching for ", |
112 "''%s''. Assuming you were intending ''%s''", | 109 "'%s'. Assuming you were intending '%s'", |
113 name, deblank (fields(pos,:))); | 110 name, deblank (fields(pos,:))); |
114 endif | 111 endif |
115 | 112 |
116 opt.(deblank (fields(pos,:))) = varargin{1}.(optA_fields{i}); | 113 opt.(deblank (fields(pos,:))) = varargin{1}.(optA_fields{i}); |
117 break | 114 break |
118 endif | 115 endif |
119 | 116 |
120 ## if there are more matching, ask the user to be more precise | 117 ## if there are more matching, ask the user to be more precise |
121 warning ("OdePkg:InvalidArgument", | 118 warning ("OdePkg:InvalidArgument", |
122 "no exact matching for ''%s''. %d possible fields were found", | 119 "no exact matching for '%s'. %d possible fields were found", |
123 name, size(pos, 1)); | 120 name, size(pos, 1)); |
124 for j = 1:(size (pos, 1)) | 121 for j = 1:(size (pos, 1)) |
125 fprintf ("%s\n", deblank (fields(pos(j),:))); | 122 fprintf ("%s\n", deblank (fields(pos(j),:))); |
126 endfor | 123 endfor |
127 do | 124 do |
129 name = input ("New field name: "); | 126 name = input ("New field name: "); |
130 until (ischar (name)) | 127 until (ischar (name)) |
131 endwhile | 128 endwhile |
132 endfor | 129 endfor |
133 | 130 |
134 if ((nargin == 2) | 131 if (nargin == 2 && isstruct (varargin{2})) |
135 && (isstruct (varargin{2}))) | |
136 ode_struct_value_check (varargin{2}); | 132 ode_struct_value_check (varargin{2}); |
137 | 133 |
138 optB_fields = fieldnames (varargin{2}); | 134 optB_fields = fieldnames (varargin{2}); |
139 optB_f_nb = length (optB_fields); | 135 optB_f_nb = length (optB_fields); |
140 | 136 |
144 while (1) | 140 while (1) |
145 pos = fuzzy_compare (name, fields); | 141 pos = fuzzy_compare (name, fields); |
146 | 142 |
147 if (size (pos, 1) == 0) | 143 if (size (pos, 1) == 0) |
148 warning ("OdePkg:InvalidArgument", ... | 144 warning ("OdePkg:InvalidArgument", ... |
149 "no property found with name ''%s''", name); | 145 "no property found with name '%s'", name); |
150 endif | 146 endif |
151 | 147 |
152 if (size(pos, 1) == 1) | 148 if (size(pos, 1) == 1) |
153 if (! strcmp (lower (deblank (name)), lower (deblank (fields(pos,:))))) | 149 if (! strcmp (lower (deblank (name)), ... |
150 lower (deblank (fields(pos,:))))) | |
154 warning ("OdePkg:InvalidArgument", "no exact matching for ", | 151 warning ("OdePkg:InvalidArgument", "no exact matching for ", |
155 "''%s''. Assuming you were intending ''%s''", | 152 "'%s'. Assuming you were intending '%s'", |
156 name, deblank (fields(pos,:))); | 153 name, deblank (fields(pos,:))); |
157 endif | 154 endif |
158 opt.(deblank (fields(pos,:))) = varargin{2}.(optB_fields{i}); | 155 opt.(deblank (fields(pos,:))) = varargin{2}.(optB_fields{i}); |
159 break | 156 break |
160 endif | 157 endif |
161 | 158 |
162 ## if there are more matching, ask the user to be more precise | 159 ## if there are more matching, ask the user to be more precise |
163 warning ("OdePkg:InvalidArgument", "no exact matching for ''%s''. ", | 160 warning ("OdePkg:InvalidArgument", "no exact matching for '%s'. ", |
164 "%d possible fields were found", | 161 "%d possible fields were found", |
165 name, size (pos, 1)); | 162 name, size (pos, 1)); |
166 for j = 1:(size (pos, 1)) | 163 for j = 1:(size (pos, 1)) |
167 fprintf ("%s\n", deblank (fields(pos(j),:))); | 164 fprintf ("%s\n", deblank (fields(pos(j),:))); |
168 endfor | 165 endfor |
170 fprintf ("Please insert field name again\n"); | 167 fprintf ("Please insert field name again\n"); |
171 name = input ("New field name: "); | 168 name = input ("New field name: "); |
172 until (ischar (name)) | 169 until (ischar (name)) |
173 endwhile | 170 endwhile |
174 endfor | 171 endfor |
175 return | 172 return; |
176 endif | 173 endif |
177 | 174 |
178 ## if the second argument is not a struct, | 175 ## if the second argument is not a struct, |
179 ## pass new values of the OdePkg options to the first struct | 176 ## pass new values of the OdePkg options to the first struct |
180 if (mod (nargin, 2) != 1) | 177 if (mod (nargin, 2) != 1) |
196 while (1) | 193 while (1) |
197 pos = fuzzy_compare (name, fields); | 194 pos = fuzzy_compare (name, fields); |
198 | 195 |
199 if (size (pos, 1) == 0) | 196 if (size (pos, 1) == 0) |
200 error ("OdePkg:InvalidArgument", | 197 error ("OdePkg:InvalidArgument", |
201 "no property found with name ''%s''", name); | 198 "no property found with name '%s'", name); |
202 endif | 199 endif |
203 | 200 |
204 if (size (pos, 1) == 1) | 201 if (size (pos, 1) == 1) |
205 if (! strcmp (lower (deblank (name)), lower (deblank (fields(pos,:))))) | 202 if (! strcmp (lower (deblank (name)), lower (deblank (fields(pos,:))))) |
206 warning ("OdePkg:InvalidArgument", "no exact matching for ''%s''. ", | 203 warning ("OdePkg:InvalidArgument", "no exact matching for '%s'. ", |
207 "%d possible fields were found", | 204 "%d possible fields were found", |
208 name, size (pos, 1)); | 205 name, size (pos, 1)); |
209 endif | 206 endif |
210 opt.(deblank (fields(pos,:))) = varargin{i+1}; | 207 opt.(deblank (fields(pos,:))) = varargin{i+1}; |
211 break | 208 break |
212 endif | 209 endif |
213 | 210 |
214 ## if there are more matching, ask the user to be more precise | 211 ## if there are more matching, ask the user to be more precise |
215 warning ("OdePkg:InvalidArgument", "no exact matching for ''%s''. ", | 212 warning ("OdePkg:InvalidArgument", "no exact matching for '%s'. ", |
216 "%d possible fields were found", | 213 "%d possible fields were found", |
217 name, size (pos, 1)); | 214 name, size (pos, 1)); |
218 for j = 1:(size (pos, 1)) | 215 for j = 1:(size (pos, 1)) |
219 fprintf ("%s\n", deblank (fields(pos(j),:))); | 216 fprintf ("%s\n", deblank (fields(pos(j),:))); |
220 endfor | 217 endfor |
225 endwhile | 222 endwhile |
226 endfor | 223 endfor |
227 | 224 |
228 ## check if all has been done gives a valid OdePkg struct | 225 ## check if all has been done gives a valid OdePkg struct |
229 ode_struct_value_check (opt); | 226 ode_struct_value_check (opt); |
230 return | 227 return; |
231 endif | 228 endif |
232 | 229 |
233 ## first input argument was not a struct | 230 ## first input argument was not a struct |
234 if (mod (nargin, 2) != 0) | 231 if (mod (nargin, 2) != 0) |
235 error ("OdePkg:InvalidArgument", "odeset expects an even number ", | 232 error ("OdePkg:InvalidArgument", "odeset expects an even number ", |
247 while (1) | 244 while (1) |
248 pos = fuzzy_compare (name, fields); | 245 pos = fuzzy_compare (name, fields); |
249 | 246 |
250 if (size (pos, 1) == 0) | 247 if (size (pos, 1) == 0) |
251 error ("OdePkg:InvalidArgument", | 248 error ("OdePkg:InvalidArgument", |
252 "invalid property. No property found with name ''%s''", name); | 249 "invalid property. No property found with name '%s'", name); |
253 endif | 250 endif |
254 | 251 |
255 if (size (pos, 1) == 1) | 252 if (size (pos, 1) == 1) |
256 if (! strcmp (lower (deblank (name)), | 253 if (! strcmp (lower (deblank (name)), |
257 lower (deblank (fields(pos,:))))) | 254 lower (deblank (fields(pos,:))))) |
258 warning ("OdePkg:InvalidArgument", "no exact matching for ", | 255 warning ("OdePkg:InvalidArgument", "no exact matching for ", |
259 "''%s''. Assuming you were intending ''%s''", | 256 "'%s'. Assuming you were intending '%s'", |
260 name, deblank (fields(pos,:))); | 257 name, deblank (fields(pos,:))); |
261 endif | 258 endif |
262 opt.(deblank (fields(pos,:))) = varargin{i+1}; | 259 opt.(deblank (fields(pos,:))) = varargin{i+1}; |
263 break | 260 break |
264 endif | 261 endif |
265 | 262 |
266 ## if there are more matching, ask the user to be more precise | 263 ## if there are more matching, ask the user to be more precise |
267 warning ("OdePkg:InvalidArgument", "no exact matching for ''%s''. ", | 264 warning ("OdePkg:InvalidArgument", "no exact matching for '%s'. ", |
268 "%d possible fields were found", | 265 "%d possible fields were found", |
269 name, size (pos, 1)); | 266 name, size (pos, 1)); |
270 for j = 1:(size (pos, 1)) | 267 for j = 1:(size (pos, 1)) |
271 fprintf ("%s\n", deblank (fields(pos(j),:))); | 268 fprintf ("%s\n", deblank (fields(pos(j),:))); |
272 endfor | 269 endfor |
277 endwhile | 274 endwhile |
278 endfor | 275 endfor |
279 | 276 |
280 ## check if all has been done gives a valid OdePkg struct | 277 ## check if all has been done gives a valid OdePkg struct |
281 ode_struct_value_check (opt); | 278 ode_struct_value_check (opt); |
279 | |
282 endfunction | 280 endfunction |
283 | 281 |
284 ## function useful to print all the possible options | 282 ## function useful to print all the possible options |
285 function print_options () | 283 function print_options () |
286 | 284 |
287 fprintf ("These following are all possible options.\n", | 285 printf ("These following are all possible options.\n", |
288 "Default values are put in square brackets.\n\n"); | 286 "Default values are put in square brackets.\n\n"); |
289 | 287 |
290 disp (" AbsTol: scalar or vector, >0, [1.e-6]"); | 288 disp (" AbsTol: scalar or vector, >0, [1.e-6]"); |
291 disp (" Algorithm: string, {[''gmres''], ''pcg'', ''bicgstab''}"); | 289 disp (" Algorithm: string, {['gmres'], 'pcg', 'bicgstab'}"); |
292 disp (" BDF: binary, {''on'', [''off'']}"); | 290 disp (" BDF: binary, {'on', ['off']}"); |
293 disp (" Choice: switch, {[1], 2}"); | 291 disp (" Choice: switch, {[1], 2}"); |
294 disp (" Eta: scalar, >=0, <1, [0.5]"); | 292 disp (" Eta: scalar, >=0, <1, [0.5]"); |
295 disp (" Events: function_handle, []"); | 293 disp (" Events: function_handle, []"); |
296 disp (" Explicit: binary, {''yes'', [''no'']}"); | 294 disp (" Explicit: binary, {'yes', ['no']}"); |
297 disp (" InexactSolver: string, {''inexact_newton'', ''fsolve'', []}"); | 295 disp (" InexactSolver: string, {'inexact_newton', 'fsolve', []}"); |
298 disp (" InitialSlope: vector, []"); | 296 disp (" InitialSlope: vector, []"); |
299 disp (" InitialStep: scalar, >0, []"); | 297 disp (" InitialStep: scalar, >0, []"); |
300 disp (" Jacobian: matrix or function_handle, []"); | 298 disp (" Jacobian: matrix or function_handle, []"); |
301 disp (" JConstant: binary, {''on'', [''off'']}"); | 299 disp (" JConstant: binary, {'on', ['off']}"); |
302 disp (" JPattern: sparse matrix, []"); | 300 disp (" JPattern: sparse matrix, []"); |
303 disp (" Mass: matrix or function_handle, []"); | 301 disp (" Mass: matrix or function_handle, []"); |
304 disp (" MassConstant: binary, {''on'', [''off'']}"); | 302 disp (" MassConstant: binary, {'on', ['off']}"); |
305 disp (" MassSingular: switch, {''yes'', [''maybe''], ''no''}"); | 303 disp (" MassSingular: switch, {'yes', ['maybe'], 'no'}"); |
306 disp ("MaxNewtonIterations: scalar, integer, >0, [1.e3]"); | 304 disp ("MaxNewtonIterations: scalar, integer, >0, [1.e3]"); |
307 disp (" MaxOrder: switch, {1, 2, 3, 4, [5]}"); | 305 disp (" MaxOrder: switch, {1, 2, 3, 4, [5]}"); |
308 disp (" MaxStep: scalar, >0, []"); | 306 disp (" MaxStep: scalar, >0, []"); |
309 disp (" MStateDependence: switch, {''none'', [''weak''], ''strong''}"); | 307 disp (" MStateDependence: switch, {'none', ['weak'], 'strong'}"); |
310 disp (" MvPattern: sparse matrix, []"); | 308 disp (" MvPattern: sparse matrix, []"); |
311 disp (" NewtonTol: scalar or vector, >0, []"); | 309 disp (" NewtonTol: scalar or vector, >0, []"); |
312 disp (" NonNegative: vector of integers, []"); | 310 disp (" NonNegative: vector of integers, []"); |
313 disp (" NormControl: binary, {''on'', [''off'']}"); | 311 disp (" NormControl: binary, {'on', ['off']}"); |
314 disp (" OutputFcn: function_handle, []"); | 312 disp (" OutputFcn: function_handle, []"); |
315 disp (" OutputSave: scalar, integer, >0, []"); | 313 disp (" OutputSave: scalar, integer, >0, []"); |
316 disp (" OutputSel: scalar or vector, []"); | 314 disp (" OutputSel: scalar or vector, []"); |
317 disp (" PolynomialDegree: scalar, integer, >0, []"); | 315 disp (" PolynomialDegree: scalar, integer, >0, []"); |
318 disp (" QuadratureOrder: scalar, integer, >0, []"); | 316 disp (" QuadratureOrder: scalar, integer, >0, []"); |
319 disp (" Refine: scalar, integer, >0, []"); | 317 disp (" Refine: scalar, integer, >0, []"); |
320 disp (" RelTol: scalar, >0, [1.e-3]"); | 318 disp (" RelTol: scalar, >0, [1.e-3]"); |
321 disp (" Restart: scalar, integer, >0, [20]"); | 319 disp (" Restart: scalar, integer, >0, [20]"); |
322 disp (" Stats: binary, {''on'', [''off'']}"); | 320 disp (" Stats: binary, {'on', ['off']}"); |
323 disp (" TimeStepNumber: scalar, integer, >0, []"); | 321 disp (" TimeStepNumber: scalar, integer, >0, []"); |
324 disp (" TimeStepSize: scalar, >0, []"); | 322 disp (" TimeStepSize: scalar, >0, []"); |
325 disp (" UseJacobian: binary, {''yes'', [''no'']}"); | 323 disp (" UseJacobian: binary, {'yes', ['no']}"); |
326 disp (" Vectorized: binary, {''on'', [''off'']}"); | 324 disp (" Vectorized: binary, {'on', ['off']}"); |
327 | 325 |
328 endfunction | 326 endfunction |
327 | |
328 | |
329 %!demo | |
330 %! # A new OdePkg options structure with default values is created. | |
331 %! | |
332 %! odeoptA = odeset (); | |
333 %! | |
334 %!demo | |
335 %! # A new OdePkg options structure with manually set options | |
336 %! # "AbsTol" and "RelTol" is created. | |
337 %! | |
338 %! odeoptB = odeset ("AbsTol", 1e-2, "RelTol", 1e-1); | |
339 %! | |
340 %!demo | |
341 %! # A new OdePkg options structure from odeoptB is created with | |
342 %! # a modified value for option "NormControl". | |
343 %! | |
344 %! odeoptB = odeset ("AbsTol", 1e-2, "RelTol", 1e-1); | |
345 %! odeoptC = odeset (odeoptB, "NormControl", "on"); | |
329 | 346 |
330 ## All tests that are needed to check if a correct resp. valid option | 347 ## All tests that are needed to check if a correct resp. valid option |
331 ## has been set are implemented in ode_struct_value_check.m. | 348 ## has been set are implemented in ode_struct_value_check.m. |
332 %! ## Turn off output of warning messages for all tests, turn them on | 349 %! ## Turn off output of warning messages for all tests, turn them on |
333 %! ## again if the last test is called | 350 %! ## again if the last test is called |
334 %! warning ('off', 'OdePkg:InvalidArgument'); | 351 %! warning ("off", "OdePkg:InvalidArgument"); |
335 %!test odeoptA = odeset (); | 352 %!test odeoptA = odeset (); |
336 %!test odeoptB = odeset ('AbsTol', 1e-2, 'RelTol', 1e-1); | 353 %!test odeoptB = odeset ("AbsTol", 1e-2, "RelTol", 1e-1); |
337 %! if (odeoptB.AbsTol != 1e-2), error; endif | 354 %! if (odeoptB.AbsTol != 1e-2), error; endif |
338 %! if (odeoptB.RelTol != 1e-1), error; endif | 355 %! if (odeoptB.RelTol != 1e-1), error; endif |
339 %!test odeoptB = odeset ('AbsTol', 1e-2, 'RelTol', 1e-1); | 356 %!test odeoptB = odeset ("AbsTol", 1e-2, "RelTol", 1e-1); |
340 %! odeoptC = odeset (odeoptB, 'NormControl', 'on'); | 357 %! odeoptC = odeset (odeoptB, "NormControl", "on"); |
341 %!test odeoptB = odeset ('AbsTol', 1e-2, 'RelTol', 1e-1); | 358 %!test odeoptB = odeset ("AbsTol", 1e-2, "RelTol", 1e-1); |
342 %! odeoptC = odeset (odeoptB, 'NormControl', 'on'); | 359 %! odeoptC = odeset (odeoptB, "NormControl", "on"); |
343 %! odeoptD = odeset (odeoptC, odeoptB); | 360 %! odeoptD = odeset (odeoptC, odeoptB); |
344 %! | 361 %! |
345 %! warning ('on', 'OdePkg:InvalidArgument'); | 362 %! warning ("on", "OdePkg:InvalidArgument"); |
346 | 363 |
347 %!demo | |
348 %! # A new OdePkg options structure with default values is created. | |
349 %! | |
350 %! odeoptA = odeset (); | |
351 %! | |
352 %!demo | |
353 %! # A new OdePkg options structure with manually set options | |
354 %! # "AbsTol" and "RelTol" is created. | |
355 %! | |
356 %! odeoptB = odeset ('AbsTol', 1e-2, 'RelTol', 1e-1); | |
357 %! | |
358 %!demo | |
359 %! # A new OdePkg options structure from odeoptB is created with | |
360 %! # a modified value for option "NormControl". | |
361 %! | |
362 %! odeoptB = odeset ('AbsTol', 1e-2, 'RelTol', 1e-1); | |
363 %! odeoptC = odeset (odeoptB, 'NormControl', 'on'); | |
364 | |
365 ## Local Variables: *** | |
366 ## mode: octave *** | |
367 ## End: *** |