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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
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> |
2928
|
31 |
|
32 #include "NLEqn.h" |
|
33 |
|
34 #include "defun-dld.h" |
|
35 #include "error.h" |
|
36 #include "gripes.h" |
|
37 #include "oct-obj.h" |
2968
|
38 #include "ov-fcn.h" |
2928
|
39 #include "pager.h" |
3243
|
40 #include "unwind-prot.h" |
2928
|
41 #include "utils.h" |
|
42 #include "variables.h" |
|
43 |
3998
|
44 #include "NLEqn-opts.cc" |
|
45 |
2928
|
46 // Global pointer for user defined function required by hybrd1. |
2968
|
47 static octave_function *fsolve_fcn; |
2928
|
48 |
4140
|
49 // Global pointer for optional user defined jacobian function. |
|
50 static octave_function *fsolve_jac; |
|
51 |
4139
|
52 // Have we warned about imaginary values returned from user function? |
4140
|
53 static bool warned_fcn_imaginary = false; |
|
54 static bool warned_jac_imaginary = false; |
4139
|
55 |
3243
|
56 // Is this a recursive call? |
|
57 static int call_depth = 0; |
|
58 |
5275
|
59 octave_idx_type |
|
60 hybrd_info_to_fsolve_info (octave_idx_type info) |
2928
|
61 { |
|
62 switch (info) |
|
63 { |
|
64 case -1: |
|
65 info = -2; |
|
66 break; |
|
67 |
|
68 case 0: |
|
69 info = -1; |
|
70 break; |
|
71 |
|
72 case 1: |
|
73 break; |
|
74 |
|
75 case 2: |
|
76 info = 4; |
|
77 break; |
|
78 |
|
79 case 3: |
|
80 case 4: |
|
81 case 5: |
|
82 info = 3; |
|
83 break; |
|
84 |
|
85 default: |
|
86 panic_impossible (); |
|
87 break; |
|
88 } |
|
89 |
|
90 return info; |
|
91 } |
|
92 |
|
93 ColumnVector |
|
94 fsolve_user_function (const ColumnVector& x) |
|
95 { |
|
96 ColumnVector retval; |
|
97 |
5275
|
98 octave_idx_type n = x.length (); |
2928
|
99 |
|
100 octave_value_list args; |
|
101 args.resize (1); |
|
102 |
|
103 if (n > 1) |
|
104 { |
|
105 Matrix m (n, 1); |
5275
|
106 for (octave_idx_type i = 0; i < n; i++) |
2928
|
107 m (i, 0) = x (i); |
|
108 octave_value vars (m); |
|
109 args(0) = vars; |
|
110 } |
|
111 else |
|
112 { |
|
113 double d = x (0); |
|
114 octave_value vars (d); |
|
115 args(0) = vars; |
|
116 } |
|
117 |
|
118 if (fsolve_fcn) |
|
119 { |
3544
|
120 octave_value_list tmp = fsolve_fcn->do_multi_index_op (1, args); |
4139
|
121 |
2928
|
122 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
123 { |
4140
|
124 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
4139
|
125 { |
|
126 warning ("fsolve: ignoring imaginary part returned from user-supplied function"); |
4140
|
127 warned_fcn_imaginary = true; |
4139
|
128 } |
|
129 |
3419
|
130 retval = ColumnVector (tmp(0).vector_value ()); |
2928
|
131 |
|
132 if (error_state || retval.length () <= 0) |
|
133 gripe_user_supplied_eval ("fsolve"); |
|
134 } |
|
135 else |
|
136 gripe_user_supplied_eval ("fsolve"); |
|
137 } |
|
138 |
|
139 return retval; |
|
140 } |
|
141 |
4140
|
142 Matrix |
|
143 fsolve_user_jacobian (const ColumnVector& x) |
|
144 { |
|
145 Matrix retval; |
|
146 |
5275
|
147 octave_idx_type n = x.length (); |
4140
|
148 |
|
149 octave_value_list args; |
|
150 args.resize (1); |
|
151 |
|
152 if (n > 1) |
|
153 { |
|
154 Matrix m (n, 1); |
5275
|
155 for (octave_idx_type i = 0; i < n; i++) |
4628
|
156 m(i,0) = x(i); |
4140
|
157 octave_value vars (m); |
|
158 args(0) = vars; |
|
159 } |
|
160 else |
|
161 { |
4628
|
162 double d = x(0); |
4140
|
163 octave_value vars (d); |
|
164 args(0) = vars; |
|
165 } |
|
166 |
|
167 if (fsolve_fcn) |
|
168 { |
|
169 octave_value_list tmp = fsolve_jac->do_multi_index_op (1, args); |
|
170 |
|
171 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
172 { |
|
173 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
174 { |
|
175 warning ("fsolve: ignoring imaginary part returned from user-supplied jacobian function"); |
|
176 warned_fcn_imaginary = true; |
|
177 } |
|
178 |
|
179 retval = tmp(0).matrix_value (); |
|
180 |
|
181 if (error_state || retval.length () <= 0) |
|
182 gripe_user_supplied_eval ("fsolve"); |
|
183 } |
|
184 else |
|
185 gripe_user_supplied_eval ("fsolve"); |
|
186 } |
|
187 |
|
188 return retval; |
|
189 } |
|
190 |
3323
|
191 #define FSOLVE_ABORT() \ |
|
192 do \ |
|
193 { \ |
|
194 unwind_protect::run_frame ("Ffsolve"); \ |
|
195 return retval; \ |
|
196 } \ |
|
197 while (0) |
|
198 |
|
199 #define FSOLVE_ABORT1(msg) \ |
|
200 do \ |
|
201 { \ |
3747
|
202 ::error ("fsolve: " msg); \ |
3323
|
203 FSOLVE_ABORT (); \ |
|
204 } \ |
|
205 while (0) |
|
206 |
|
207 #define FSOLVE_ABORT2(fmt, arg) \ |
|
208 do \ |
|
209 { \ |
3747
|
210 ::error ("fsolve: " fmt, arg); \ |
3323
|
211 FSOLVE_ABORT (); \ |
|
212 } \ |
|
213 while (0) |
|
214 |
2928
|
215 DEFUN_DLD (fsolve, args, nargout, |
3368
|
216 "-*- texinfo -*-\n\ |
3971
|
217 @deftypefn {Loadable Function} {[@var{x}, @var{info}, @var{msg}] =} fsolve (@var{fcn}, @var{x0})\n\ |
3368
|
218 Given @var{fcn}, the name of a function of the form @code{f (@var{x})}\n\ |
|
219 and an initial starting point @var{x0}, @code{fsolve} solves the set of\n\ |
|
220 equations such that @code{f(@var{x}) == 0}.\n\ |
3964
|
221 \n\ |
4144
|
222 If @var{fcn} is a two-element string array, the first element names\n\ |
|
223 the function @math{f} described above, and the second element names\n\ |
|
224 a function of the form @code{j (@var{x})} to compute the Jacobian\n\ |
|
225 matrix with elements\n\ |
|
226 @tex\n\ |
|
227 $$ J = {\\partial f_i \\over \\partial x_j} $$\n\ |
|
228 @end tex\n\ |
|
229 @ifinfo\n\ |
|
230 \n\ |
|
231 @example\n\ |
4197
|
232 df_i\n\ |
|
233 jac(i,j) = ----\n\ |
|
234 dx_j\n\ |
4144
|
235 @end example\n\ |
|
236 @end ifinfo\n\ |
|
237 \n\ |
3964
|
238 You can use the function @code{fsolve_options} to set optional\n\ |
|
239 parameters for @code{fsolve}.\n\ |
3368
|
240 @end deftypefn") |
2928
|
241 { |
|
242 octave_value_list retval; |
|
243 |
4140
|
244 warned_fcn_imaginary = false; |
|
245 warned_jac_imaginary = false; |
4139
|
246 |
3243
|
247 unwind_protect::begin_frame ("Ffsolve"); |
2928
|
248 |
3243
|
249 unwind_protect_int (call_depth); |
|
250 call_depth++; |
|
251 |
|
252 if (call_depth > 1) |
3323
|
253 FSOLVE_ABORT1 ("invalid recursive call"); |
2928
|
254 |
3243
|
255 int nargin = args.length (); |
2928
|
256 |
3243
|
257 if (nargin == 2 && nargout < 4) |
|
258 { |
4140
|
259 fsolve_fcn = 0; |
|
260 fsolve_jac = 0; |
|
261 |
|
262 octave_value f_arg = args(0); |
|
263 |
|
264 switch (f_arg.rows ()) |
|
265 { |
|
266 case 1: |
|
267 fsolve_fcn = extract_function |
|
268 (f_arg, "fsolve", "__fsolve_fcn__", |
|
269 "function y = __fsolve_fcn__ (x) y = ", |
|
270 "; endfunction"); |
|
271 break; |
|
272 |
|
273 case 2: |
|
274 { |
|
275 string_vector tmp = f_arg.all_strings (); |
|
276 |
|
277 if (! error_state) |
|
278 { |
|
279 fsolve_fcn = extract_function |
|
280 (tmp(0), "fsolve", "__fsolve_fcn__", |
|
281 "function y = __fsolve_fcn__ (x) y = ", |
|
282 "; endfunction"); |
|
283 |
|
284 if (fsolve_fcn) |
|
285 { |
|
286 fsolve_jac = extract_function |
|
287 (tmp(1), "fsolve", "__fsolve_jac__", |
|
288 "function jac = __fsolve_jac__ (x) jac = ", |
|
289 "; endfunction"); |
|
290 |
|
291 if (! fsolve_jac) |
|
292 fsolve_fcn = 0; |
|
293 } |
|
294 } |
|
295 } |
|
296 } |
|
297 |
|
298 if (error_state || ! fsolve_fcn) |
3323
|
299 FSOLVE_ABORT (); |
2928
|
300 |
3419
|
301 ColumnVector x (args(1).vector_value ()); |
2928
|
302 |
3243
|
303 if (error_state) |
3323
|
304 FSOLVE_ABORT1 ("expecting vector as second argument"); |
3243
|
305 |
4132
|
306 if (nargin > 3) |
3243
|
307 warning ("fsolve: ignoring extra arguments"); |
2928
|
308 |
4132
|
309 if (nargout > 3) |
3243
|
310 warning ("fsolve: can't compute path output yet"); |
2928
|
311 |
3971
|
312 NLFunc nleqn_fcn (fsolve_user_function); |
4140
|
313 if (fsolve_jac) |
|
314 nleqn_fcn.set_jacobian_function (fsolve_user_jacobian); |
|
315 |
3971
|
316 NLEqn nleqn (x, nleqn_fcn); |
4122
|
317 nleqn.set_options (fsolve_opts); |
3243
|
318 |
5275
|
319 octave_idx_type info; |
3971
|
320 ColumnVector soln = nleqn.solve (info); |
2928
|
321 |
3971
|
322 if (! error_state) |
|
323 { |
|
324 std::string msg = nleqn.error_message (); |
|
325 |
|
326 retval(2) = msg; |
|
327 retval(1) = static_cast<double> (hybrd_info_to_fsolve_info (info)); |
2928
|
328 |
4132
|
329 retval(0) = soln; |
2928
|
330 |
4132
|
331 if (! nleqn.solution_ok () && nargout < 2) |
|
332 error ("fsolve: %s", msg.c_str ()); |
3971
|
333 } |
3243
|
334 } |
|
335 else |
|
336 print_usage ("fsolve"); |
2928
|
337 |
3243
|
338 unwind_protect::run_frame ("Ffsolve"); |
2928
|
339 |
|
340 return retval; |
|
341 } |
|
342 |
|
343 /* |
|
344 ;;; Local Variables: *** |
|
345 ;;; mode: C++ *** |
|
346 ;;; End: *** |
|
347 */ |