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