2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2928
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2928
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
3567
|
29 #include <iomanip> |
3523
|
30 #include <iostream> |
5856
|
31 #include <sstream> |
2928
|
32 |
|
33 #include "NLEqn.h" |
|
34 |
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "oct-obj.h" |
2968
|
39 #include "ov-fcn.h" |
5729
|
40 #include "ov-cell.h" |
2928
|
41 #include "pager.h" |
3243
|
42 #include "unwind-prot.h" |
2928
|
43 #include "utils.h" |
|
44 #include "variables.h" |
|
45 |
3998
|
46 #include "NLEqn-opts.cc" |
|
47 |
2928
|
48 // Global pointer for user defined function required by hybrd1. |
2968
|
49 static octave_function *fsolve_fcn; |
2928
|
50 |
4140
|
51 // Global pointer for optional user defined jacobian function. |
|
52 static octave_function *fsolve_jac; |
|
53 |
4139
|
54 // Have we warned about imaginary values returned from user function? |
4140
|
55 static bool warned_fcn_imaginary = false; |
|
56 static bool warned_jac_imaginary = false; |
4139
|
57 |
3243
|
58 // Is this a recursive call? |
|
59 static int call_depth = 0; |
|
60 |
5275
|
61 octave_idx_type |
|
62 hybrd_info_to_fsolve_info (octave_idx_type info) |
2928
|
63 { |
|
64 switch (info) |
|
65 { |
|
66 case -1: |
|
67 info = -2; |
|
68 break; |
|
69 |
|
70 case 0: |
|
71 info = -1; |
|
72 break; |
|
73 |
|
74 case 1: |
|
75 break; |
|
76 |
|
77 case 2: |
|
78 info = 4; |
|
79 break; |
|
80 |
|
81 case 3: |
|
82 case 4: |
|
83 case 5: |
|
84 info = 3; |
|
85 break; |
|
86 |
|
87 default: |
5856
|
88 { |
|
89 std::ostringstream buf; |
|
90 buf << "fsolve: unrecognized value of INFO from MINPACK (= " |
|
91 << info << ")"; |
|
92 std::string msg = buf.str (); |
|
93 warning (msg.c_str ()); |
|
94 } |
2928
|
95 break; |
|
96 } |
|
97 |
|
98 return info; |
|
99 } |
|
100 |
|
101 ColumnVector |
|
102 fsolve_user_function (const ColumnVector& x) |
|
103 { |
|
104 ColumnVector retval; |
|
105 |
5275
|
106 octave_idx_type n = x.length (); |
2928
|
107 |
|
108 octave_value_list args; |
|
109 args.resize (1); |
|
110 |
|
111 if (n > 1) |
|
112 { |
|
113 Matrix m (n, 1); |
5275
|
114 for (octave_idx_type i = 0; i < n; i++) |
2928
|
115 m (i, 0) = x (i); |
|
116 octave_value vars (m); |
|
117 args(0) = vars; |
|
118 } |
|
119 else |
|
120 { |
|
121 double d = x (0); |
|
122 octave_value vars (d); |
|
123 args(0) = vars; |
|
124 } |
|
125 |
|
126 if (fsolve_fcn) |
|
127 { |
3544
|
128 octave_value_list tmp = fsolve_fcn->do_multi_index_op (1, args); |
4139
|
129 |
2928
|
130 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
131 { |
4140
|
132 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
4139
|
133 { |
|
134 warning ("fsolve: ignoring imaginary part returned from user-supplied function"); |
4140
|
135 warned_fcn_imaginary = true; |
4139
|
136 } |
|
137 |
3419
|
138 retval = ColumnVector (tmp(0).vector_value ()); |
2928
|
139 |
|
140 if (error_state || retval.length () <= 0) |
|
141 gripe_user_supplied_eval ("fsolve"); |
|
142 } |
|
143 else |
|
144 gripe_user_supplied_eval ("fsolve"); |
|
145 } |
|
146 |
|
147 return retval; |
|
148 } |
|
149 |
4140
|
150 Matrix |
|
151 fsolve_user_jacobian (const ColumnVector& x) |
|
152 { |
|
153 Matrix retval; |
|
154 |
5275
|
155 octave_idx_type n = x.length (); |
4140
|
156 |
|
157 octave_value_list args; |
|
158 args.resize (1); |
|
159 |
|
160 if (n > 1) |
|
161 { |
|
162 Matrix m (n, 1); |
5275
|
163 for (octave_idx_type i = 0; i < n; i++) |
4628
|
164 m(i,0) = x(i); |
4140
|
165 octave_value vars (m); |
|
166 args(0) = vars; |
|
167 } |
|
168 else |
|
169 { |
4628
|
170 double d = x(0); |
4140
|
171 octave_value vars (d); |
|
172 args(0) = vars; |
|
173 } |
|
174 |
|
175 if (fsolve_fcn) |
|
176 { |
|
177 octave_value_list tmp = fsolve_jac->do_multi_index_op (1, args); |
|
178 |
|
179 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
180 { |
|
181 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
182 { |
|
183 warning ("fsolve: ignoring imaginary part returned from user-supplied jacobian function"); |
|
184 warned_fcn_imaginary = true; |
|
185 } |
|
186 |
|
187 retval = tmp(0).matrix_value (); |
|
188 |
|
189 if (error_state || retval.length () <= 0) |
|
190 gripe_user_supplied_eval ("fsolve"); |
|
191 } |
|
192 else |
|
193 gripe_user_supplied_eval ("fsolve"); |
|
194 } |
|
195 |
|
196 return retval; |
|
197 } |
|
198 |
3323
|
199 #define FSOLVE_ABORT() \ |
|
200 do \ |
|
201 { \ |
|
202 unwind_protect::run_frame ("Ffsolve"); \ |
|
203 return retval; \ |
|
204 } \ |
|
205 while (0) |
|
206 |
|
207 #define FSOLVE_ABORT1(msg) \ |
|
208 do \ |
|
209 { \ |
3747
|
210 ::error ("fsolve: " msg); \ |
3323
|
211 FSOLVE_ABORT (); \ |
|
212 } \ |
|
213 while (0) |
|
214 |
|
215 #define FSOLVE_ABORT2(fmt, arg) \ |
|
216 do \ |
|
217 { \ |
3747
|
218 ::error ("fsolve: " fmt, arg); \ |
3323
|
219 FSOLVE_ABORT (); \ |
|
220 } \ |
|
221 while (0) |
|
222 |
2928
|
223 DEFUN_DLD (fsolve, args, nargout, |
3368
|
224 "-*- texinfo -*-\n\ |
3971
|
225 @deftypefn {Loadable Function} {[@var{x}, @var{info}, @var{msg}] =} fsolve (@var{fcn}, @var{x0})\n\ |
3368
|
226 Given @var{fcn}, the name of a function of the form @code{f (@var{x})}\n\ |
|
227 and an initial starting point @var{x0}, @code{fsolve} solves the set of\n\ |
|
228 equations such that @code{f(@var{x}) == 0}.\n\ |
3964
|
229 \n\ |
5729
|
230 If @var{fcn} is a two-element string array, or a two element cell array\n\ |
|
231 containing either the function name or inline or function handle. The\n\ |
|
232 first element names the function @math{f} described above, and the second\n\ |
|
233 element names a function of the form @code{j (@var{x})} to compute the\n\ |
|
234 Jacobian matrix with elements\n\ |
4144
|
235 @tex\n\ |
|
236 $$ J = {\\partial f_i \\over \\partial x_j} $$\n\ |
|
237 @end tex\n\ |
|
238 @ifinfo\n\ |
|
239 \n\ |
|
240 @example\n\ |
4197
|
241 df_i\n\ |
|
242 jac(i,j) = ----\n\ |
|
243 dx_j\n\ |
4144
|
244 @end example\n\ |
|
245 @end ifinfo\n\ |
|
246 \n\ |
3964
|
247 You can use the function @code{fsolve_options} to set optional\n\ |
|
248 parameters for @code{fsolve}.\n\ |
3368
|
249 @end deftypefn") |
2928
|
250 { |
|
251 octave_value_list retval; |
|
252 |
4140
|
253 warned_fcn_imaginary = false; |
|
254 warned_jac_imaginary = false; |
4139
|
255 |
3243
|
256 unwind_protect::begin_frame ("Ffsolve"); |
2928
|
257 |
3243
|
258 unwind_protect_int (call_depth); |
|
259 call_depth++; |
|
260 |
|
261 if (call_depth > 1) |
3323
|
262 FSOLVE_ABORT1 ("invalid recursive call"); |
2928
|
263 |
3243
|
264 int nargin = args.length (); |
2928
|
265 |
3243
|
266 if (nargin == 2 && nargout < 4) |
|
267 { |
5729
|
268 std::string fcn_name, fname, jac_name, jname; |
4140
|
269 fsolve_fcn = 0; |
|
270 fsolve_jac = 0; |
|
271 |
|
272 octave_value f_arg = args(0); |
|
273 |
5729
|
274 if (f_arg.is_cell ()) |
|
275 { |
|
276 Cell c = f_arg.cell_value (); |
|
277 if (c.length() == 1) |
|
278 f_arg = c(0); |
|
279 else if (c.length() == 2) |
|
280 { |
|
281 if (c(0).is_function_handle () || c(0).is_inline_function ()) |
|
282 fsolve_fcn = c(0).function_value (); |
|
283 else |
|
284 { |
|
285 fcn_name = unique_symbol_name ("__fsolve_fcn__"); |
|
286 fname = "function y = "; |
|
287 fname.append (fcn_name); |
|
288 fname.append (" (x) y = "); |
|
289 fsolve_fcn = extract_function |
|
290 (c(0), "fsolve", fcn_name, fname, "; endfunction"); |
|
291 } |
|
292 |
|
293 if (fsolve_fcn) |
|
294 { |
|
295 if (c(1).is_function_handle () || c(1).is_inline_function ()) |
|
296 fsolve_jac = c(1).function_value (); |
|
297 else |
|
298 { |
|
299 jac_name = unique_symbol_name ("__fsolve_jac__"); |
|
300 jname = "function y = "; |
|
301 jname.append (jac_name); |
|
302 jname.append (" (x) jac = "); |
|
303 fsolve_jac = extract_function |
|
304 (c(1), "fsolve", jac_name, jname, "; endfunction"); |
4140
|
305 |
5729
|
306 if (!fsolve_jac) |
|
307 { |
|
308 if (fcn_name.length()) |
|
309 clear_function (fcn_name); |
|
310 fsolve_fcn = 0; |
|
311 } |
|
312 } |
|
313 } |
|
314 } |
|
315 else |
|
316 FSOLVE_ABORT1 ("incorrect number of elements in cell array"); |
|
317 } |
4140
|
318 |
5729
|
319 if (!fsolve_fcn && ! f_arg.is_cell()) |
|
320 { |
|
321 if (f_arg.is_function_handle () || f_arg.is_inline_function ()) |
|
322 fsolve_fcn = f_arg.function_value (); |
|
323 else |
|
324 { |
|
325 switch (f_arg.rows ()) |
|
326 { |
|
327 case 1: |
|
328 do |
|
329 { |
|
330 fcn_name = unique_symbol_name ("__fsolve_fcn__"); |
|
331 fname = "function y = "; |
|
332 fname.append (fcn_name); |
|
333 fname.append (" (x) y = "); |
|
334 fsolve_fcn = extract_function |
|
335 (f_arg, "fsolve", fcn_name, fname, "; endfunction"); |
|
336 } |
|
337 while (0); |
|
338 break; |
4140
|
339 |
5729
|
340 case 2: |
4140
|
341 { |
5729
|
342 string_vector tmp = f_arg.all_strings (); |
|
343 |
|
344 if (! error_state) |
|
345 { |
|
346 fcn_name = unique_symbol_name ("__fsolve_fcn__"); |
|
347 fname = "function y = "; |
|
348 fname.append (fcn_name); |
|
349 fname.append (" (x) y = "); |
|
350 fsolve_fcn = extract_function |
|
351 (tmp(0), "fsolve", fcn_name, fname, "; endfunction"); |
4140
|
352 |
5729
|
353 if (fsolve_fcn) |
|
354 { |
|
355 jac_name = unique_symbol_name ("__fsolve_jac__"); |
|
356 jname = "function y = "; |
|
357 jname.append (jac_name); |
|
358 jname.append (" (x) jac = "); |
|
359 fsolve_jac = extract_function |
|
360 (tmp(1), "fsolve", jac_name, jname, |
|
361 "; endfunction"); |
|
362 |
|
363 if (!fsolve_jac) |
|
364 { |
|
365 if (fcn_name.length()) |
|
366 clear_function (fcn_name); |
|
367 fsolve_fcn = 0; |
|
368 } |
|
369 } |
|
370 } |
4140
|
371 } |
5729
|
372 } |
|
373 } |
4140
|
374 } |
|
375 |
|
376 if (error_state || ! fsolve_fcn) |
3323
|
377 FSOLVE_ABORT (); |
2928
|
378 |
3419
|
379 ColumnVector x (args(1).vector_value ()); |
2928
|
380 |
3243
|
381 if (error_state) |
3323
|
382 FSOLVE_ABORT1 ("expecting vector as second argument"); |
3243
|
383 |
4132
|
384 if (nargin > 3) |
3243
|
385 warning ("fsolve: ignoring extra arguments"); |
2928
|
386 |
4132
|
387 if (nargout > 3) |
3243
|
388 warning ("fsolve: can't compute path output yet"); |
2928
|
389 |
3971
|
390 NLFunc nleqn_fcn (fsolve_user_function); |
4140
|
391 if (fsolve_jac) |
|
392 nleqn_fcn.set_jacobian_function (fsolve_user_jacobian); |
|
393 |
3971
|
394 NLEqn nleqn (x, nleqn_fcn); |
4122
|
395 nleqn.set_options (fsolve_opts); |
3243
|
396 |
5275
|
397 octave_idx_type info; |
3971
|
398 ColumnVector soln = nleqn.solve (info); |
2928
|
399 |
5729
|
400 if (fcn_name.length()) |
|
401 clear_function (fcn_name); |
|
402 if (jac_name.length()) |
|
403 clear_function (jac_name); |
|
404 |
3971
|
405 if (! error_state) |
|
406 { |
|
407 std::string msg = nleqn.error_message (); |
|
408 |
|
409 retval(2) = msg; |
|
410 retval(1) = static_cast<double> (hybrd_info_to_fsolve_info (info)); |
2928
|
411 |
4132
|
412 retval(0) = soln; |
2928
|
413 |
4132
|
414 if (! nleqn.solution_ok () && nargout < 2) |
|
415 error ("fsolve: %s", msg.c_str ()); |
3971
|
416 } |
3243
|
417 } |
|
418 else |
5823
|
419 print_usage (); |
2928
|
420 |
3243
|
421 unwind_protect::run_frame ("Ffsolve"); |
2928
|
422 |
|
423 return retval; |
|
424 } |
|
425 |
|
426 /* |
|
427 ;;; Local Variables: *** |
|
428 ;;; mode: C++ *** |
|
429 ;;; End: *** |
|
430 */ |