0

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];
>> 

2 Answers 2

0

This code is better. THe first line of output could be changed to match better what is done by hand. Basically what that means is that K1, K2 and K columns could be shifted up one row. Other columns could be left the same. THis matches Edwards and Penney however. Note how K1,K2, and K are defined here. Cheers! MM

Function Code:

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

Call Code:

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

The error is on the fact that on line 22 K1=[K1;k1]; has K1 inside the square brackets, but it wasn't defined before.

The solution is to define K1=[]; before the for loop.

Edit: the same goes for all the other variables. So this code works

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
2
  • When I run this code I get an error message.Error in impeulerTS (line 26) K1=[K1;k1]; Line 22 Mar 24, 2019 at 12:54
  • @MaryA.Marion, Sorry, I forgot to fix that part of the code you provided in the question. I added the % to comment the Line 22 text
    – pittix
    Mar 24, 2019 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .