[英]Octave ODE solver: issues with state & derivative vectors
I have the code to solve a problem of non-stationary heat transfer (no, this is not homework, but the code is taken from a textbook), which requires solving a set of ODEs with the Method of Lines, and goes as follows: 我有解决非平稳传热问题的代码(不,这不是家庭作业,但是代码取自教科书),这需要使用“线法”解决一组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
Trying to run the code returns an error: 尝试运行代码将返回错误:
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
It is the part index out of bounds; value 1 out of bound 0
它是index out of bounds; value 1 out of bound 0
的零件index out of bounds; value 1 out of bound 0
index out of bounds; value 1 out of bound 0
that I can't make sense of. 我无法理解的index out of bounds; value 1 out of bound 0
。 Can somebody help? 有人可以帮忙吗?
It turns out that that line: 原来那行:
disp([y0 ODEfun(tspan(1),y0)]);
Should instead become: 应该改为:
disp([y0 ODEfun(y0, tspan(1)]);
And also the lines: 还有这些行:
disp([t y(:,i)]);
plot(t,y(:,i));
Should be modified to: 应该修改为:
disp([tspan y(:,i)']);
plot(tspan, y(:,i)');
In order to make the code execute without errors. 为了使代码执行无错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.