Matlab for ode求解器中的循环 [英] matlab for loop within ode solver

查看:145
本文介绍了Matlab for ode求解器中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ODE解算器来解算我的ODE系统.现在我也想通过改变它们来计算各种参数的灵敏度. 我的基本ODE求解器程序如下所示: 功能:

i am using ODE solver for solving my system of ODE. Now i also want to calculate sensitivity of various parameters by varying them. My basic ODE solver program is something like: Function:

function F = ODE_site(t,y)
%%Parameter block
k1f=1e8;
k1b=1e5; %%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

主要的ODE求解器如下:

The main ODE solver looks like:

optode = odeset('NonNegative',1:41,'Abstol',1E-20,'RelTol',1E-20);
y0=zeros(41,1);
y0(41) = 1;
[t,y]=ode15s(@ODE_site,[0,50000000000],y0,optode);
%% Plus some other lines to plot the solution
%% End of ODE solver

现在,我想在主求解器中引入一个for循环,但问题是如果我在主求解器中使用for循环,它无法将命令传递给函数.我尝试使用global将变量分配给整个环境,但是没有用.如何为ODE求解器代码和函数分配for循环? 任何帮助将不胜感激. 谢谢, 马门

Now, I want to introduce a for loop in the main solver but the problem is if i use for loop in the main solver, it cannot pass the command to the function. I tried to use global to assign the variable to all over the environment but it didn't work. How can i assign the for loop for both the ODE solver code and function? Any help will be highly appreciated. Thanks, Mamun

推荐答案

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

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.

这篇关于Matlab for ode求解器中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆