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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2928
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <string> |
|
29 |
3567
|
30 #include <iomanip> |
3523
|
31 #include <iostream> |
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: |
|
88 panic_impossible (); |
|
89 break; |
|
90 } |
|
91 |
|
92 return info; |
|
93 } |
|
94 |
|
95 ColumnVector |
|
96 fsolve_user_function (const ColumnVector& x) |
|
97 { |
|
98 ColumnVector retval; |
|
99 |
5275
|
100 octave_idx_type n = x.length (); |
2928
|
101 |
|
102 octave_value_list args; |
|
103 args.resize (1); |
|
104 |
|
105 if (n > 1) |
|
106 { |
|
107 Matrix m (n, 1); |
5275
|
108 for (octave_idx_type i = 0; i < n; i++) |
2928
|
109 m (i, 0) = x (i); |
|
110 octave_value vars (m); |
|
111 args(0) = vars; |
|
112 } |
|
113 else |
|
114 { |
|
115 double d = x (0); |
|
116 octave_value vars (d); |
|
117 args(0) = vars; |
|
118 } |
|
119 |
|
120 if (fsolve_fcn) |
|
121 { |
3544
|
122 octave_value_list tmp = fsolve_fcn->do_multi_index_op (1, args); |
4139
|
123 |
2928
|
124 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
125 { |
4140
|
126 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
4139
|
127 { |
|
128 warning ("fsolve: ignoring imaginary part returned from user-supplied function"); |
4140
|
129 warned_fcn_imaginary = true; |
4139
|
130 } |
|
131 |
3419
|
132 retval = ColumnVector (tmp(0).vector_value ()); |
2928
|
133 |
|
134 if (error_state || retval.length () <= 0) |
|
135 gripe_user_supplied_eval ("fsolve"); |
|
136 } |
|
137 else |
|
138 gripe_user_supplied_eval ("fsolve"); |
|
139 } |
|
140 |
|
141 return retval; |
|
142 } |
|
143 |
4140
|
144 Matrix |
|
145 fsolve_user_jacobian (const ColumnVector& x) |
|
146 { |
|
147 Matrix retval; |
|
148 |
5275
|
149 octave_idx_type n = x.length (); |
4140
|
150 |
|
151 octave_value_list args; |
|
152 args.resize (1); |
|
153 |
|
154 if (n > 1) |
|
155 { |
|
156 Matrix m (n, 1); |
5275
|
157 for (octave_idx_type i = 0; i < n; i++) |
4628
|
158 m(i,0) = x(i); |
4140
|
159 octave_value vars (m); |
|
160 args(0) = vars; |
|
161 } |
|
162 else |
|
163 { |
4628
|
164 double d = x(0); |
4140
|
165 octave_value vars (d); |
|
166 args(0) = vars; |
|
167 } |
|
168 |
|
169 if (fsolve_fcn) |
|
170 { |
|
171 octave_value_list tmp = fsolve_jac->do_multi_index_op (1, args); |
|
172 |
|
173 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
174 { |
|
175 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
176 { |
|
177 warning ("fsolve: ignoring imaginary part returned from user-supplied jacobian function"); |
|
178 warned_fcn_imaginary = true; |
|
179 } |
|
180 |
|
181 retval = tmp(0).matrix_value (); |
|
182 |
|
183 if (error_state || retval.length () <= 0) |
|
184 gripe_user_supplied_eval ("fsolve"); |
|
185 } |
|
186 else |
|
187 gripe_user_supplied_eval ("fsolve"); |
|
188 } |
|
189 |
|
190 return retval; |
|
191 } |
|
192 |
3323
|
193 #define FSOLVE_ABORT() \ |
|
194 do \ |
|
195 { \ |
|
196 unwind_protect::run_frame ("Ffsolve"); \ |
|
197 return retval; \ |
|
198 } \ |
|
199 while (0) |
|
200 |
|
201 #define FSOLVE_ABORT1(msg) \ |
|
202 do \ |
|
203 { \ |
3747
|
204 ::error ("fsolve: " msg); \ |
3323
|
205 FSOLVE_ABORT (); \ |
|
206 } \ |
|
207 while (0) |
|
208 |
|
209 #define FSOLVE_ABORT2(fmt, arg) \ |
|
210 do \ |
|
211 { \ |
3747
|
212 ::error ("fsolve: " fmt, arg); \ |
3323
|
213 FSOLVE_ABORT (); \ |
|
214 } \ |
|
215 while (0) |
|
216 |
2928
|
217 DEFUN_DLD (fsolve, args, nargout, |
3368
|
218 "-*- texinfo -*-\n\ |
3971
|
219 @deftypefn {Loadable Function} {[@var{x}, @var{info}, @var{msg}] =} fsolve (@var{fcn}, @var{x0})\n\ |
3368
|
220 Given @var{fcn}, the name of a function of the form @code{f (@var{x})}\n\ |
|
221 and an initial starting point @var{x0}, @code{fsolve} solves the set of\n\ |
|
222 equations such that @code{f(@var{x}) == 0}.\n\ |
3964
|
223 \n\ |
5729
|
224 If @var{fcn} is a two-element string array, or a two element cell array\n\ |
|
225 containing either the function name or inline or function handle. The\n\ |
|
226 first element names the function @math{f} described above, and the second\n\ |
|
227 element names a function of the form @code{j (@var{x})} to compute the\n\ |
|
228 Jacobian matrix with elements\n\ |
4144
|
229 @tex\n\ |
|
230 $$ J = {\\partial f_i \\over \\partial x_j} $$\n\ |
|
231 @end tex\n\ |
|
232 @ifinfo\n\ |
|
233 \n\ |
|
234 @example\n\ |
4197
|
235 df_i\n\ |
|
236 jac(i,j) = ----\n\ |
|
237 dx_j\n\ |
4144
|
238 @end example\n\ |
|
239 @end ifinfo\n\ |
|
240 \n\ |
3964
|
241 You can use the function @code{fsolve_options} to set optional\n\ |
|
242 parameters for @code{fsolve}.\n\ |
3368
|
243 @end deftypefn") |
2928
|
244 { |
|
245 octave_value_list retval; |
|
246 |
4140
|
247 warned_fcn_imaginary = false; |
|
248 warned_jac_imaginary = false; |
4139
|
249 |
3243
|
250 unwind_protect::begin_frame ("Ffsolve"); |
2928
|
251 |
3243
|
252 unwind_protect_int (call_depth); |
|
253 call_depth++; |
|
254 |
|
255 if (call_depth > 1) |
3323
|
256 FSOLVE_ABORT1 ("invalid recursive call"); |
2928
|
257 |
3243
|
258 int nargin = args.length (); |
2928
|
259 |
3243
|
260 if (nargin == 2 && nargout < 4) |
|
261 { |
5729
|
262 std::string fcn_name, fname, jac_name, jname; |
4140
|
263 fsolve_fcn = 0; |
|
264 fsolve_jac = 0; |
|
265 |
|
266 octave_value f_arg = args(0); |
|
267 |
5729
|
268 if (f_arg.is_cell ()) |
|
269 { |
|
270 Cell c = f_arg.cell_value (); |
|
271 if (c.length() == 1) |
|
272 f_arg = c(0); |
|
273 else if (c.length() == 2) |
|
274 { |
|
275 if (c(0).is_function_handle () || c(0).is_inline_function ()) |
|
276 fsolve_fcn = c(0).function_value (); |
|
277 else |
|
278 { |
|
279 fcn_name = unique_symbol_name ("__fsolve_fcn__"); |
|
280 fname = "function y = "; |
|
281 fname.append (fcn_name); |
|
282 fname.append (" (x) y = "); |
|
283 fsolve_fcn = extract_function |
|
284 (c(0), "fsolve", fcn_name, fname, "; endfunction"); |
|
285 } |
|
286 |
|
287 if (fsolve_fcn) |
|
288 { |
|
289 if (c(1).is_function_handle () || c(1).is_inline_function ()) |
|
290 fsolve_jac = c(1).function_value (); |
|
291 else |
|
292 { |
|
293 jac_name = unique_symbol_name ("__fsolve_jac__"); |
|
294 jname = "function y = "; |
|
295 jname.append (jac_name); |
|
296 jname.append (" (x) jac = "); |
|
297 fsolve_jac = extract_function |
|
298 (c(1), "fsolve", jac_name, jname, "; endfunction"); |
4140
|
299 |
5729
|
300 if (!fsolve_jac) |
|
301 { |
|
302 if (fcn_name.length()) |
|
303 clear_function (fcn_name); |
|
304 fsolve_fcn = 0; |
|
305 } |
|
306 } |
|
307 } |
|
308 } |
|
309 else |
|
310 FSOLVE_ABORT1 ("incorrect number of elements in cell array"); |
|
311 } |
4140
|
312 |
5729
|
313 if (!fsolve_fcn && ! f_arg.is_cell()) |
|
314 { |
|
315 if (f_arg.is_function_handle () || f_arg.is_inline_function ()) |
|
316 fsolve_fcn = f_arg.function_value (); |
|
317 else |
|
318 { |
|
319 switch (f_arg.rows ()) |
|
320 { |
|
321 case 1: |
|
322 do |
|
323 { |
|
324 fcn_name = unique_symbol_name ("__fsolve_fcn__"); |
|
325 fname = "function y = "; |
|
326 fname.append (fcn_name); |
|
327 fname.append (" (x) y = "); |
|
328 fsolve_fcn = extract_function |
|
329 (f_arg, "fsolve", fcn_name, fname, "; endfunction"); |
|
330 } |
|
331 while (0); |
|
332 break; |
4140
|
333 |
5729
|
334 case 2: |
4140
|
335 { |
5729
|
336 string_vector tmp = f_arg.all_strings (); |
|
337 |
|
338 if (! error_state) |
|
339 { |
|
340 fcn_name = unique_symbol_name ("__fsolve_fcn__"); |
|
341 fname = "function y = "; |
|
342 fname.append (fcn_name); |
|
343 fname.append (" (x) y = "); |
|
344 fsolve_fcn = extract_function |
|
345 (tmp(0), "fsolve", fcn_name, fname, "; endfunction"); |
4140
|
346 |
5729
|
347 if (fsolve_fcn) |
|
348 { |
|
349 jac_name = unique_symbol_name ("__fsolve_jac__"); |
|
350 jname = "function y = "; |
|
351 jname.append (jac_name); |
|
352 jname.append (" (x) jac = "); |
|
353 fsolve_jac = extract_function |
|
354 (tmp(1), "fsolve", jac_name, jname, |
|
355 "; endfunction"); |
|
356 |
|
357 if (!fsolve_jac) |
|
358 { |
|
359 if (fcn_name.length()) |
|
360 clear_function (fcn_name); |
|
361 fsolve_fcn = 0; |
|
362 } |
|
363 } |
|
364 } |
4140
|
365 } |
5729
|
366 } |
|
367 } |
4140
|
368 } |
|
369 |
|
370 if (error_state || ! fsolve_fcn) |
3323
|
371 FSOLVE_ABORT (); |
2928
|
372 |
3419
|
373 ColumnVector x (args(1).vector_value ()); |
2928
|
374 |
3243
|
375 if (error_state) |
3323
|
376 FSOLVE_ABORT1 ("expecting vector as second argument"); |
3243
|
377 |
4132
|
378 if (nargin > 3) |
3243
|
379 warning ("fsolve: ignoring extra arguments"); |
2928
|
380 |
4132
|
381 if (nargout > 3) |
3243
|
382 warning ("fsolve: can't compute path output yet"); |
2928
|
383 |
3971
|
384 NLFunc nleqn_fcn (fsolve_user_function); |
4140
|
385 if (fsolve_jac) |
|
386 nleqn_fcn.set_jacobian_function (fsolve_user_jacobian); |
|
387 |
3971
|
388 NLEqn nleqn (x, nleqn_fcn); |
4122
|
389 nleqn.set_options (fsolve_opts); |
3243
|
390 |
5275
|
391 octave_idx_type info; |
3971
|
392 ColumnVector soln = nleqn.solve (info); |
2928
|
393 |
5729
|
394 if (fcn_name.length()) |
|
395 clear_function (fcn_name); |
|
396 if (jac_name.length()) |
|
397 clear_function (jac_name); |
|
398 |
3971
|
399 if (! error_state) |
|
400 { |
|
401 std::string msg = nleqn.error_message (); |
|
402 |
|
403 retval(2) = msg; |
|
404 retval(1) = static_cast<double> (hybrd_info_to_fsolve_info (info)); |
2928
|
405 |
4132
|
406 retval(0) = soln; |
2928
|
407 |
4132
|
408 if (! nleqn.solution_ok () && nargout < 2) |
|
409 error ("fsolve: %s", msg.c_str ()); |
3971
|
410 } |
3243
|
411 } |
|
412 else |
5823
|
413 print_usage (); |
2928
|
414 |
3243
|
415 unwind_protect::run_frame ("Ffsolve"); |
2928
|
416 |
|
417 return retval; |
|
418 } |
|
419 |
|
420 /* |
|
421 ;;; Local Variables: *** |
|
422 ;;; mode: C++ *** |
|
423 ;;; End: *** |
|
424 */ |