简体   繁体   English

在 Matlab 中求解矩阵值微分方程

[英]Solve a matrix valued differential equation in Matlab

I am trying to solve a particular system of ODE's dF/dt = A*F, F_initial = eye(9) .我正在尝试解决 ODE 的dF/dt = A*F, F_initial = eye(9)的特定系统。 Being a Matlab novice, I am trying to somehow use the implemented ode45 function, and I found useful advises online.作为一个 Matlab 新手,我试图以某种方式使用实现的ode45函数,我在网上找到了有用的建议。 However, all of them assume A to be constant, while in my case the matrix A is a function of t , in other words, A changes with each time step.然而,他们都假设 A 是常数,而在我的情况下,矩阵At的函数,换句话说, A随着每个时间步长而变化。

I have solved A separately and stored it in a 9x9xN array (because my grid is t = 0:dt:2 , N=2/dt is the number of time-steps, and A(:,:,i) corresponds to it's value at the i-th time step).我已经单独解决了A并将其存储在一个 9x9xN 数组中(因为我的网格是t = 0:dt:2N=2/dt是时间步数,而A(:,:,i)对应于它的第 i 个时间步的值)。 But I can't implement this array in ode45 to eventually solve my ODE.但是我无法在ode45实现这个数组来最终解决我的 ODE。

Any help is welcomed, and please tell me if I have missed anything important while explaining my problem.欢迎任何帮助,如果我在解释我的问题时遗漏了任何重要内容,请告诉我。 Thank you谢谢

First of all, F must be a column vector when using ode45.首先,使用ode45时F必须是列向量。 You won't ever get a result by setting F_initial = eye(9), you'd need F = ones(9,1).你永远不会通过设置 F_initial = eye(9) 得到结果,你需要 F =ones(9,1)。

Also, ode45 (documentation here, check tspan section) doesn't necessarily evaluate your function at the timesteps that you give it, so you can't pre-compute the A matrix.此外,ode45 (此处的文档,检查 tspan 部分)不一定在您提供的时间步长处评估您的函数,因此您无法预先计算 A 矩阵。 Here I'm going to assume that F is a column vector and A is a matrix which acts on it, which can be computed each timestep.在这里,我将假设 F 是一个列向量,A 是一个作用于它的矩阵,可以在每个时间步计算。 If this is the case, then we can just include A in the function passed to ode45, like so:如果是这种情况,那么我们可以在传递给 ode45 的函数中包含 A,如下所示:

F_initial = ones(9,1);
dt = 0.01;
tspan = 0:2/dt:2;
[t, F] = ode45(@(t,F) foo(t, F, Ainput), tspan, F_initial);

function f = foo(t, F, Ainput)
    A = calculate_A(t, Ainput);
    f = A*F;
end

function A = calculate_A(t, Ainput)
    %some logic, calculate A based on inputs and timestep
    A = ones(9,9)*sqrt(t)*Ainput;
end

The @(x) f(x,y) basically creates a new anonymous function which allows you to treat y as a constant in the calculation. @(x) f(x,y) 基本上创建了一个新的匿名函数,它允许您将 y 视为计算中的常量。

Hope this is helpful, let me know if I've misunderstood something or if you have other questions.希望这对您有所帮助,如果我误解了什么或者您有其他问题,请告诉我。

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

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