简体   繁体   English

ode45具有基质初始条件

[英]ode45 with matrix initial conditions

I am a novice user of MATLAB. 我是MATLAB的新手。 I have code that is trying to find the time history of a state space model. 我有代码试图找到状态空间模型的时间历史。 There are four first order ODEs that I want to solve simultaneously using ode45 . 我想用ode45同时解决四个一阶ODE。 The essence of the equations to be solved is as follows: 要解决的方程的本质如下:

x1_dot = x2

x2_dot = -[M] * [K] * x1 - [M] * [C] * x2 + constant*[M] * [P3] * x3 + constant*[M] * [P4] * x4

x3_dot = x2 - constant*x3

x4_dot = x2 - constant*x4

Where [M] , [K] , [C] , [P3] , and [P4] are 3x3 matrices; 其中[M][K][C][P3][P4]是3x3矩阵; x1 , x2 , x3 , x4 are all 3x1 vectors; x1x2x3x4都是3x1向量; and x1_dot etc. represent the time derivatives (which are 3x1 vectors). x1_dot等表示时间导数(3x1向量)。 I have initial conditions only for x1 . 我只有x1初始条件。

The MATLAB code I've written is below. 我写的MATLAB代码如下。 This code is within my overall program. 这段代码在我的整个程序中。 I am not calling a separate function because I do not know how to pass all of the matrices/vectors into ode45 through a function. 我不是在调用单独的函数,因为我不知道如何通过函数将所有矩阵/向量传递到ode45 I am getting the error: "Index exceeds matrix dimensions." 我收到错误:“索引超出矩阵维度。”

tspan = 0:1:20;

initial = [0 0.03491 0];

f = @(t,x) [x(2);
            -inv(M_Dbl_Bar_Matrix)*K_Dbl_Bar_Matrix*x(1) - inv(M_Dbl_Bar_Matrix)*C_Dbl_Bar_Matrix*x(2) + (0.5*rho*U^2)*inv(M_Dbl_Bar_Matrix)*P3_Matrix*x(3) + (0.5*rho*U^2)*inv(M_Dbl_Bar_Matrix)*P4_Matrix*x(4);
            x(2) - Beta_1*x(3);
            x(2) - Beta_2*x(4)];

[t,xp] = ode45(f,tspan,initial);

Questions: 问题:

  1. How do I address the x(1) , x(2) , x(3) , and x(4) in ode45 being 3x1 vectors? 如何解决ode45中的x(1)x(2)x(3)x(4)为3x1向量?

  2. How do I apply initial conditions for this system of equations? 如何应用此方程组的初始条件? For example, do I use a vector format such as: initial = [0 0.03491 0; 0 0 0; 0 0 0; 0 0 0] 例如,我是否使用矢量格式,例如: initial = [0 0.03491 0; 0 0 0; 0 0 0; 0 0 0] initial = [0 0.03491 0; 0 0 0; 0 0 0; 0 0 0] initial = [0 0.03491 0; 0 0 0; 0 0 0; 0 0 0] ? initial = [0 0.03491 0; 0 0 0; 0 0 0; 0 0 0]

  3. Am I correctly using / written the function ( f ) and ode45 ? 我正确使用/写了函数( f )和ode45吗?

1.) What do you mean by "being 3x1 vectors"? 1.)“3x1向量”是什么意思? x(i) has to be a single variable in order for this to work, so size(x)=1x4. x(i)必须是单个变量才能使其工作,因此size(x)= 1x4。 Having x(i)=(x,y,z) does not really make any sense in the context of ODE's. 拥有x(i)=(x,y,z)在ODE的背景下并没有任何意义。

2.) Your variable vector x should have length 4, so your initial conditions should reflect this. 2.)您的变量向量x应该具有长度4,因此您的初始条件应该反映这一点。

initial = [0 0.03491 0 0];

Works fine for me. 对我来说很好。

3.) Yes, I think so. 3.)是的,我想是的。

Could you maybe provide more information on what you are trying to do? 你能提供一些关于你想做什么的更多信息吗?

EDIT 编辑

Ok, I think I understand now what you are trying to do. 好的,我想我现在明白你要做什么了。

You have 4 vectors for instance: x(x,y,z), x'(x,y,z), p(x,y,z), q(x,y,z) and a large matrix 4x4 matrix consisting of 3x3 matrices. 例如,你有4个向量:x(x,y,z),x'(x,y,z),p(x,y,z),q(x,y,z)和一个大矩阵4x4矩阵组成3x3矩阵。 So I guess 所以我想

(forgive the hosted images, since im a stackoverflow noob I'm not allowed to post html or images directly, or multiple links) (原谅托管图像,因为我是一个stackoverflow noob我不允许直接发布html或图片,或多个链接)

Reference for Math 1,2,3: https://postimg.org/gallery/1qh2ywiqq/ 数学1,2,3的参考: https//postimg.org/gallery/1qh2ywiqq/

Math 1 from link 数学1来自链接

with

Math 2 from link 数学2来自链接

your state space equation. 你的状态空间方程。

So to solve this you have to set up a 12 dimensional ode45 problem, instead of the 4 dimension problem you have now, since each vector has 3 components. 因此,要解决此问题,您必须设置12维ode45问题,而不是现在的4维问题,因为每个向量都有3个组件。 What you have to do is change the 4x4 matrix into a 12x12 matrix by specifying each of the entries explicitly. 您需要做的是通过明确指定每个条目将4x4矩阵更改为12x12矩阵。 You also have to give f = @(t,x) a 1x12 vector: 你还必须给f = @(t,x)一个1x12向量:

Math 3 from link 数学3来自链接

Then set initial to a 1x12 vector of initial conditions (one for each dimension in our vector). 然后将initial设置为初始条件的1x12向量(对于我们向量中的每个维度一个)。

With the matrix form and initial conditions we have now, we can use this source: https://nl.mathworks.com/help/symbolic/solve-a-system-of-differential-equations.html#buxuujb 通过我们现在的矩阵形式和初始条件,我们可以使用此来源: https//nl.mathworks.com/help/symbolic/solve-a-system-of-differential-equations.html#buxuujb

which tells us how to set this up properly. 它告诉我们如何正确设置它。

I currently don't have the time to give you the code, but I hope I understood correctly what you are trying to do and this will be useful. 我目前没有时间给你代码,但我希望我理解你正在尝试做什么,这将是有用的。

Just integrate the 12×1 system as if they are 12 coupled ODEs. 只需将12×1系统集成为12个耦合的ODE。

A couple of other observations: 其他几点意见:

  1. Avoid inv() wherever possible -- it's slow and inaccurate. 尽可能避免使用inv() - 它很慢且不准确。 Use mldivide (or mrdivide ) instead. 请改用mldivide (或mrdivide )。 Moreover, you recompute it no less than 4 times at each evaluation of f , while it's basically constant! 而且,在f每次评估中,你重新计算它不少于4次,而它基本上是不变的!
  2. You seem to want output at every second. 你似乎每秒都想要输出。 ode45 is a variable step integrator, meaning that it automatically adjusts the step size based on estimates of the error made at each step. ode45是一个可变步长积分器,这意味着它会根据每个步骤的误差估计值自动调整步长。 Requesting outputs at times that deviate from the times chosen by ode45 (called "dense output") is easy to do, but it'll cost you extra function evaluations. 请求输出有时偏离ode45选择的ode45 (称为“密集输出”)很容易,但它会花费额外的功能评估。 Usually that's not really needed, and you can get away with using the more efficient tspan = [0 20] instead. 通常这不是真的需要,你可以使用效率更高的tspan = [0 20]来逃避。 It all depends on your specific needs. 这一切都取决于您的具体需求。

Now, here's what I came up with: 现在,这就是我想出的:

% Time interval of interest
tspan = [0 20];

% Initial values
x1_0 = [0 0.03491 0];
x2_0 = [0       0 0];
x3_0 = [0       0 0];
x4_0 = [0       0 0];
x0   = [x1_0 x2_0 x3_0 x4_0].';

% Pre-compute a few constants
Fd  = 0.5*rho*U^2;
P3f = Fd*M_Dbl_Bar_Matrix\P3_Matrix;
P4f = Fd*M_Dbl_Bar_Matrix\P4_Matrix;
P1f = -M_Dbl_Bar_Matrix\K_Dbl_Bar_Matrix;
P2f = -M_Dbl_Bar_Matrix\C_Dbl_Bar_Matrix;

% The derivative (12×1, but constructed as 4·3×1)

one = 1:3;   three = 7:9;   % well-named index vectors to
two = 4:6;   four  = 10:12; % make our lives a bit easier

f = @(t,x) [x(two)
            P1f*x(one) + P2f*x(two) + P3f*x(three) + P4f*x(four)
            x(two) - Beta_1*x(three)
            x(two) - Beta_2*x(three)];

Now integrate this system
[t, xR] = ode45(f, tspan, x0);

% extract results in the same kind of blocks:
x1 = xR(:, one);
x2 = xR(:, two);
x3 = xR(:, three);
x4 = xR(:, four);

% ... process the results in whatever way you see fit

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

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