且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Matlab for ode求解器中的循环

更新时间:2022-02-23 21:36:16

定义要修改的参数,作为函数的附加输入参数:

Define the parameters, which you want to modify as addional input parameter to the function:

function F = ODE_site(t,y,param)
%%Parameter block
k1f=param(1);
k1b=param(2); %%and so on

%%Elementary rate block
r1=k1f*y(1)-k1b*P*y(5);
and so on

%%Mass balance block
F=[  r1-r5-r78+r86 ;
    and so on ];
%% End of function

优化参数后,您可以在ode15()中将参数移交给函数:

You can hand over the parameters to your function in ode15() after the optimisation Parameters:

optode = odeset('NonNegative',1:41,'Abstol',1E-20,'RelTol',1E-20);
y0=zeros(41,1);
y0(41) = 1;
para=[1e5, 1e8]
[t,y]=ode15s(@ODE_site,[0,50000000000],y0,optode,para);

两个注意事项:

*)代码未经测试.由于您的示例不可执行,因此我也不必制作一个正在运行的示例.

*) Code is untested. Since your example was not executable I didn't care to make a running example too.

*)好像您正在解决化学反应动力学.您很可能可以通过使用矩阵和向量运算来进一步优化代码. 因此,不用使用单独的变量r1, r2, r3, ..您可以将其存储在一个向量r(1), r(2), r(3),中... 最后一行,可以用反应矢量与化学计量矩阵的乘积来写.

*) It looks like you are solving chemical reaction kinetics. Most likely you could further optimize your code by using matrix and vector operations. So instead of usning separate variables r1, r2, r3, .. you could store it in one vector r(1), r(2), r(3), ... Your last line, than could be written by the product of the reaction vector with the stoechiometric matrix.