Asked by Camilo Sánchez
on 10 Nov 2017

function [xSol,ySol] = runKut4(dEqs,x,y,xStop,h)

%function [xSol,ySol] = runKut4(dEqs,x,y,xStop,h)

% 4th-order Runge--Kutta integration.

% USAGE: [xSol,ySol] = runKut4(dEqs,x,y,xStop,h)

% INPUT:

% dEqs = handle of function that specifies the 1st-order differential equations

% F(x,y) = [dy1/dx dy2/dx dy3/dx ...].

% x,y = initial values; y must be row vector.

% xStop = terminal value of x.

% h = increment of x used in integration.

% OUTPUT:

% xSol = x-values at which solution is computed.

% ySol = values of y corresponding to the x-values.

if size(y,1) > 1;

y = y';

end % y must be row vector

xSol = zeros(2,1);

ySol = zeros(2,length(y));

xSol(1)= x;

ySol(1,:)=y;

i = 1;

while x < xStop

i = i + 1;

h = min(h,xStop - x);

K1 = h*feval(dEqs,x,y);

K2 = h*feval(dEqs,x + h/2,y + K1/2);

K3 = h*feval(dEqs,x + h/2,y + K2/2);

K4 = h*feval(dEqs,x+h,y + K3);

y = y + (K1 + 2*K2 + 2*K3 + K4)/6;

x = x + h;

xSol(i) = x;

ySol(i,:) = y; % Store current soln.

end

-------------------------------------------------------------------------

function F = fex7_4(x,y)

% Differential. eqs. used in Example 7.4

F = zeros(1,2);

F(1) = y(2); F(2) = -0.1*y(2) - x;

[x,y] = runKut4(@fex7_4,0,[0 1],2,0.25);

printSol(x,y,1)

--------------------------------------------------------------

>> fex7_4 Error using fex7_4 (line 4) Not enough input arguments.

not enough input arguments ??????

Answer by Mischa Kim
on 10 Nov 2017

Edited by Mischa Kim
on 10 Nov 2017

Accepted Answer

Store runKut4 and fex7_4 in separate m-files and then execute

[x,y] = runKut4(@fex7_4,0,[0 1],2,0.25);

e.g. in the MATLAB command window and you should get results for x and y. printSol is probably another function you have access to, I do not know. But you could simply use plot to plot the solution trajectory.

Camilo Sánchez
on 10 Nov 2017

Thanks friend, I found the problem and the other function.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## James Tursa (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/366326-error-rungekutta-method-not-enough-input-arguments#comment_503434

## Camilo Sánchez (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/366326-error-rungekutta-method-not-enough-input-arguments#comment_503438

## Camilo Sánchez (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/366326-error-rungekutta-method-not-enough-input-arguments#comment_503439

Sign in to comment.