如何在 Matlab 中输出具有指定标题列的表格?

如何在 Matlab 中输出具有指定标题列的表格?

我使用表中的“列”变量创建向量的技术仅在部分时间有效。以下代码中的 K1 与索引有何不同?我正在调试数值方法,需要列索引 X、K1、K2、K、Y。在我添加 K1、K2 和 K 之前,一切都正常?

功能代码:

function [index,X,K1,K2,K,Y] = impeulerT(x,y,x1,n)
% modified version of Improved Euler method found in
% Elementary Differential Equations by Edwards and Penney 
X=x;               % initial x
Y=y;               % initial y
x1 = x1;           % final x
n = n;             % number of subintervals
h = (x1-x)/n;      % step size
index = 0;         % initialize index
k1=0;
k2=0;
k=0;
for i=1:n;         % begin loop
k1=f(x,y);         % first slope
k2=f(x+h,y+h*k1);  % second slope
k=(k1+k2)/2;       % average slope
x=x+h;             % new x
y=y+h*k;           % new y
X=[X;x];           % update x-column       
Y=[Y;y];           % update y-column
index = [index;i]; % update index-column
K1=[K1;k1];   Line 22
K2=[K2;k2];
K= [K;k];
end                % end loop
ImprovedEulerTable=table(index,X,K1,K2,K,Y)
end

呼叫代码:

[index,X,K1,K2,K,Y] = impeulerT(0,1,1,10);

日志:

>> [index,X,K1,K2,K,Y] = impeulerT(0,1,1,10);
Undefined function or variable 'K1'.

Error in impeulerT (line 22)
K1=[K1;k1];

22  K1=[K1;k1];
>> 

答案1

这个代码更好。输出的第一行可以更改,以更好地匹配手动完成的操作。基本上,这意味着 K1、K2 和 K 列可以上移一行。其他列可以保持不变。但这与 Edwards 和 Penney 相匹配。请注意这里如何定义 K1、K2 和 K。干杯!MM

功能代码:

function [index,X,Y,K1,K2,K] = impeulerT(x,y,x1,n)
% modified version of Improved Euler method found in
% Elementary Differential Equations by Edwards and Penney 
X=x;               % initial x
Y=y;               % initial y
x1 = x1;           % final x
n = n;             % number of subintervals
h = (x1-x)/n;      % step size
index = 0;         % initialize index
% Initialize the lower-case variables
k1=0;
k2=0;
k=0;
% Initialize the upper-case variables
K1=k1;
K2=k2;
K =k;
for i=1:n;         % begin loop
k1=f(x,y);         % first slope
k2=f(x+h,y+h*k1);  % second slope
k=(k1+k2)/2;       % average slope
x=x+h;             % new x
y=y+h*k;           % new y
X=[X;x];           % update x-column       
Y=[Y;y];           % update y-column
index = [index;i]; % update index-column
K1=[K1;k1];        % update K1 column
K2=[K2;k2];        % update K2 column
K= [K;k];          % update K column
end                % end loop
ImprovedEulerTable=table(index,X,Y,K1,K2,K)
end

致电代码:

% Improved Euler
[index,X,Y,K1,K2,K] = impeulerTX(0,1,1,10);

答案2

错误在于第 22 行的K1=[K1;k1];方括号内有 K1,但之前并未定义过。

K1=[]; 解决方法是在for循环之前定义。

编辑:其他所有变量也一样。所以此代码有效

function [index,X,K1,K2,K,Y] = impeulerT(x,y,x1,n)
% modified version of Improved Euler method found in
% Elementary Differential Equations by Edwards and Penney 
X=x;               % initial x
Y=y;               % initial y
x1 = x1;           % final x
n = n;             % number of subintervals
h = (x1-x)/n;      % step size
index = 0;         % initialize index
k1=0;
k2=0;
k=0;
% Initialize the upper-case variables
K1=[];
K2=[];
K=[];
for i=1:n;         % begin loop
  k1=f(x,y);         % first slope
  k2=f(x+h,y+h*k1);  % second slope
  k=(k1+k2)/2;       % average slope
  x=x+h;             % new x
  y=y+h*k;           % new y
  X=[X;x];           % update x-column       
  Y=[Y;y];           % update y-column
  index = [index;i]; % update index-column
  K1=[K1;k1];  % Line 22
  K2=[K2;k2];
  K= [K;k];
end                % end loop
ImprovedEulerTable=table(index,X,K1,K2,K,Y)
end

相关内容