简体   繁体   English

使用ODE45的Vertcat错误

[英]Vertcat error using ODE45

I am trying to numerically simulate a system using ODE45. 我正在尝试使用ODE45数值模拟系统。 I cannot seem to figure out why i'm getting the following error: 我似乎无法弄清楚为什么出现以下错误:

Error using vertcat Dimensions of matrices being concatenated are not consistent. 使用vertcat时出错连接的矩阵维数不一致。

[t,x] = ode45(@NL_hw3c, [0,20], [1, 2, 3]);

function sys = NL_hw3c(t,x)
sys = [x(2)+ x(1)^2;
       x(3)+log10(x(2)^2 + 1);
       -3*x(1)-5*x(2)-3*x(3)+4*cos(t)-5*x(1)^2 -3*log10(x(2)^2 + 1) -6*x(1)*x(2) -6*x(1)^3 -(2*x(2)*(x(3)+ log10(x(2)^2 + 1)))/((x(2)^2 + 1)*log(10)) -2*x(1)*x(3) -2*x(1)*log10(x(2)^2 + 1) -2*x(2)^2 -8*x(1)^2*x(2) -6*x(1)^4];
end

Googled and couldn't find a similar solution. 谷歌搜索,找不到类似的解决方案。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

I had to separate each of the variables in your array for it to work: 我必须分离数组中的每个变量才能使其工作:

 function s = NL_hw3c(t,x)
    s1 = x(2)+ x(1)^2;
    s2 = x(3)+log10(x(2)^2 + 1);
    s3 = -3*x(1)-5*x(2)-3*x(3)+4*cos(t)-5*x(1)^2 -3*log10(x(2)^2 + 1) -6*x(1)*x(2) -6*x(1)^3 -(2*x(2)*(x(3)+ log10(x(2)^2 + 1)))/((x(2)^2 + 1)*log(10)) -2*x(1)*x(3) -2*x(1)*log10(x(2)^2 + 1) -2*x(2)^2 -8*x(1)^2*x(2) -6*x(1)^4;
    s = [s1;s2;s3];
 end

I got the following output: 我得到以下输出:

 t =

     0
0.0018
0.0037
0.0055
0.0074
0.0166
...
...
19.7647
19.8431
19.9216
20.0000

x =

 1.0000    2.0000    3.0000
 1.0055    2.0067    2.8493
 1.0111    2.0131    2.6987
 1.0167    2.0192    2.5481
 1.0224    2.0251    2.3975
 ...
 ...
 0.7926   -0.0187   -1.7587
 0.8380   -0.1567   -1.7624
 0.8781   -0.2928   -1.7534
 0.9129   -0.4253   -1.7299

The reason why your function didn't work was because in the last value of your array, the spaces between each part of the expression are interpreted as going into a separate column. 函数不起作用的原因是,在数组的最后一个值中,表达式各部分之间的空格被解释为进入单独的列。 Essentially, the first two rows of your matrix consist of 1 element and if you use the last expression exactly as it is, you would be trying to place 9 elements in the last row. 本质上,矩阵的前两行包含1个元素,并且如果完全按原样使用最后一个表达式,则将尝试在最后一行中放置9个元素。 I'm assuming you want a 3 x 1 matrix and the last element would thus violate the size of this matrix that you want to create and this is why it's giving you an error. 我假设您需要一个3 x 1的矩阵,因此最后一个元素将违反您要创建的矩阵的大小,这就是为什么它会给您带来错误。

I'm assuming you want the last value as an entire expression, so to do this you will need to place this as a separate expression then place it into your array. 我假设您希望最后一个值作为一个完整的表达式,因此,您需要将其作为一个单独的表达式放置,然后将其放入数组中。

To make the code more readable, I've placed all of the entries as separate variables before making the array. 为了使代码更具可读性,在制作数组之前,我将所有条目都放置为单独的变量。

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

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