Octave ODE求解器:状态和放大器的问题衍生矢量

时间:2018-04-12 11:19:10

标签: octave ode

我有解决非稳态传热问题的代码(不,这不是作业,但代码来自教科书),这需要用线法解决一组ODE,然后去如下:

%Problem P6_08A
clear, clc, format short g, format compact
tspan = [0 1000.]; % Range for the independent variable 
y0 = [100.; 100.; 100.; 100.; 100.; 100.; 100.; 100.; 100.]; % Initial values for the dependent variables 
%- - - - - - - - - - - - - - - - - - - - - -
function dYfuncvecdt = ODEfun(Yfuncvec, t); 
Yfuncvec = [];
T2 = Yfuncvec(1); 
T3 = Yfuncvec(2); 
T4 = Yfuncvec(3); 
T5 = Yfuncvec(4); 
T6 = Yfuncvec(5); 
T7 = Yfuncvec(6);
T8 = Yfuncvec(7); 
T9 = Yfuncvec(8); 
T10 = Yfuncvec(9); 
alpha = .00002; 
deltax = .1;
T1 = 0; 
T11 = (4 * T10 - T9) / 3; 
dT2dt = alpha / (deltax ^ 2) * (T3 - (2 * T2) + T1); 
dT3dt = alpha / (deltax ^ 2) * (T4 - (2 * T3) + T2); 
dT4dt = alpha / (deltax ^ 2) * (T5 - (2 * T4) + T3); 
dT5dt = alpha / (deltax ^ 2) * (T6 - (2 * T5) + T4); 
dT6dt = alpha / (deltax ^ 2) * (T7 - (2 * T6) + T5); 
dT7dt = alpha / (deltax ^ 2) * (T8 - (2 * T7) + T6); 
dT8dt = alpha / (deltax ^ 2) * (T9 - (2 * T8) + T7); 
dT9dt = alpha / (deltax ^ 2) * (T10 - (2 * T9) + T8); 
dT10dt = alpha / (deltax ^ 2) * (T11 - (2 * T10) + T9); 
dYfuncvecdt = [dT2dt; dT3dt; dT4dt; dT5dt; dT6dt; dT7dt; dT8dt; dT9dt; dT10dt]; 
end
%
[y, t]=lsode(@ODEfun, y0,tspan);
disp([y0 ODEfun(tspan(1),y0)]);
disp(' Variable values at the initial point ');
disp([' t    = ' num2str(tspan(1))]);
disp('           y                  dy/dt         ');
for i=1:size(y,2)
    disp([' Solution for dependent variable y' int2str(i)]);
    disp(['              t                  y' int2str(i)]);
    disp([t y(:,i)]);
    plot(t,y(:,i));
    title([' Plot of dependent variable y' int2str(i)]);
    xlabel(' Independent variable (t)');
    ylabel([' Dependent variable y' int2str(i)]);
    pause
end
%EOF

尝试运行代码会返回错误:

error: ODEfun: A(I): index out of bounds; value 1 out of bound 0
error: called from:
error:   ODEfun at line 8, column 4
error: lsode: evaluation of user-supplied function failed
error: lsode: inconsistent sizes for state and derivative vectors
error:   $path/lines01_diff_eq.m at line 33, column 5

这是我无法理解的部分index out of bounds; value 1 out of bound 0。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

事实证明这一行:

disp([y0 ODEfun(tspan(1),y0)]);

应改为:

disp([y0 ODEfun(y0, tspan(1)]);

还有一行:

disp([t y(:,i)]);
plot(t,y(:,i));

应修改为:

disp([tspan y(:,i)']);
plot(tspan, y(:,i)');

为了使代码顺利执行。