1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1728
|
27 #include <string> |
|
28 |
289
|
29 #include <strstream.h> |
|
30 |
1
|
31 #include "NLEqn.h" |
|
32 |
1352
|
33 #include "defun-dld.h" |
1
|
34 #include "error.h" |
1352
|
35 #include "gripes.h" |
|
36 #include "help.h" |
289
|
37 #include "pager.h" |
1740
|
38 #include "oct-obj.h" |
1352
|
39 #include "utils.h" |
|
40 #include "variables.h" |
1
|
41 |
|
42 // Global pointer for user defined function required by hybrd1. |
488
|
43 static tree_fvc *fsolve_fcn; |
1
|
44 |
289
|
45 static NLEqn_options fsolve_opts; |
|
46 |
1
|
47 int |
|
48 hybrd_info_to_fsolve_info (int info) |
|
49 { |
|
50 switch (info) |
|
51 { |
252
|
52 case -1: |
|
53 info = -2; |
|
54 break; |
636
|
55 |
1
|
56 case 0: |
|
57 info = -1; |
|
58 break; |
636
|
59 |
1
|
60 case 1: |
|
61 break; |
636
|
62 |
1
|
63 case 2: |
|
64 info = 4; |
|
65 break; |
636
|
66 |
1
|
67 case 3: |
|
68 case 4: |
|
69 case 5: |
|
70 info = 3; |
|
71 break; |
636
|
72 |
1
|
73 default: |
|
74 panic_impossible (); |
|
75 break; |
|
76 } |
636
|
77 |
1
|
78 return info; |
|
79 } |
|
80 |
|
81 ColumnVector |
162
|
82 fsolve_user_function (const ColumnVector& x) |
1
|
83 { |
|
84 ColumnVector retval; |
|
85 |
|
86 int n = x.capacity (); |
|
87 |
2086
|
88 octave_value_list args; |
712
|
89 args.resize (1); |
1
|
90 |
|
91 if (n > 1) |
|
92 { |
|
93 Matrix m (n, 1); |
|
94 for (int i = 0; i < n; i++) |
|
95 m (i, 0) = x.elem (i); |
2086
|
96 octave_value vars (m); |
712
|
97 args(0) = vars; |
1
|
98 } |
|
99 else |
|
100 { |
|
101 double d = x.elem (0); |
2086
|
102 octave_value vars (d); |
712
|
103 args(0) = vars; |
1
|
104 } |
|
105 |
519
|
106 if (fsolve_fcn) |
1
|
107 { |
2086
|
108 octave_value_list tmp = fsolve_fcn->eval (0, 1, args); |
497
|
109 if (tmp.length () > 0 && tmp(0).is_defined ()) |
1
|
110 { |
628
|
111 retval = tmp(0).vector_value (); |
252
|
112 |
636
|
113 if (error_state || retval.length () <= 0) |
252
|
114 gripe_user_supplied_eval ("fsolve"); |
1
|
115 } |
|
116 else |
497
|
117 gripe_user_supplied_eval ("fsolve"); |
1
|
118 } |
|
119 |
|
120 return retval; |
|
121 } |
|
122 |
1957
|
123 DEFUN_DLD_BUILTIN (fsolve, args, nargout, |
519
|
124 "Solve nonlinear equations using Minpack. Usage:\n\ |
|
125 \n\ |
|
126 [X, INFO] = fsolve (F, X0)\n\ |
|
127 \n\ |
|
128 Where the first argument is the name of the function to call to\n\ |
|
129 compute the vector of function values. It must have the form\n\ |
|
130 \n\ |
|
131 y = f (x) |
|
132 \n\ |
|
133 where y and x are vectors.") |
1
|
134 { |
2086
|
135 octave_value_list retval; |
1
|
136 |
506
|
137 int nargin = args.length (); |
|
138 |
718
|
139 if (nargin != 2 || nargout > 3) |
519
|
140 { |
|
141 print_usage ("fsolve"); |
|
142 return retval; |
|
143 } |
|
144 |
712
|
145 fsolve_fcn = is_valid_function (args(0), "fsolve", 1); |
1488
|
146 if (! fsolve_fcn) |
1
|
147 return retval; |
|
148 |
712
|
149 ColumnVector x = args(1).vector_value (); |
1
|
150 |
636
|
151 if (error_state) |
|
152 { |
|
153 error ("fsolve: expecting vector as second argument"); |
|
154 return retval; |
|
155 } |
|
156 |
712
|
157 if (nargin > 2) |
289
|
158 warning ("fsolve: ignoring extra arguments"); |
1
|
159 |
|
160 if (nargout > 2) |
216
|
161 warning ("fsolve: can't compute path output yet"); |
1
|
162 |
|
163 NLFunc foo_fcn (fsolve_user_function); |
|
164 NLEqn foo (x, foo_fcn); |
1872
|
165 foo.set_options (fsolve_opts); |
1
|
166 |
|
167 int info; |
|
168 ColumnVector soln = foo.solve (info); |
|
169 |
|
170 info = hybrd_info_to_fsolve_info (info); |
|
171 |
497
|
172 retval.resize (nargout ? nargout : 1); |
516
|
173 retval(0) = soln, 1; |
1
|
174 |
|
175 if (nargout > 1) |
516
|
176 retval(1) = (double) info; |
1
|
177 |
|
178 return retval; |
|
179 } |
|
180 |
289
|
181 typedef void (NLEqn_options::*d_set_opt_mf) (double); |
|
182 typedef double (NLEqn_options::*d_get_opt_mf) (void); |
|
183 |
|
184 #define MAX_TOKENS 1 |
|
185 |
|
186 struct NLEQN_OPTIONS |
|
187 { |
540
|
188 const char *keyword; |
|
189 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
190 int min_len[MAX_TOKENS + 1]; |
|
191 int min_toks_to_match; |
|
192 d_set_opt_mf d_set_fcn; |
|
193 d_get_opt_mf d_get_fcn; |
|
194 }; |
|
195 |
497
|
196 static NLEQN_OPTIONS fsolve_option_table [] = |
289
|
197 { |
|
198 { "tolerance", |
519
|
199 { "tolerance", 0, }, |
289
|
200 { 1, 0, }, 1, |
|
201 NLEqn_options::set_tolerance, |
|
202 NLEqn_options::tolerance, }, |
|
203 |
519
|
204 { 0, |
|
205 { 0, 0, }, |
289
|
206 { 0, 0, }, 0, |
519
|
207 0, 0, }, |
289
|
208 }; |
|
209 |
|
210 static void |
|
211 print_fsolve_option_list (void) |
|
212 { |
|
213 ostrstream output_buf; |
|
214 |
|
215 print_usage ("fsolve_options", 1); |
|
216 |
|
217 output_buf << "\n" |
|
218 << "Options for fsolve include:\n\n" |
|
219 << " keyword value\n" |
|
220 << " ------- -----\n\n"; |
|
221 |
|
222 NLEQN_OPTIONS *list = fsolve_option_table; |
|
223 |
540
|
224 const char *keyword; |
519
|
225 while ((keyword = list->keyword) != 0) |
289
|
226 { |
|
227 output_buf.form (" %-40s ", keyword); |
|
228 |
|
229 double val = (fsolve_opts.*list->d_get_fcn) (); |
|
230 if (val < 0.0) |
|
231 output_buf << "computed automatically"; |
|
232 else |
|
233 output_buf << val; |
|
234 |
|
235 output_buf << "\n"; |
|
236 list++; |
|
237 } |
|
238 |
|
239 output_buf << "\n" << ends; |
|
240 maybe_page_output (output_buf); |
|
241 } |
|
242 |
|
243 static void |
1755
|
244 set_fsolve_option (const string& keyword, double val) |
289
|
245 { |
|
246 NLEQN_OPTIONS *list = fsolve_option_table; |
|
247 |
519
|
248 while (list->keyword != 0) |
289
|
249 { |
|
250 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
251 list->min_toks_to_match, MAX_TOKENS)) |
|
252 { |
|
253 (fsolve_opts.*list->d_set_fcn) (val); |
|
254 |
|
255 return; |
|
256 } |
|
257 list++; |
|
258 } |
|
259 |
1755
|
260 warning ("fsolve_options: no match for `%s'", keyword.c_str ()); |
289
|
261 } |
|
262 |
2086
|
263 static octave_value_list |
1755
|
264 show_fsolve_option (const string& keyword) |
1329
|
265 { |
2086
|
266 octave_value_list retval; |
1329
|
267 |
|
268 NLEQN_OPTIONS *list = fsolve_option_table; |
|
269 |
|
270 while (list->keyword != 0) |
|
271 { |
|
272 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
273 list->min_toks_to_match, MAX_TOKENS)) |
|
274 { |
|
275 return (fsolve_opts.*list->d_get_fcn) (); |
|
276 } |
|
277 list++; |
|
278 } |
|
279 |
1755
|
280 warning ("fsolve_options: no match for `%s'", keyword.c_str ()); |
1329
|
281 |
|
282 return retval; |
|
283 } |
|
284 |
1986
|
285 DEFUN_DLD_BUILTIN (fsolve_options, args, , |
519
|
286 "fsolve_options (KEYWORD, VALUE)\n\ |
|
287 \n\ |
|
288 Set or show options for fsolve. Keywords may be abbreviated\n\ |
|
289 to the shortest match.") |
272
|
290 { |
2086
|
291 octave_value_list retval; |
272
|
292 |
506
|
293 int nargin = args.length (); |
|
294 |
712
|
295 if (nargin == 0) |
519
|
296 { |
|
297 print_fsolve_option_list (); |
636
|
298 return retval; |
519
|
299 } |
1329
|
300 else if (nargin == 1 || nargin == 2) |
289
|
301 { |
1755
|
302 string keyword = args(0).string_value (); |
636
|
303 |
|
304 if (! error_state) |
289
|
305 { |
1329
|
306 if (nargin == 1) |
|
307 return show_fsolve_option (keyword); |
|
308 else |
|
309 { |
|
310 double val = args(1).double_value (); |
636
|
311 |
1329
|
312 if (! error_state) |
|
313 { |
|
314 set_fsolve_option (keyword, val); |
|
315 return retval; |
|
316 } |
636
|
317 } |
289
|
318 } |
|
319 } |
636
|
320 |
|
321 print_usage ("fsolve_options"); |
289
|
322 |
272
|
323 return retval; |
|
324 } |
|
325 |
1
|
326 /* |
|
327 ;;; Local Variables: *** |
|
328 ;;; mode: C++ *** |
|
329 ;;; End: *** |
|
330 */ |