Passing additional iteration-dependent inputs to ode45

你说的曾经没有我的故事 提交于 2019-12-06 15:47:20

You may have noticed that the official docs are not too helpful in this scenario (as they pretty much force you to use global variables - which is doable, but discouraged). Instead, I'll show you how this can be done with classes and function handles. Consider the following:

classdef SimpleQueue < handle
  %SIMPLEQUEUE A simple FIFO data structure.

  properties (Access = private)
    data
    position
  end

  methods (Access = public)
    function obj = SimpleQueue(inputData)
      %SIMPLEQUEUE Construct an instance of this class
      obj.data = inputData;
      rewind(obj);
    end % constructor

    function out = pop(obj, howMany)
      %POP return the next howMany elements.
      if nargin < 2 
        howMany = 1; % default amount of values to return
      end
      finalPosition = obj.position + howMany;
      if finalPosition > numel(obj.data)
        error('Too many elements requested!');
      end      
      out = obj.data(obj.position + 1 : obj.position + howMany);
      obj.position = finalPosition;      
    end % pop

    function [] = rewind(obj)
      %REWIND restarts the element tracking
      % Subsequent calls to pop() shall return elements from the beginning.
      obj.position = 0;
    end % rewind
  end % methods  
end % classdef

How to use this? Simple:

C1q = SimpleQueue(C1);
C2q = SimpleQueue(C2);
C3q = SimpleQueue(C3);
C4q = SimpleQueue(C4);

[t1,X2] = ode45(@(t,x)fun(t,x,@C1.pop,@C2.pop,@C3.pop,@C4.pop),t0,X01);

Then, inside fun you would use C1() instead of C1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!