简体   繁体   English

Matlab:从ODE45导出变量

[英]Matlab: Exporting variables from ODE45

I have an ODE that uses many functions. 我有一个使用许多功能的ODE。 I wish to export these "helper" functions so that I may graph them vs the independent variable of the ODE. 我希望导出这些“帮助器”函数,以便可以将它们与ODE的自变量相对应。

function dFfuncvecdW = ODE(W,Ffuncvec); 
X = Ffuncvec(1); 
y = Ffuncvec(2); 

#lots of code
R = ... #R is a function of X,W and y.
#and a few other functions that are a function of X,W and y.

dXdW = ... #some formula
dydW = ... #some formula
dFfuncvecdW = [dXdW; dydW];

end

I call this function with: 我称这个功能为:

Wspan = [0 8000.]
X0 = [0; 1.]
[W,X] = ode45(@ODE, Wspan, X0);

I can easily output X or W to an excel file: 我可以轻松地将X或W输出到excel文件:

xlswrite(filename,X,'Conversion','A1');

But I what I need is to save "R" and many other functions' values to an Excel file. 但是我需要将“ R”和许多其他函数的值保存到Excel文件中。

How do I do that? 我怎么做?
I am still extremely new to Matlab. 我还是Matlab的新手。 I usually use Polymath, but for this system of ODE's, Polymath cannot compute the answer within a reasonable amount of time. 我通常使用Polymath,但是对于这种ODE系统,Polymath无法在合理的时间内计算出答案。

EDIT1: The code I use was generated by Polymath. EDIT1:我使用的代码是由Polymath生成的。 I used a basic version of my problem so that Polymath may excecute the program as it only gives the Matlab code once the Polymath code has succefully run. 我使用了问题的基本版本,以便Polymath可以执行该程序,因为只有Polymath代码成功运行后,它才提供Matlab代码。 After the export, the complete set of equations were entered. 导出后,输入了完整的方程组。

The easiest, and possibly fastest, way to handle this is to re-evaluate your functions after ode45 returns W and X . 处理此问题的最简单且可能最快的方法是 ode45返回WX 之后重新评估您的函数。 If the functions are vectorized it will be easy. 如果功能被矢量化,将很容易。 Otherwise, just use a simple for loop that iterates from 1 to length(W) . 否则,只需使用从1length(W)迭代的简单for循环。

Alternatively, you can use an output function to save your values on each iteration to a file, or a global , or, most efficiently, a sub-function (aka nested function ) variable that shares scope with an outer function (see here , for example). 另外,您可以使用输出函数将每次迭代的值保存到文件中,或者将global (最有效地是与外部函数共享作用域的子函数(又称嵌套函数 ))保存到文件中(请参见此处 ,有关例)。 See this answer of mine for an example of how to use an output function. 有关如何使用输出函数的示例,请参见我的答案

I found a rather quick and painless solution to my answer. 我找到了一个相当快速而轻松的解决方案。 I merely appended a text file with code inside the ode function. 我只是在ode函数内部附加了带有代码的文本文件。

EDIT: I am unable to comment because I do have enough rep on this branch of SE. 编辑:我无法发表评论,因为我在SE的这个分支上确实有足够的代表。 My solution was add the following code: 我的解决方案是添加以下代码:

fid = fopen('abc1.txt', 'at');
fprintf(fid, '%f\n', T);
fclose(fid);

right above 正上方

dYfuncvecdW = [dFAdW; dFBdW; dFCdW; dFDdW; dydW]; 

at the end of the ode function. 在ode函数的末尾。 This proved to be a temporary solution. 事实证明,这是一个临时解决方案。 I have opened another question about the output I recieved. 我对收到的输出提出了另一个问题。

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

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