7
|
1 // f-fsolve.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
277
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
289
|
28 #include <strstream.h> |
|
29 |
1
|
30 #include "NLEqn.h" |
|
31 |
|
32 #include "tree-const.h" |
|
33 #include "variables.h" |
|
34 #include "gripes.h" |
|
35 #include "error.h" |
|
36 #include "utils.h" |
289
|
37 #include "pager.h" |
544
|
38 #include "help.h" |
519
|
39 #include "defun-dld.h" |
1
|
40 |
|
41 // Global pointer for user defined function required by hybrd1. |
488
|
42 static tree_fvc *fsolve_fcn; |
1
|
43 |
289
|
44 static NLEqn_options fsolve_opts; |
|
45 |
1
|
46 int |
|
47 hybrd_info_to_fsolve_info (int info) |
|
48 { |
|
49 switch (info) |
|
50 { |
252
|
51 case -1: |
|
52 info = -2; |
|
53 break; |
636
|
54 |
1
|
55 case 0: |
|
56 info = -1; |
|
57 break; |
636
|
58 |
1
|
59 case 1: |
|
60 break; |
636
|
61 |
1
|
62 case 2: |
|
63 info = 4; |
|
64 break; |
636
|
65 |
1
|
66 case 3: |
|
67 case 4: |
|
68 case 5: |
|
69 info = 3; |
|
70 break; |
636
|
71 |
1
|
72 default: |
|
73 panic_impossible (); |
|
74 break; |
|
75 } |
636
|
76 |
1
|
77 return info; |
|
78 } |
|
79 |
|
80 ColumnVector |
162
|
81 fsolve_user_function (const ColumnVector& x) |
1
|
82 { |
|
83 ColumnVector retval; |
|
84 |
|
85 int n = x.capacity (); |
|
86 |
516
|
87 // tree_constant name = fsolve_fcn->name (); |
565
|
88 Octave_object args; |
|
89 args.resize (2); |
497
|
90 // args(0) = name; |
1
|
91 |
|
92 if (n > 1) |
|
93 { |
|
94 Matrix m (n, 1); |
|
95 for (int i = 0; i < n; i++) |
|
96 m (i, 0) = x.elem (i); |
|
97 tree_constant vars (m); |
497
|
98 args(1) = vars; |
1
|
99 } |
|
100 else |
|
101 { |
|
102 double d = x.elem (0); |
|
103 tree_constant vars (d); |
497
|
104 args(1) = vars; |
1
|
105 } |
|
106 |
519
|
107 if (fsolve_fcn) |
1
|
108 { |
506
|
109 Octave_object tmp = fsolve_fcn->eval (0, 1, args); |
497
|
110 if (tmp.length () > 0 && tmp(0).is_defined ()) |
1
|
111 { |
628
|
112 retval = tmp(0).vector_value (); |
252
|
113 |
636
|
114 if (error_state || retval.length () <= 0) |
252
|
115 gripe_user_supplied_eval ("fsolve"); |
1
|
116 } |
|
117 else |
497
|
118 gripe_user_supplied_eval ("fsolve"); |
1
|
119 } |
|
120 |
|
121 return retval; |
|
122 } |
|
123 |
701
|
124 DEFUN_DLD_BUILTIN ("fsolve", Ffsolve, Sfsolve, 5, 1, |
519
|
125 "Solve nonlinear equations using Minpack. Usage:\n\ |
|
126 \n\ |
|
127 [X, INFO] = fsolve (F, X0)\n\ |
|
128 \n\ |
|
129 Where the first argument is the name of the function to call to\n\ |
|
130 compute the vector of function values. It must have the form\n\ |
|
131 \n\ |
|
132 y = f (x) |
|
133 \n\ |
|
134 where y and x are vectors.") |
1
|
135 { |
497
|
136 Octave_object retval; |
1
|
137 |
506
|
138 int nargin = args.length (); |
|
139 |
519
|
140 if (nargin < 3 || nargin > 7 || nargout > 3) |
|
141 { |
|
142 print_usage ("fsolve"); |
|
143 return retval; |
|
144 } |
|
145 |
497
|
146 fsolve_fcn = is_valid_function (args(1), "fsolve", 1); |
519
|
147 if (! fsolve_fcn || takes_correct_nargs (fsolve_fcn, 2, "fsolve", 1) != 1) |
1
|
148 return retval; |
|
149 |
628
|
150 ColumnVector x = args(2).vector_value (); |
1
|
151 |
636
|
152 if (error_state) |
|
153 { |
|
154 error ("fsolve: expecting vector as second argument"); |
|
155 return retval; |
|
156 } |
|
157 |
1
|
158 if (nargin > 3) |
289
|
159 warning ("fsolve: ignoring extra arguments"); |
1
|
160 |
|
161 if (nargout > 2) |
216
|
162 warning ("fsolve: can't compute path output yet"); |
1
|
163 |
|
164 NLFunc foo_fcn (fsolve_user_function); |
|
165 NLEqn foo (x, foo_fcn); |
289
|
166 foo.copy (fsolve_opts); |
1
|
167 |
|
168 int info; |
|
169 ColumnVector soln = foo.solve (info); |
|
170 |
|
171 info = hybrd_info_to_fsolve_info (info); |
|
172 |
497
|
173 retval.resize (nargout ? nargout : 1); |
516
|
174 retval(0) = soln, 1; |
1
|
175 |
|
176 if (nargout > 1) |
516
|
177 retval(1) = (double) info; |
1
|
178 |
|
179 return retval; |
|
180 } |
|
181 |
289
|
182 typedef void (NLEqn_options::*d_set_opt_mf) (double); |
|
183 typedef double (NLEqn_options::*d_get_opt_mf) (void); |
|
184 |
|
185 #define MAX_TOKENS 1 |
|
186 |
|
187 struct NLEQN_OPTIONS |
|
188 { |
540
|
189 const char *keyword; |
|
190 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
191 int min_len[MAX_TOKENS + 1]; |
|
192 int min_toks_to_match; |
|
193 d_set_opt_mf d_set_fcn; |
|
194 d_get_opt_mf d_get_fcn; |
|
195 }; |
|
196 |
497
|
197 static NLEQN_OPTIONS fsolve_option_table [] = |
289
|
198 { |
|
199 { "tolerance", |
519
|
200 { "tolerance", 0, }, |
289
|
201 { 1, 0, }, 1, |
|
202 NLEqn_options::set_tolerance, |
|
203 NLEqn_options::tolerance, }, |
|
204 |
519
|
205 { 0, |
|
206 { 0, 0, }, |
289
|
207 { 0, 0, }, 0, |
519
|
208 0, 0, }, |
289
|
209 }; |
|
210 |
|
211 static void |
|
212 print_fsolve_option_list (void) |
|
213 { |
|
214 ostrstream output_buf; |
|
215 |
|
216 print_usage ("fsolve_options", 1); |
|
217 |
|
218 output_buf << "\n" |
|
219 << "Options for fsolve include:\n\n" |
|
220 << " keyword value\n" |
|
221 << " ------- -----\n\n"; |
|
222 |
|
223 NLEQN_OPTIONS *list = fsolve_option_table; |
|
224 |
540
|
225 const char *keyword; |
519
|
226 while ((keyword = list->keyword) != 0) |
289
|
227 { |
|
228 output_buf.form (" %-40s ", keyword); |
|
229 |
|
230 double val = (fsolve_opts.*list->d_get_fcn) (); |
|
231 if (val < 0.0) |
|
232 output_buf << "computed automatically"; |
|
233 else |
|
234 output_buf << val; |
|
235 |
|
236 output_buf << "\n"; |
|
237 list++; |
|
238 } |
|
239 |
|
240 output_buf << "\n" << ends; |
|
241 maybe_page_output (output_buf); |
|
242 } |
|
243 |
|
244 static void |
|
245 do_fsolve_option (char *keyword, double val) |
|
246 { |
|
247 NLEQN_OPTIONS *list = fsolve_option_table; |
|
248 |
519
|
249 while (list->keyword != 0) |
289
|
250 { |
|
251 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
252 list->min_toks_to_match, MAX_TOKENS)) |
|
253 { |
|
254 (fsolve_opts.*list->d_set_fcn) (val); |
|
255 |
|
256 return; |
|
257 } |
|
258 list++; |
|
259 } |
|
260 |
|
261 warning ("fsolve_options: no match for `%s'", keyword); |
|
262 } |
|
263 |
701
|
264 DEFUN_DLD_BUILTIN ("fsolve_options", Ffsolve_options, Sfsolve_options, -1, 1, |
519
|
265 "fsolve_options (KEYWORD, VALUE)\n\ |
|
266 \n\ |
|
267 Set or show options for fsolve. Keywords may be abbreviated\n\ |
|
268 to the shortest match.") |
272
|
269 { |
497
|
270 Octave_object retval; |
272
|
271 |
506
|
272 int nargin = args.length (); |
|
273 |
289
|
274 if (nargin == 1) |
519
|
275 { |
|
276 print_fsolve_option_list (); |
636
|
277 return retval; |
519
|
278 } |
289
|
279 else if (nargin == 3) |
|
280 { |
636
|
281 char *keyword = args(1).string_value (); |
|
282 |
|
283 if (! error_state) |
289
|
284 { |
497
|
285 double val = args(2).double_value (); |
636
|
286 |
|
287 if (! error_state) |
|
288 { |
|
289 do_fsolve_option (keyword, val); |
|
290 return retval; |
|
291 } |
289
|
292 } |
|
293 } |
636
|
294 |
|
295 print_usage ("fsolve_options"); |
289
|
296 |
272
|
297 return retval; |
|
298 } |
|
299 |
1
|
300 /* |
|
301 ;;; Local Variables: *** |
|
302 ;;; mode: C++ *** |
|
303 ;;; page-delimiter: "^/\\*" *** |
|
304 ;;; End: *** |
|
305 */ |