Mercurial > octave-nkf
comparison scripts/ode/private/integrate_const.m @ 20584:eb9e2d187ed2
maint: Use Octave coding conventions in scripts/ode/private dir.
* AbsRel_Norm.m, fuzzy_compare.m, hermite_quartic_interpolation.m,
integrate_adaptive.m, integrate_const.m, integrate_n_steps.m, kahan.m,
ode_struct_value_check.m, odepkg_event_handle.m, odepkg_structure_check.m,
runge_kutta_45_dorpri.m, starting_stepsize.m:
Wrap long lines to < 80 chars.
Use double quotes rather than single quotes where possible.
Use ';' at end of keywords "return;" and "break;"
Use '##" for stand-alone comments and '#' for end-of-line comments.
Use two spaces after period before starting new sentence.
Use '!' instead of '~' for logical negation.
Use specific form of end (endif, endfor, etc.).
Don't use line continuation marker '...' unless necessary.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 04 Oct 2015 22:18:54 -0700 |
parents | 25623ef2ff4f |
children | b7ac1e94266e |
comparison
equal
deleted
inserted
replaced
20583:d746695bf494 | 20584:eb9e2d187ed2 |
---|---|
61 ## | 61 ## |
62 ## @seealso{integrate_adaptive, integrate_n_steps} | 62 ## @seealso{integrate_adaptive, integrate_n_steps} |
63 | 63 |
64 function solution = integrate_const (stepper, func, tspan, x0, dt, options) | 64 function solution = integrate_const (stepper, func, tspan, x0, dt, options) |
65 | 65 |
66 solution = struct; | 66 solution = struct (); |
67 | 67 |
68 ## first values for time and solution | 68 ## first values for time and solution |
69 t = tspan(1); | 69 t = tspan(1); |
70 x = x0(:); | 70 x = x0(:); |
71 | 71 |
72 vdirection = odeget (options, "vdirection", [], "fast"); | 72 vdirection = odeget (options, "vdirection", [], "fast"); |
73 if (sign (dt) != vdirection) | 73 if (sign (dt) != vdirection) |
74 error ("OdePkg:InvalidArgument", | 74 error ("OdePkg:InvalidArgument", |
75 "option ''InitialStep'' has a wrong sign"); | 75 "option 'InitialStep' has a wrong sign"); |
76 endif | 76 endif |
77 | 77 |
78 ## setting parameters | 78 ## setting parameters |
79 k = length (tspan); | 79 k = length (tspan); |
80 counter = 2; | 80 counter = 2; |
187 endif | 187 endif |
188 solution.vcntloop = solution.vcntloop + 1; | 188 solution.vcntloop = solution.vcntloop + 1; |
189 vcntiter = 0; | 189 vcntiter = 0; |
190 | 190 |
191 ## Call plot only if a valid result has been found, therefore this | 191 ## Call plot only if a valid result has been found, therefore this |
192 ## code fragment has moved here. Stop integration if plot function | 192 ## code fragment has moved here. Stop integration if plot function |
193 ## returns false | 193 ## returns false |
194 if (options.vhaveoutputfunction) | 194 if (options.vhaveoutputfunction) |
195 for vcnt = 0:options.Refine # Approximation between told and t | 195 for vcnt = 0:options.Refine # Approximation between told and t |
196 if (options.vhaverefine) # Do interpolation | 196 if (options.vhaverefine) # Do interpolation |
197 vapproxtime = (vcnt + 1) / (options.Refine + 2); | 197 vapproxtime = (vcnt + 1) / (options.Refine + 2); |
198 vapproxvals = (1 - vapproxtime) * vSaveVUForRefine ... | 198 vapproxvals = (1 - vapproxtime) * vSaveVUForRefine ... |
199 + (vapproxtime) * y(:,end); | 199 + (vapproxtime) * y(:,end); |
200 vapproxtime = s(end) + vapproxtime*dt; | 200 vapproxtime = s(end) + vapproxtime*dt; |
201 else | 201 else |
205 if (options.vhaveoutputselection) | 205 if (options.vhaveoutputselection) |
206 vapproxvals = vapproxvals(options.OutputSel); | 206 vapproxvals = vapproxvals(options.OutputSel); |
207 endif | 207 endif |
208 vpltret = feval (options.OutputFcn, vapproxtime, vapproxvals, [], | 208 vpltret = feval (options.OutputFcn, vapproxtime, vapproxvals, [], |
209 options.vfunarguments{:}); | 209 options.vfunarguments{:}); |
210 if (vpltret) # Leave refinement loop | 210 if (vpltret) # Leave refinement loop |
211 break | 211 break; |
212 endif | 212 endif |
213 endfor | 213 endfor |
214 if (vpltret) # Leave main loop | 214 if (vpltret) # Leave main loop |
215 solution.vunhandledtermination = false; | 215 solution.vunhandledtermination = false; |
216 break | 216 break; |
217 endif | 217 endif |
218 endif | 218 endif |
219 | 219 |
220 ## Call event only if a valid result has been found, therefore this | 220 ## Call event only if a valid result has been found, therefore this |
221 ## code fragment has moved here. Stop integration if veventbreak is true | 221 ## code fragment has moved here. Stop integration if veventbreak is true |
222 if (options.vhaveeventfunction) | 222 if (options.vhaveeventfunction) |
223 solution.vevent = odepkg_event_handle (options.Events, t(end), x(:,end), | 223 solution.vevent = odepkg_event_handle (options.Events, t(end), x(:,end), |
224 [], options.vfunarguments{:}); | 224 [], options.vfunarguments{:}); |
225 if (! isempty (solution.vevent{1}) | 225 if (! isempty (solution.vevent{1}) |
226 && solution.vevent{1} == 1) | 226 && solution.vevent{1} == 1) |
227 t(solution.vcntloop-1,:) = solution.vevent{3}(end,:); | 227 t(solution.vcntloop-1,:) = solution.vevent{3}(end,:); |
228 x(:,solution.vcntloop-1) = solution.vevent{4}(end,:)'; | 228 x(:,solution.vcntloop-1) = solution.vevent{4}(end,:)'; |
229 solution.vunhandledtermination = false; | 229 solution.vunhandledtermination = false; |
230 break | 230 break; |
231 endif | 231 endif |
232 endif | 232 endif |
233 | 233 |
234 ## Update counters that count the number of iteration cycles | 234 ## Update counters that count the number of iteration cycles |
235 solution.vcntcycles = solution.vcntcycles + 1; # Needed for cost statistics | 235 solution.vcntcycles = solution.vcntcycles + 1; # Needed for cost statistics |
236 vcntiter = vcntiter + 1; # Needed to find iteration problems | 236 vcntiter = vcntiter + 1; # Needed to find iteration problems |
237 | 237 |
238 ## Stop solving because the last 1000 steps no successful valid | 238 ## Stop solving because the last 1000 steps no successful valid |
239 ## value has been found | 239 ## value has been found |
240 if (vcntiter >= 5000) | 240 if (vcntiter >= 5000) |
241 error (["Solving has not been successful. The iterative", | 241 error (["Solving has not been successful. The iterative", |
242 " integration loop exited at time t = %f before endpoint at", | 242 " integration loop exited at time t = %f before endpoint at", |
243 " tend = %f was reached. This happened because the iterative", | 243 " tend = %f was reached. This happened because the iterative", |
244 " integration loop does not find a valid solution at this time", | 244 " integration loop does not find a valid solution at this time", |
245 " stamp. Try to reduce the value of ''InitialStep'' and/or", | 245 " stamp. Try to reduce the value of 'InitialStep' and/or", |
246 " ''MaxStep'' with the command ''odeset''.\n"], | 246 " 'MaxStep' with the command 'odeset'.\n"], |
247 s(end), tspan(end)); | 247 s(end), tspan(end)); |
248 endif | 248 endif |
249 | 249 |
250 ## if this is the last iteration, save the length of last interval | 250 ## if this is the last iteration, save the length of last interval |
251 if (counter > k) | 251 if (counter > k) |
255 | 255 |
256 ## Check if integration of the ode has been successful | 256 ## Check if integration of the ode has been successful |
257 if (vdirection * z(end) < vdirection * tspan(end)) | 257 if (vdirection * z(end) < vdirection * tspan(end)) |
258 if (solution.vunhandledtermination == true) | 258 if (solution.vunhandledtermination == true) |
259 error ("OdePkg:InvalidArgument", | 259 error ("OdePkg:InvalidArgument", |
260 ["Solving has not been successful. The iterative integration loop", | 260 ["Solving has not been successful. The iterative integration" |
261 " exited at time t = %f before endpoint at tend = %f was", | 261 " loop exited at time t = %f before endpoint at tend = %f was", |
262 " reached. This may happen if the stepsize grows smaller than", | 262 " reached. This may happen if the stepsize grows smaller than", |
263 " defined in vminstepsize. Try to reduce the value of", | 263 " defined in vminstepsize. Try to reduce the value of", |
264 " ''InitialStep'' and/or ''MaxStep'' with the command", | 264 " 'InitialStep' and/or 'MaxStep' with the command 'odeset'.\n"], |
265 " ''odeset''.\n"], z(end), tspan(end)); | 265 z(end), tspan(end)); |
266 else | 266 else |
267 warning ("OdePkg:InvalidArgument", | 267 warning ("OdePkg:InvalidArgument", |
268 ["Solver has been stopped by a call of ''break'' in the main", | 268 ["Solver has been stopped by a call of 'break' in the main", |
269 " iteration loop at time t = %f before endpoint at tend = %f", | 269 " iteration loop at time t = %f before endpoint at tend = %f", |
270 " was reached. This may happen because the @odeplot function", | 270 " was reached. This may happen because the @odeplot function", |
271 " returned ''true'' or the @event function returned", | 271 " returned 'true' or the @event function returned 'true'.\n"], |
272 " ''true''.\n"], | |
273 z(end), tspan(end)); | 272 z(end), tspan(end)); |
274 endif | 273 endif |
275 endif | 274 endif |
276 | 275 |
277 ## compute how many values are out of time inerval | 276 ## compute how many values are out of time inerval |
282 solution.t = t(1:end-f); | 281 solution.t = t(1:end-f); |
283 solution.x = x(:,1:end-f)'; | 282 solution.x = x(:,1:end-f)'; |
284 | 283 |
285 endfunction | 284 endfunction |
286 | 285 |
287 ## Local Variables: *** | |
288 ## mode: octave *** | |
289 ## End: *** |