1

ODE45 を使用していくつかの方程式を解くクラス関数があります。ODE45 が解決する必要がある odefunction を表す別のプライベート クラス関数があります。しかし、クラスの ode 関数のハンドルを ODE45 に渡す方法がわかりません。サンプルコードは次のとおりです。

class ODESolver < handle

    methods (Access = public)

        function obj = RunODE(obj, t, y0)
            [~, Z] = ode45(@ODEFunction, t, y0);
        end

    end

    methods (Access = private)

        function dy = ODEFunction(t,y)
            % Calculate dy here.
        end

    end

end

これを実行すると、次のようなエラーが表示されます。

Undefined function 'ODEFunction' for input arguments of type 'double'.

ODEFunction をクラスの外に移動して独自の *.m ファイルに配置すると、コードは問題なく実行されます。また、ode45 呼び出しで "@obj.ODEFunction" を使用してみましたが、次のように表示されます。

Too many input arguments.

ODEFunction をクラス内に保持し、そのハンドルを ode45 に渡すことができる最善の方法は何ですか?

4

1 に答える 1

2

privateODEFunctionは静的メソッドではないため、次のように記述する必要があります。

classdef ODESolver < handle

    methods (Access = public)

        function obj = RunODE(obj, t, y0)
            [~, Z] = ode45(@(tt, yy)obj.ODEFunction(tt, yy), t, y0);
        end

    end

    methods (Access = private)

        function dy = ODEFunction(obj, t,y)
            dy = 0.1; % Calculate dy here.
        end

    end

end

objNB: また、プライベートの最初の引数として渡すのを忘れていましたODEFunction...静的メソッドを使用して例を書いており、テストしたらここに貼り付けます。

編集

ODEFunctionクラスにprivate static を含める場合は、次のように記述します。

classdef ODESolver < handle

    methods (Access = public)

        function obj = RunODE(obj, t, y0)
            [~, Z] = ode45(@(tt, yy)ODESolver.ODEFunction(tt, yy), t, y0);
        end

    end

    methods (Static, Access = private)

        function dy = ODEFunction(t,y)
            dy = 0.1; % Calculate dy here.
        end

    end

end
于 2015-02-13T17:53:39.173 に答える