My technique of making a vector out of "column" variables in a table only works part of the time. What is different about K1 from index in the following code? I am debugging a numerical method and I need the columns index, X, K1, K2, K, Y. All works fine until I add K1, K2, and K?
FUNCTION CODE:
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
CALL CODE:
[index,X,K1,K2,K,Y] = impeulerT(0,1,1,10);
LOG:
>> [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];
>>