OCTAVE - 从2个函数绘制值

时间:2015-01-19 16:45:52

标签: plot charts octave

问候语, 我有一个关于数字集成的Octave脚本的问题。 我有3个功能:1st计算Heuler积分;第二计算Heun集成;第3个应该在同一个窗口中绘制Heuler和heun的折线图。

问题是第1和第2个函数运行得很好,但第3个函数叫做previsao(),无论我做什么总是抛出错误: A(I,J):列索引超出范围;值1超出界限0 previsao在第124行,第7栏

第124行是这一行: plt1 = plot(eul(:,x),eul(:,ti),heu(:,x),heu(:,ti)); < / p>

以下是heuler的代码

function e = euler(x,ti,tf,h,fDerivada)

 xInicial = x;
 tInicial = ti;
 contador = 0;

 % inicia array na posição 1 a 0
  x(1) = 0;
 t(1) = 0;

 while(tInicial <= tf)

% implementa o método de Euler
xFinal = xInicial;
funcao = fDerivada(tInicial, xInicial);
xInicial = xInicial + h * funcao;

% insere valores nos arrays x e t e vai incrementando de posição no array
% vai dar origem à matriz com t e x 
x(contador + 1) = xInicial;
t(contador + 1) = tInicial;

tInicial = tInicial + h;

contador++;

 endwhile

 printf("\n");
 disp("Metodo de Euler:");
 % escreve a matriz em colunas
 [t', x']

 %gráfico 
 plt = plot(x);
 set(plt(1),"linewidth",2);
 set(plt(1),"color","r");
 xlabel("Tempo");
ylabel("Var dependente x");
title("Heuler");
legend("x");

 endfunction

Heun功能

function h = heun(x, ti, tf, h, fDerivada)

 xInicial = x;
 tInicial = ti;
 media = h/2;
 contador = 0;

 % inicia array na posição 1 a 0
 x(1) = 0;
 t(1) = 0;

while(tInicial <= tf)

% implementa o método de heun
xFinal = xInicial;
funcao = fDerivada(tInicial, xInicial);
% previsor
previsor = xInicial + h * funcao;
% cálculo com corretor
% var media = h/2
corretor = xInicial + media * (funcao + fDerivada(tInicial + h, previsor));

% insere valores nos arrays x e t e vai incrementando de posição no array
% vai dar origem à matriz com t e x 
x(contador + 1) = corretor;
t(contador + 1) = tInicial;

tInicial = tInicial + h;

contador++;

endwhile

printf("\n");
disp("Metodo de Heun:");
% escreve a matriz em colunas
[t', x']

% gráfico 
plt = plot(x);
set(plt(1),"linewidth",2);
set(plt(1),"color","r");
grid;
xlabel("Tempo");
ylabel("Var dependente x");
title("Heun");
legend("x");

endfunction

有问题的功能

它应该在同一个窗口中绘制euler和heun图表

previsao功能

function previsao(x, ti, tf, h, fDerivada)

% chama ambas as funções
% remover o ponto e virgula para fazer saida da matriz t, x
eul = euler(x,ti,tf,h,fDerivada);
heu = heun(x,ti, tf, h, fDerivada);

% gráfico comparativo entre ambas
plt1 = plot(eul(:,x),eul(:,ti),heu(:,x),heu(:,ti));
set(plt1(1),"linewidth",2);
set(plt1(1),"color","g");
set(plt1(2),"linewidth",2);
set(plt1(2),"color","r");
grid;
xlabel("var independente t");
ylabel("Var dependente x");
title("Metodos de integracao");
legend("Heuler","Heun");

endfunction

非常感谢可能的解决方案...

1 个答案:

答案 0 :(得分:0)

调试此类问题的典型方法是启用debug_on_error

debug_on_error(1);

再次运行您的脚本。这次它将在previsao中停止调试&gt;提示。现在你可以检测“eul”和“heu”,例如第一步就是

size(eul)
size(heu)
x
ti
那么问题应该是显而易见的。对于未来:请尝试将您的问题删除到您遇到问题的基本部分,而不是“这是我的x文件工作区”。如果代码可以复制并粘贴到解释器并且没有帮助的人可以看到问题,它总是有帮助的。在你的情况下,我必须弄清楚我需要x,ti,tf,h,fDerivada运行你的函数的值。

编辑:你的功能

function e = euler(x,ti,tf,h,fDerivada)

在这种情况下,不写回报值所以显然eul是[]并且我确信Octave会发出警告,对吧?顺便说一句,你不应该覆盖像e,pi,i ......这样的内置组件。