首页 > 解决方案 > Octave - 带有 lsode 的微分方程组

问题描述

这是我的问题。由于下面的两个函数,我正在尝试求解一个由两个微分方程组成的系统。给我带来麻烦的代码部分是变量“rho”。“rho”是一个函数,它的值是从文件中给出的,我试图把它放入变量 rho。

function [xdot]=f2(x,t) 
# Parameters of the equations
t=[1:1:35926];
x = dlmread('data_txt.txt');
rho=x(:,4);
beta = 0.68*10^-2;    
g = 1.5;         
l = 1.6;         

# Definition of the system of 2 ODE's :

xdot(1) = ((rho-beta)/g)*x(1) + l*x(2); 
xdot(2) = (beta/g)*x(1)-l*x(2);    

endfunction

.

# Initial conditions for the two variables :
x0 = [0;1];

# Definition of the time-vector -> (initial time,temporal mesh,final time) :
t = linspace (1, 10, 10000);  

# Resolution with the lsode routine :
x = lsode ("f2", x0, t);

# Plot of the two curves :
plot (t,x);

当我运行我的代码时,我收到以下错误:

>> resolution_effective2

  error: f2: A(I) = X: X must have the same size as I
  error: called from
  f2 at line 34 column 9
  resolution_effective2 at line 8 column 3
  error: lsode: evaluation of user-supplied function failed
  error: called from
  resolution_effective2 at line 8 column 3
  error: lsode: inconsistent sizes for state and derivative vectors
  error: called from
  resolution_effective2 at line 8 column 3

我知道我的错误来自某些变量之间的大小不匹配,但我已经找了好几天了,但我没有看到。有人可以尝试给我一个有效的更正并向我解释吗?谢谢

标签: octave

解决方案


错误可能来自您的函数f2。它的第一个参数应该具有与的初始条件x相同的维度x0x0x

在您的情况下,无论您打算成为第一个参数,f2都将被忽略,因为在您的函数中,您这样做x = dlmread('data_txt.txt');似乎是一个错误。

那么,xdot(1) = ((rho-beta)/g)*x(1) + l*x(2);将是一个问题,因为rho它是一个向量。

您需要检查 和 的x尺寸rho


推荐阅读