How can I solve an ODE without using nested functions?

前端 未结 2 770
借酒劲吻你
借酒劲吻你 2021-01-21 07:06

I have some differential equations that I need to solve using MATLAB\'s ODE solvers. While the differential equations themselves are fairly simple, they depend on a lot of \"con

2条回答
  •  青春惊慌失措
    2021-01-21 08:05

    I would suggest creating specific "generator" functions for each system of ODEs you want to solve (based on Loren's suggestion to make use of anonymous functions). Here's what one might look like for your example:

    function odeFcn = makeODE(j,k,l,m,n,o)
      odeFcn = @(t,y) [-j*(k+y(1))/(l+y(1)); -m*(n+y(2))/(o+y(2))];
    end
    

    Each generator function would accept a set of input parameters and use them to create an anonymous function, returning the function handle as an output from the generator function. Here's how you can then use it:

    outputVector = ode15s(makeODE(a,b,c,d,e,f), [0,endTime], [x,y]);
    

提交回复
热议问题