简体   繁体   English

使用ode45函数时odefun的输入变量是什么?

[英]What are the input variables of odefun when using the ode45 function?

I am trying to solve a quite simple ODE (1. order reaction rates) with the function ode45, but while I am able to do that successfully, I am not 100% sure what is actually happening. 我正在尝试使用功能ode45解决一个非常简单的ODE(1级反应速率),但是尽管我能够成功完成此操作,但我不确定100%实际发生了什么。 This is the code for reference: 这是供参考的代码:

x0 = [0.1,0.1];
tspan = [0, 10];
k = [1, 1];

[t_a, x_a] = ode45(@odefun, tspan, x0, [], k);


plot(t_a, x_a)
xlabel('t')
ylabel('x')
legend('A','B')

function dxdt = odefun(t,x,k)
    k1  = k(1);
    dxdt = zeros(2,1);
    dxdt(1) = -2*k1*x(1)*x(1);
    dxdt(2) = k1*x(1)*x(1);
end

This yields the expected results, but what I do not understand is, what the input parameter of odefun actually are (I know what they represent, but not how they were created, as I never defined them). 这样可以产生预期的结果,但是我不了解的是odefun的输入参数实际上是什么(我知道它们代表什么,但不知道它们是如何创建的,因为我从未定义过)。 The documentation was no great help for that, so I am wondering, where are the coming from? 文档并没有太大的帮助,所以我想知道,来源是什么? Are they provided by ode45? 它们由ode45提供吗? If so how is that possible when odefun is an input of ode45? 如果是这样,当odefun是ode45的输入时怎么办?

Obviously this code is not fully written by me, so, to be more specific, I have trouble understanding how I know I have to use x(1). 显然,此代码不是我完全编写的,因此,更具体地说,我很难理解我如何使用x(1)。

Thanks for your help! 谢谢你的帮助!

The t and x inputs are generated by ode45 based on the initial values of t and x which you provide. tx输入由ode45根据您提供的t和x的初始值生成。 ode45 calls the function with new values of t and x as it solves the problem, with the new values being based on the output of your function. ode45在解决问题时使用新的t和x值调用该函数,新值基于函数的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM