Timestep change in matlab ballode example












0














In this 'ballode' example of Matlab, which simulates a bouncing ball. Can I please know what the use of this part of the code is?



  % A good guess of a valid first timestep is the length of the last valid
% timestep, so use it for faster computation. 'refine' is 4 by default.
options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
'MaxStep',t(nt)-t(1));


particularly, what is the use of t(nt)-t(nt-refine)? How to select the value of refine?



The code works even without this options line, so what is its use?



This is the full code,



function ballode 
%BALLODE Run a demo of a bouncing ball.
% This is an example of repeated event location, where the initial
% conditions are changed after each terminal event. This demo computes ten
% bounces with calls to ODE23. The speed of the ball is attenuated by 0.9
% after each bounce. The trajectory is plotted using the output function
% ODEPLOT. % % See also ODE23, ODE45, ODESET, ODEPLOT, FUNCTION_HANDLE.
tstart = 0;
tfinal = 30;
y0 = [0; 20];
refine = 4;
options = odeset('Events',@events,'OutputFcn',@odeplot,'OutputSel',1,...
'Refine',refine);
figure;
set(gca,'xlim',[0 30],'ylim',[0 25]);
box on
hold on;
tout = tstart;
yout = y0.';
teout = ;
yeout = ;
ieout = ;
for i = 1:10
% Solve until the first terminal event.
[t,y,te,ye,ie] = ode23(@f,[tstart tfinal],y0,options);
if ~ishold
hold on
end
% Accumulate output. This could be passed out as output arguments.
nt = length(t);
tout = [tout; t(2:nt)];
yout = [yout; y(2:nt,:)];
teout = [teout; te]; % Events at tstart are never reported.
yeout = [yeout; ye];
ieout = [ieout; ie];
ud = get(gcf,'UserData');
if ud.stop
break;
end
% Set the new initial conditions, with .9 attenuation.
y0(1) = 0;
y0(2) = -.9*y(nt,2);
% A good guess of a valid first timestep is the length of the last valid
% timestep, so use it for faster computation. 'refine' is 4 by default.
options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
'MaxStep',t(nt)-t(1));
tstart = t(nt);
end
plot(teout,yeout(:,1),'ro')
xlabel('time');
ylabel('height');
title('Ball trajectory and the events');
hold off
odeplot(,,'done');
% --------------------------------------------------------------------------
function dydt = f(t,y)
dydt = [y(2); -9.8];
% --------------------------------------------------------------------------
function [value,isterminal,direction] = events(t,y)
% Locate the time when height passes through zero in a decreasing direction
% and stop integration.
value = y(1); % detect height = 0
isterminal = 1; % stop the integration
direction = -1; % negative direction









share|cite|improve this question



























    0














    In this 'ballode' example of Matlab, which simulates a bouncing ball. Can I please know what the use of this part of the code is?



      % A good guess of a valid first timestep is the length of the last valid
    % timestep, so use it for faster computation. 'refine' is 4 by default.
    options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
    'MaxStep',t(nt)-t(1));


    particularly, what is the use of t(nt)-t(nt-refine)? How to select the value of refine?



    The code works even without this options line, so what is its use?



    This is the full code,



    function ballode 
    %BALLODE Run a demo of a bouncing ball.
    % This is an example of repeated event location, where the initial
    % conditions are changed after each terminal event. This demo computes ten
    % bounces with calls to ODE23. The speed of the ball is attenuated by 0.9
    % after each bounce. The trajectory is plotted using the output function
    % ODEPLOT. % % See also ODE23, ODE45, ODESET, ODEPLOT, FUNCTION_HANDLE.
    tstart = 0;
    tfinal = 30;
    y0 = [0; 20];
    refine = 4;
    options = odeset('Events',@events,'OutputFcn',@odeplot,'OutputSel',1,...
    'Refine',refine);
    figure;
    set(gca,'xlim',[0 30],'ylim',[0 25]);
    box on
    hold on;
    tout = tstart;
    yout = y0.';
    teout = ;
    yeout = ;
    ieout = ;
    for i = 1:10
    % Solve until the first terminal event.
    [t,y,te,ye,ie] = ode23(@f,[tstart tfinal],y0,options);
    if ~ishold
    hold on
    end
    % Accumulate output. This could be passed out as output arguments.
    nt = length(t);
    tout = [tout; t(2:nt)];
    yout = [yout; y(2:nt,:)];
    teout = [teout; te]; % Events at tstart are never reported.
    yeout = [yeout; ye];
    ieout = [ieout; ie];
    ud = get(gcf,'UserData');
    if ud.stop
    break;
    end
    % Set the new initial conditions, with .9 attenuation.
    y0(1) = 0;
    y0(2) = -.9*y(nt,2);
    % A good guess of a valid first timestep is the length of the last valid
    % timestep, so use it for faster computation. 'refine' is 4 by default.
    options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
    'MaxStep',t(nt)-t(1));
    tstart = t(nt);
    end
    plot(teout,yeout(:,1),'ro')
    xlabel('time');
    ylabel('height');
    title('Ball trajectory and the events');
    hold off
    odeplot(,,'done');
    % --------------------------------------------------------------------------
    function dydt = f(t,y)
    dydt = [y(2); -9.8];
    % --------------------------------------------------------------------------
    function [value,isterminal,direction] = events(t,y)
    % Locate the time when height passes through zero in a decreasing direction
    % and stop integration.
    value = y(1); % detect height = 0
    isterminal = 1; % stop the integration
    direction = -1; % negative direction









    share|cite|improve this question

























      0












      0








      0







      In this 'ballode' example of Matlab, which simulates a bouncing ball. Can I please know what the use of this part of the code is?



        % A good guess of a valid first timestep is the length of the last valid
      % timestep, so use it for faster computation. 'refine' is 4 by default.
      options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
      'MaxStep',t(nt)-t(1));


      particularly, what is the use of t(nt)-t(nt-refine)? How to select the value of refine?



      The code works even without this options line, so what is its use?



      This is the full code,



      function ballode 
      %BALLODE Run a demo of a bouncing ball.
      % This is an example of repeated event location, where the initial
      % conditions are changed after each terminal event. This demo computes ten
      % bounces with calls to ODE23. The speed of the ball is attenuated by 0.9
      % after each bounce. The trajectory is plotted using the output function
      % ODEPLOT. % % See also ODE23, ODE45, ODESET, ODEPLOT, FUNCTION_HANDLE.
      tstart = 0;
      tfinal = 30;
      y0 = [0; 20];
      refine = 4;
      options = odeset('Events',@events,'OutputFcn',@odeplot,'OutputSel',1,...
      'Refine',refine);
      figure;
      set(gca,'xlim',[0 30],'ylim',[0 25]);
      box on
      hold on;
      tout = tstart;
      yout = y0.';
      teout = ;
      yeout = ;
      ieout = ;
      for i = 1:10
      % Solve until the first terminal event.
      [t,y,te,ye,ie] = ode23(@f,[tstart tfinal],y0,options);
      if ~ishold
      hold on
      end
      % Accumulate output. This could be passed out as output arguments.
      nt = length(t);
      tout = [tout; t(2:nt)];
      yout = [yout; y(2:nt,:)];
      teout = [teout; te]; % Events at tstart are never reported.
      yeout = [yeout; ye];
      ieout = [ieout; ie];
      ud = get(gcf,'UserData');
      if ud.stop
      break;
      end
      % Set the new initial conditions, with .9 attenuation.
      y0(1) = 0;
      y0(2) = -.9*y(nt,2);
      % A good guess of a valid first timestep is the length of the last valid
      % timestep, so use it for faster computation. 'refine' is 4 by default.
      options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
      'MaxStep',t(nt)-t(1));
      tstart = t(nt);
      end
      plot(teout,yeout(:,1),'ro')
      xlabel('time');
      ylabel('height');
      title('Ball trajectory and the events');
      hold off
      odeplot(,,'done');
      % --------------------------------------------------------------------------
      function dydt = f(t,y)
      dydt = [y(2); -9.8];
      % --------------------------------------------------------------------------
      function [value,isterminal,direction] = events(t,y)
      % Locate the time when height passes through zero in a decreasing direction
      % and stop integration.
      value = y(1); % detect height = 0
      isterminal = 1; % stop the integration
      direction = -1; % negative direction









      share|cite|improve this question













      In this 'ballode' example of Matlab, which simulates a bouncing ball. Can I please know what the use of this part of the code is?



        % A good guess of a valid first timestep is the length of the last valid
      % timestep, so use it for faster computation. 'refine' is 4 by default.
      options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
      'MaxStep',t(nt)-t(1));


      particularly, what is the use of t(nt)-t(nt-refine)? How to select the value of refine?



      The code works even without this options line, so what is its use?



      This is the full code,



      function ballode 
      %BALLODE Run a demo of a bouncing ball.
      % This is an example of repeated event location, where the initial
      % conditions are changed after each terminal event. This demo computes ten
      % bounces with calls to ODE23. The speed of the ball is attenuated by 0.9
      % after each bounce. The trajectory is plotted using the output function
      % ODEPLOT. % % See also ODE23, ODE45, ODESET, ODEPLOT, FUNCTION_HANDLE.
      tstart = 0;
      tfinal = 30;
      y0 = [0; 20];
      refine = 4;
      options = odeset('Events',@events,'OutputFcn',@odeplot,'OutputSel',1,...
      'Refine',refine);
      figure;
      set(gca,'xlim',[0 30],'ylim',[0 25]);
      box on
      hold on;
      tout = tstart;
      yout = y0.';
      teout = ;
      yeout = ;
      ieout = ;
      for i = 1:10
      % Solve until the first terminal event.
      [t,y,te,ye,ie] = ode23(@f,[tstart tfinal],y0,options);
      if ~ishold
      hold on
      end
      % Accumulate output. This could be passed out as output arguments.
      nt = length(t);
      tout = [tout; t(2:nt)];
      yout = [yout; y(2:nt,:)];
      teout = [teout; te]; % Events at tstart are never reported.
      yeout = [yeout; ye];
      ieout = [ieout; ie];
      ud = get(gcf,'UserData');
      if ud.stop
      break;
      end
      % Set the new initial conditions, with .9 attenuation.
      y0(1) = 0;
      y0(2) = -.9*y(nt,2);
      % A good guess of a valid first timestep is the length of the last valid
      % timestep, so use it for faster computation. 'refine' is 4 by default.
      options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
      'MaxStep',t(nt)-t(1));
      tstart = t(nt);
      end
      plot(teout,yeout(:,1),'ro')
      xlabel('time');
      ylabel('height');
      title('Ball trajectory and the events');
      hold off
      odeplot(,,'done');
      % --------------------------------------------------------------------------
      function dydt = f(t,y)
      dydt = [y(2); -9.8];
      % --------------------------------------------------------------------------
      function [value,isterminal,direction] = events(t,y)
      % Locate the time when height passes through zero in a decreasing direction
      % and stop integration.
      value = y(1); % detect height = 0
      isterminal = 1; % stop the integration
      direction = -1; % negative direction






      differential-equations matlab






      share|cite|improve this question













      share|cite|improve this question











      share|cite|improve this question




      share|cite|improve this question










      asked Nov 21 '18 at 9:49









      sam_rox

      4872920




      4872920






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "69"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3007494%2ftimestep-change-in-matlab-ballode-example%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematics Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3007494%2ftimestep-change-in-matlab-ballode-example%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules