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; |
1
|
54 case 0: |
|
55 info = -1; |
|
56 break; |
|
57 case 1: |
|
58 break; |
|
59 case 2: |
|
60 info = 4; |
|
61 break; |
|
62 case 3: |
|
63 case 4: |
|
64 case 5: |
|
65 info = 3; |
|
66 break; |
|
67 default: |
|
68 panic_impossible (); |
|
69 break; |
|
70 } |
|
71 return info; |
|
72 } |
|
73 |
|
74 ColumnVector |
162
|
75 fsolve_user_function (const ColumnVector& x) |
1
|
76 { |
|
77 ColumnVector retval; |
|
78 |
|
79 int n = x.capacity (); |
|
80 |
516
|
81 // tree_constant name = fsolve_fcn->name (); |
565
|
82 Octave_object args; |
|
83 args.resize (2); |
497
|
84 // args(0) = name; |
1
|
85 |
|
86 if (n > 1) |
|
87 { |
|
88 Matrix m (n, 1); |
|
89 for (int i = 0; i < n; i++) |
|
90 m (i, 0) = x.elem (i); |
|
91 tree_constant vars (m); |
497
|
92 args(1) = vars; |
1
|
93 } |
|
94 else |
|
95 { |
|
96 double d = x.elem (0); |
|
97 tree_constant vars (d); |
497
|
98 args(1) = vars; |
1
|
99 } |
|
100 |
519
|
101 if (fsolve_fcn) |
1
|
102 { |
506
|
103 Octave_object tmp = fsolve_fcn->eval (0, 1, args); |
497
|
104 if (tmp.length () > 0 && tmp(0).is_defined ()) |
1
|
105 { |
628
|
106 retval = tmp(0).vector_value (); |
252
|
107 |
|
108 if (retval.length () <= 0) |
|
109 gripe_user_supplied_eval ("fsolve"); |
1
|
110 } |
|
111 else |
497
|
112 gripe_user_supplied_eval ("fsolve"); |
1
|
113 } |
|
114 |
|
115 return retval; |
|
116 } |
|
117 |
519
|
118 DEFUN_DLD ("fsolve", Ffsolve, Sfsolve, 5, 1, |
|
119 "Solve nonlinear equations using Minpack. Usage:\n\ |
|
120 \n\ |
|
121 [X, INFO] = fsolve (F, X0)\n\ |
|
122 \n\ |
|
123 Where the first argument is the name of the function to call to\n\ |
|
124 compute the vector of function values. It must have the form\n\ |
|
125 \n\ |
|
126 y = f (x) |
|
127 \n\ |
|
128 where y and x are vectors.") |
1
|
129 { |
497
|
130 Octave_object retval; |
1
|
131 |
506
|
132 int nargin = args.length (); |
|
133 |
519
|
134 if (nargin < 3 || nargin > 7 || nargout > 3) |
|
135 { |
|
136 print_usage ("fsolve"); |
|
137 return retval; |
|
138 } |
|
139 |
497
|
140 fsolve_fcn = is_valid_function (args(1), "fsolve", 1); |
519
|
141 if (! fsolve_fcn || takes_correct_nargs (fsolve_fcn, 2, "fsolve", 1) != 1) |
1
|
142 return retval; |
|
143 |
628
|
144 ColumnVector x = args(2).vector_value (); |
1
|
145 |
|
146 if (nargin > 3) |
289
|
147 warning ("fsolve: ignoring extra arguments"); |
1
|
148 |
|
149 if (nargout > 2) |
216
|
150 warning ("fsolve: can't compute path output yet"); |
1
|
151 |
|
152 NLFunc foo_fcn (fsolve_user_function); |
|
153 NLEqn foo (x, foo_fcn); |
289
|
154 foo.copy (fsolve_opts); |
1
|
155 |
|
156 int info; |
|
157 ColumnVector soln = foo.solve (info); |
|
158 |
|
159 info = hybrd_info_to_fsolve_info (info); |
|
160 |
497
|
161 retval.resize (nargout ? nargout : 1); |
516
|
162 retval(0) = soln, 1; |
1
|
163 |
|
164 if (nargout > 1) |
516
|
165 retval(1) = (double) info; |
1
|
166 |
|
167 return retval; |
|
168 } |
|
169 |
289
|
170 typedef void (NLEqn_options::*d_set_opt_mf) (double); |
|
171 typedef double (NLEqn_options::*d_get_opt_mf) (void); |
|
172 |
|
173 #define MAX_TOKENS 1 |
|
174 |
|
175 struct NLEQN_OPTIONS |
|
176 { |
540
|
177 const char *keyword; |
|
178 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
179 int min_len[MAX_TOKENS + 1]; |
|
180 int min_toks_to_match; |
|
181 d_set_opt_mf d_set_fcn; |
|
182 d_get_opt_mf d_get_fcn; |
|
183 }; |
|
184 |
497
|
185 static NLEQN_OPTIONS fsolve_option_table [] = |
289
|
186 { |
|
187 { "tolerance", |
519
|
188 { "tolerance", 0, }, |
289
|
189 { 1, 0, }, 1, |
|
190 NLEqn_options::set_tolerance, |
|
191 NLEqn_options::tolerance, }, |
|
192 |
519
|
193 { 0, |
|
194 { 0, 0, }, |
289
|
195 { 0, 0, }, 0, |
519
|
196 0, 0, }, |
289
|
197 }; |
|
198 |
|
199 static void |
|
200 print_fsolve_option_list (void) |
|
201 { |
|
202 ostrstream output_buf; |
|
203 |
|
204 print_usage ("fsolve_options", 1); |
|
205 |
|
206 output_buf << "\n" |
|
207 << "Options for fsolve include:\n\n" |
|
208 << " keyword value\n" |
|
209 << " ------- -----\n\n"; |
|
210 |
|
211 NLEQN_OPTIONS *list = fsolve_option_table; |
|
212 |
540
|
213 const char *keyword; |
519
|
214 while ((keyword = list->keyword) != 0) |
289
|
215 { |
|
216 output_buf.form (" %-40s ", keyword); |
|
217 |
|
218 double val = (fsolve_opts.*list->d_get_fcn) (); |
|
219 if (val < 0.0) |
|
220 output_buf << "computed automatically"; |
|
221 else |
|
222 output_buf << val; |
|
223 |
|
224 output_buf << "\n"; |
|
225 list++; |
|
226 } |
|
227 |
|
228 output_buf << "\n" << ends; |
|
229 maybe_page_output (output_buf); |
|
230 } |
|
231 |
|
232 static void |
|
233 do_fsolve_option (char *keyword, double val) |
|
234 { |
|
235 NLEQN_OPTIONS *list = fsolve_option_table; |
|
236 |
519
|
237 while (list->keyword != 0) |
289
|
238 { |
|
239 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
240 list->min_toks_to_match, MAX_TOKENS)) |
|
241 { |
|
242 (fsolve_opts.*list->d_set_fcn) (val); |
|
243 |
|
244 return; |
|
245 } |
|
246 list++; |
|
247 } |
|
248 |
|
249 warning ("fsolve_options: no match for `%s'", keyword); |
|
250 } |
|
251 |
519
|
252 DEFUN_DLD ("fsolve_options", Ffsolve_options, Sfsolve_options, -1, 1, |
|
253 "fsolve_options (KEYWORD, VALUE)\n\ |
|
254 \n\ |
|
255 Set or show options for fsolve. Keywords may be abbreviated\n\ |
|
256 to the shortest match.") |
272
|
257 { |
497
|
258 Octave_object retval; |
272
|
259 |
506
|
260 int nargin = args.length (); |
|
261 |
289
|
262 if (nargin == 1) |
519
|
263 { |
|
264 print_fsolve_option_list (); |
|
265 } |
289
|
266 else if (nargin == 3) |
|
267 { |
610
|
268 if (args(1).is_string ()) |
289
|
269 { |
497
|
270 char *keyword = args(1).string_value (); |
|
271 double val = args(2).double_value (); |
289
|
272 do_fsolve_option (keyword, val); |
|
273 } |
|
274 else |
|
275 print_usage ("fsolve_options"); |
|
276 } |
|
277 else |
497
|
278 print_usage ("fsolve_options"); |
289
|
279 |
272
|
280 return retval; |
|
281 } |
|
282 |
1
|
283 /* |
|
284 ;;; Local Variables: *** |
|
285 ;;; mode: C++ *** |
|
286 ;;; page-delimiter: "^/\\*" *** |
|
287 ;;; End: *** |
|
288 */ |