Error RungeKutta method......not enough input arguments
3 views (last 30 days)
Show older comments
Camilo Sánchez
on 10 Nov 2017
Commented: 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 ??????
3 Comments
Accepted Answer
Mischa Kim
on 10 Nov 2017
Edited: Mischa Kim
on 10 Nov 2017
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.
More Answers (0)
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!