How do I pass ode45 an odefun that takes two arguments?

后端 未结 2 1444

I have a use case as follows:

Inside F.m I have a function F that takes as its argument a 2 x 1 matrix x. F

相关标签:
2条回答
  • 2021-01-28 20:55

    Firstly, the proper way to handle kmat is to make it an input argument to F.m

    function result = F(x,kmat)
        result = kmat*x;
    end
    

    Secondly, the input function to ode45 must be a function with inputs t and x (possibly vectors, t is the dependent variable and x is the dependent). Since your F function doesn't have t as an input argument, and you have an extra parameter kmat, you have to make a small anonymous function when you call ode45

    ode45(@(t,x) F(x,kmat),[tstart tend],x_0)
    

    If your derivative function was function result=derivative(t,x), then you simply do ode45(@derivative,[tstart tend],x_0) as Erik said.

    0 讨论(0)
  • 2021-01-28 21:05

    I believe F in ode45(F,...) should be a function handle, i.e. @F. Also, you can have a look at this page of the MATLAB documentation for different methods to pass extra parameters to functions.

    0 讨论(0)
提交回复
热议问题