Error RungeKutta method......not enough input arguments

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 个评论

Please show us exactly how you are calling your function (i.e., give us the inputs), and then copy & paste the entire error message for us to see.
Upss sorry....here is:
[x,y] = runKut4(@fex7_4,0,[0 1],2,0.25);
printSol(x,y,1)
>> fex7_4 Error using fex7_4 (line 5) Not enough input arguments.

请先登录,再进行评论。

 采纳的回答

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.

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by