MATLAB:没有足够的输入论证

时间:2017-02-27 07:44:46

标签: matlab

我尝试多次运行此代码,并且因为我在最后一个for循环中添加了运气。在错误之前,向量k不会更新,因此向量L是重复的相同数字。

我无法弄清楚为什么我会得到“没有足够的输入参数'事先工作正常时出错。

非常感谢任何帮助!

% Set up parameters of the functions
omega = 2*pi/10; % 1/s    
g = 9.81; % m/s^2
h = 20; % m
parms = [omega, g, h];
% Set up the root finding variables
etol = 1e-6; % convergence criteria
iter = 100; % maximum number of iterations
f = @my_fun; % function pointer to my_func
fp = @my_fprime; % function pointer to my_fprime
k0 = kguess(parms); % initial guess for root
% Find the root
[k, error, n_iterations] = newtraph(f, fp, k0, etol, iter, parms);
% Get the wavelength
if n_iterations < iter
% Converged correctly
L = 2 * pi / k;
else
% Did not converge
disp('ERROR: Maximum number of iterations exceeded')
return
end

wave = load('wavedata.dat');
dt = 0.04; %s
%dh = 0.234; %water depth in meters
wave = wave*.01; %covnverts from meters to cm
nw = wave([926:25501],1);
a = length(nw);
t = 0;
spot = 1;
points  = zeros(1,100);
for i = 1:a-1
    t=t+dt;
    if nw(i) < 0 
        if nw(i+1) > 0
        points(spot)=t;
        spot=spot+1;
        t=0;
        end
    end
end

omega = 2*pi./points; %w
l = length(points);
L = zeros(1,509);
k = zeros(1,509);
for j = 1:l
g = 9.81; % m/s^2
h = 0.234; % m
parms = [omega(j), g, h];
% Set up the root finding variables
etol = 1e-6; % convergence criteria
iter = 100; % maximum number of iterations
f = @my_fun; % function pointer to my_func
fp = @my_fprime; % function pointer to my_fprime
k0(j) = kguess(parms); % initial guess for root
% Find the root
[k(j), error, n_iterations] = newtraph(f, fp, k0(j), etol, iter, parms);
% Get the wavelength
if n_iterations < iter
    % Converged correctly
    L(j) = 2 * pi / k(j);
else
    % Did not converge
    disp('ERROR: Maximum number of iterations exceeded')
    return
end
end

    function [ f ] = my_fun(k,parms)
%MY_FUN creates a function handle for linear dispersion
%   Detailed explanation goes here
w = parms(1) ;
g = parms(2);
h = parms(3);

f = g*k*tanh(k*h)-(w^2);
end

    function [ fp ] = my_fprime(k,parms)
%MY_FPRIME creates a function handle for first derivative of linear 
%   dispersion.

g = parms(2);
h = parms(3);
% w = 2*pi/10; % 1/s
% g = 9.81; % m/s^2
% h = 20; % m

fp = g*(k*h*((sech(k*h)).^2) + tanh(k*h));

end

    function [ k, error, n_iterations ] = newtraph( f, fp, k0, etol, iterA, parms )
%NEWTRAPH Estimates the value of k using the newton raphson method.
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(etol),es=etol;end
if nargin<5|isempty(iterA),maxit=iterA;end
iter = 0;
k = k0;
%func =@f;
%dfunc =@fp;
while (1)
    xrold = k;
    k = k - f(k)/fp(k);
    iter = iter + 1;
    if k ~= 0, ea = abs((k - xrold)/k) * 100; end
    if ea <= etol | iter >= iterA, break, end
end
error = ea;
n_iterations = iter;

end

1 个答案:

答案 0 :(得分:1)

在功能newtraph第106行(while(1)循环中的第二行),您忘记将parms传递给函数调用f

k = k - f(k)/fp(k);

应该成为

k = k - f(k,parms)/fp(k,parms);