comparison scripts/ode/ode23s.m @ 31495:4cb81dcc5cc5 stable

Add BIST for OutputSel option in ode23, ode23s, ode45 (bug #63229) * ode23.m, ode23s.m, ode45.m: Add a BIST test function to be called within odeset to ensure the odeXX function call passes the correct elements to the output monitoring function. Add BISTs that call test function in scalar and vector forms.
author Ken Marek <marek_ka@mercer.edu>
date Wed, 16 Nov 2022 23:07:58 -0500
parents 7286327ec4b6
children e7b51b5b33f8
comparison
equal deleted inserted replaced
31493:c05ef94a2bbc 31495:4cb81dcc5cc5
396 %! warning ('fout: step "done"'); 396 %! warning ('fout: step "done"');
397 %! endif 397 %! endif
398 %! else 398 %! else
399 %! error ("fout: invalid flag <%s>", flag); 399 %! error ("fout: invalid flag <%s>", flag);
400 %! endif 400 %! endif
401 %!endfunction
402 %!function stop_solve = OutputSel_test (t, y, flag, x)
403 %! ## x == 1: select y(1)
404 %! ## x == 2: select y(2)
405 %! ## x == 3: select y([1,2])
406 %! persistent y_last
407 %! if strcmp (flag, "init")
408 %! y_last = y;
409 %! if ((x == 1) || (x == 2))
410 %! assert (length (y) == 1);
411 %! elseif (x == 3)
412 %! assert (length (y) == 2);
413 %! endif
414 %! elseif strcmp (flag, "done")
415 %! y_exp = fref ().';
416 %! if (x < 3)
417 %! assert (y_last, y_exp(x), 1e-4);
418 %! else
419 %! assert (y_last, y_exp, 1e-4);
420 %! endif
421 %! else # flag == ""
422 %! y_last = y(:,end);
423 %! if ((x == 1) || (x == 2))
424 %! assert (length (t) == length (y));
425 %! else
426 %! assert (2 * length (t) == length (y(:)));
427 %! endif
428 %! endif
429 %! stop_solve = 0;
401 %!endfunction 430 %!endfunction
402 %! 431 %!
403 %!test # two output arguments 432 %!test # two output arguments
404 %! [t, y] = ode23s (@fpol, [0 2], [2 0]); 433 %! [t, y] = ode23s (@fpol, [0 2], [2 0]);
405 %! assert ([t(end), y(end,:)], [2, fref], 1e-3); 434 %! assert ([t(end), y(end,:)], [2, fref], 1e-3);
440 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); 469 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3);
441 %!test # RelTol and NormControl option -- higher accuracy 470 %!test # RelTol and NormControl option -- higher accuracy
442 %! opt = odeset ("RelTol", 1e-8, "NormControl", "on"); 471 %! opt = odeset ("RelTol", 1e-8, "NormControl", "on");
443 %! sol = ode23s (@fpol, [0 2], [2 0], opt); 472 %! sol = ode23s (@fpol, [0 2], [2 0], opt);
444 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-4); 473 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-4);
445 %!test # Details of OutputSel can't be tested 474 %!test # OutputSel 1 (see function OutputSel_test for asserts)
446 %! opt = odeset ("OutputFcn", @fout, "OutputSel", 1); 475 %! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 1), ...
476 %! "OutputSel", 1);
477 %! sol = ode23s (@fpol, [0 2], [2 0], opt);
478 %!test # OutputSel 2 (see function OutputSel_test for asserts)
479 %! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 2), ...
480 %! "OutputSel", 2);
481 %! sol = ode23s (@fpol, [0 2], [2 0], opt);
482 %!test # OutputSel [1,2] (see function OutputSel_test for asserts)
483 %! opt = odeset ("OutputFcn", @(t, y, flag) OutputSel_test (t, y, flag, 3), ...
484 %! "OutputSel", [1, 2]);
447 %! sol = ode23s (@fpol, [0 2], [2 0], opt); 485 %! sol = ode23s (@fpol, [0 2], [2 0], opt);
448 %!test # Stats must add further elements in sol 486 %!test # Stats must add further elements in sol
449 %! opt = odeset ("Stats", "on"); 487 %! opt = odeset ("Stats", "on");
450 %! stat_str = evalc ("sol = ode23s (@fpol, [0 2], [2 0], opt);"); 488 %! stat_str = evalc ("sol = ode23s (@fpol, [0 2], [2 0], opt);");
451 %! assert (strncmp (stat_str, "Number of successful steps:", 27)); 489 %! assert (strncmp (stat_str, "Number of successful steps:", 27));