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 |
2095
|
29 #include <iostream.h> |
289
|
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" |
2367
|
38 #include "pt-fvc.h" |
1740
|
39 #include "oct-obj.h" |
1352
|
40 #include "utils.h" |
|
41 #include "variables.h" |
1
|
42 |
|
43 // Global pointer for user defined function required by hybrd1. |
488
|
44 static tree_fvc *fsolve_fcn; |
1
|
45 |
289
|
46 static NLEqn_options fsolve_opts; |
|
47 |
1
|
48 int |
|
49 hybrd_info_to_fsolve_info (int info) |
|
50 { |
|
51 switch (info) |
|
52 { |
252
|
53 case -1: |
|
54 info = -2; |
|
55 break; |
636
|
56 |
1
|
57 case 0: |
|
58 info = -1; |
|
59 break; |
636
|
60 |
1
|
61 case 1: |
|
62 break; |
636
|
63 |
1
|
64 case 2: |
|
65 info = 4; |
|
66 break; |
636
|
67 |
1
|
68 case 3: |
|
69 case 4: |
|
70 case 5: |
|
71 info = 3; |
|
72 break; |
636
|
73 |
1
|
74 default: |
|
75 panic_impossible (); |
|
76 break; |
|
77 } |
636
|
78 |
1
|
79 return info; |
|
80 } |
|
81 |
|
82 ColumnVector |
162
|
83 fsolve_user_function (const ColumnVector& x) |
1
|
84 { |
|
85 ColumnVector retval; |
|
86 |
|
87 int n = x.capacity (); |
|
88 |
2086
|
89 octave_value_list args; |
712
|
90 args.resize (1); |
1
|
91 |
|
92 if (n > 1) |
|
93 { |
|
94 Matrix m (n, 1); |
|
95 for (int i = 0; i < n; i++) |
2305
|
96 m (i, 0) = x (i); |
2086
|
97 octave_value vars (m); |
712
|
98 args(0) = vars; |
1
|
99 } |
|
100 else |
|
101 { |
2305
|
102 double d = x (0); |
2086
|
103 octave_value vars (d); |
712
|
104 args(0) = vars; |
1
|
105 } |
|
106 |
519
|
107 if (fsolve_fcn) |
1
|
108 { |
2086
|
109 octave_value_list 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 |
2465
|
124 DEFUN_DLD (fsolve, args, nargout, |
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 { |
2086
|
136 octave_value_list retval; |
1
|
137 |
506
|
138 int nargin = args.length (); |
|
139 |
718
|
140 if (nargin != 2 || nargout > 3) |
519
|
141 { |
|
142 print_usage ("fsolve"); |
|
143 return retval; |
|
144 } |
|
145 |
712
|
146 fsolve_fcn = is_valid_function (args(0), "fsolve", 1); |
1488
|
147 if (! fsolve_fcn) |
1
|
148 return retval; |
|
149 |
712
|
150 ColumnVector x = args(1).vector_value (); |
1
|
151 |
636
|
152 if (error_state) |
|
153 { |
|
154 error ("fsolve: expecting vector as second argument"); |
|
155 return retval; |
|
156 } |
|
157 |
712
|
158 if (nargin > 2) |
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); |
1872
|
166 foo.set_options (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 |
2095
|
212 print_fsolve_option_list (ostream& os) |
289
|
213 { |
|
214 print_usage ("fsolve_options", 1); |
|
215 |
2095
|
216 os << "\n" |
|
217 << "Options for fsolve include:\n\n" |
|
218 << " keyword value\n" |
|
219 << " ------- -----\n\n"; |
289
|
220 |
|
221 NLEQN_OPTIONS *list = fsolve_option_table; |
|
222 |
540
|
223 const char *keyword; |
519
|
224 while ((keyword = list->keyword) != 0) |
289
|
225 { |
2095
|
226 os.form (" %-40s ", keyword); |
289
|
227 |
|
228 double val = (fsolve_opts.*list->d_get_fcn) (); |
|
229 if (val < 0.0) |
2095
|
230 os << "computed automatically"; |
289
|
231 else |
2095
|
232 os << val; |
289
|
233 |
2095
|
234 os << "\n"; |
289
|
235 list++; |
|
236 } |
|
237 |
2095
|
238 os << "\n"; |
289
|
239 } |
|
240 |
|
241 static void |
1755
|
242 set_fsolve_option (const string& keyword, double val) |
289
|
243 { |
|
244 NLEQN_OPTIONS *list = fsolve_option_table; |
|
245 |
519
|
246 while (list->keyword != 0) |
289
|
247 { |
|
248 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
249 list->min_toks_to_match, MAX_TOKENS)) |
|
250 { |
|
251 (fsolve_opts.*list->d_set_fcn) (val); |
|
252 |
|
253 return; |
|
254 } |
|
255 list++; |
|
256 } |
|
257 |
1755
|
258 warning ("fsolve_options: no match for `%s'", keyword.c_str ()); |
289
|
259 } |
|
260 |
2086
|
261 static octave_value_list |
1755
|
262 show_fsolve_option (const string& keyword) |
1329
|
263 { |
2086
|
264 octave_value_list retval; |
1329
|
265 |
|
266 NLEQN_OPTIONS *list = fsolve_option_table; |
|
267 |
|
268 while (list->keyword != 0) |
|
269 { |
|
270 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
271 list->min_toks_to_match, MAX_TOKENS)) |
|
272 { |
|
273 return (fsolve_opts.*list->d_get_fcn) (); |
|
274 } |
|
275 list++; |
|
276 } |
|
277 |
1755
|
278 warning ("fsolve_options: no match for `%s'", keyword.c_str ()); |
1329
|
279 |
|
280 return retval; |
|
281 } |
|
282 |
2465
|
283 DEFUN_DLD (fsolve_options, args, , |
519
|
284 "fsolve_options (KEYWORD, VALUE)\n\ |
|
285 \n\ |
|
286 Set or show options for fsolve. Keywords may be abbreviated\n\ |
|
287 to the shortest match.") |
272
|
288 { |
2086
|
289 octave_value_list retval; |
272
|
290 |
506
|
291 int nargin = args.length (); |
|
292 |
712
|
293 if (nargin == 0) |
519
|
294 { |
2095
|
295 print_fsolve_option_list (octave_stdout); |
636
|
296 return retval; |
519
|
297 } |
1329
|
298 else if (nargin == 1 || nargin == 2) |
289
|
299 { |
1755
|
300 string keyword = args(0).string_value (); |
636
|
301 |
|
302 if (! error_state) |
289
|
303 { |
1329
|
304 if (nargin == 1) |
|
305 return show_fsolve_option (keyword); |
|
306 else |
|
307 { |
|
308 double val = args(1).double_value (); |
636
|
309 |
1329
|
310 if (! error_state) |
|
311 { |
|
312 set_fsolve_option (keyword, val); |
|
313 return retval; |
|
314 } |
636
|
315 } |
289
|
316 } |
|
317 } |
636
|
318 |
|
319 print_usage ("fsolve_options"); |
289
|
320 |
272
|
321 return retval; |
|
322 } |
|
323 |
1
|
324 /* |
|
325 ;;; Local Variables: *** |
|
326 ;;; mode: C++ *** |
|
327 ;;; End: *** |
|
328 */ |