Reputation: 31
I'm trying to evaluate a function in matlab using quadtx but my code is giving me an error of "too many input arguments" and I am not sure what is wrong? Am I putting a wrong value in for varargin or is it something else?
I enter this in the MATLAB command prompt:
>f = @(t)(1./sin(sqrt(abs(t))));
>quadtx(f, -1, 2, 1.e-6, 3)
but receive the following:
Error using
@(t)(1./sin(sqrt(abs(t))))
Too many input arguments.Error in
quadtx
(line 29)
fa = feval(F,a,varargin{:});
Here is the MATLAB quadtx
code:
function [Q,fcount] = quadtx(F,a,b,tol,varargin)
%QUADTX Evaluate definite integral numerically.
% Q = QUADTX(F,A,B) approximates the integral of F(x) from A to B
% to within a tolerance of 1.e-6. F is a string defining a function
% of a single variable, an inline function, a function handle, or a
% symbolic expression involving a single variable.
%
% Q = QUADTX(F,A,B,tol) uses the given tolerance instead of 1.e-6.
%
% Arguments beyond the first four, Q = QUADTX(F,a,b,tol,p1,p2,...),
% are passed on to the integrand, F(x,p1,p2,..).
%
% [Q,fcount] = QUADTX(F,...) also counts the number of evaluations
% of F(x).
%
% See also QUAD, QUADL, DBLQUAD, QUADGUI.
% Make F callable by feval.
if ischar(F) & exist(F)~=2
F = inline(F);
elseif isa(F,'sym')
F = inline(char(F));
end
% Default tolerance
if nargin < 4 | isempty(tol)
tol = 1.e-6;
end
% Initialization
c = (a + b)/2;
fa = feval(F,a,varargin{:});
fc = feval(F,c,varargin{:});
fb = feval(F,b,varargin{:});
% Recursive call
[Q,k] = quadtxstep(F, a, b, tol, fa, fc, fb, varargin{:});
fcount = k + 3;
% ---------------------------------------------------------
function [Q,fcount] = quadtxstep(F,a,b,tol,fa,fc,fb,varargin)
% Recursive subfunction used by quadtx.
h = b - a;
c = (a + b)/2;
fd = feval(F,(a+c)/2,varargin{:});
fe = feval(F,(c+b)/2,varargin{:});
Q1 = h/6 * (fa + 4*fc + fb);
Q2 = h/12 * (fa + 4*fd + 2*fc + 4*fe + fb);
if abs(Q2 - Q1) <= tol
Q = Q2 + (Q2 - Q1)/15;
fcount = 2;
else
[Qa,ka] = quadtxstep(F, a, c, tol, fa, fd, fc, varargin{:});
[Qb,kb] = quadtxstep(F, c, b, tol, fc, fe, fb, varargin{:});
Q = Qa + Qb;
fcount = ka + kb + 2;
end
Upvotes: 0
Views: 1725
Reputation: 2777
f = @(t)(1./sin(sqrt(abs(t))))
has only one input variable t
.
Remove the fourth argument in quadtx(f, -1, 2, 1.e-6, 3)
---> quadtx(f, -1, 2, 1.e-6)
Meaning of varargin
Trigonometric functions are usually defined with additional parameter n
, namely
their periodicity.
For example fn(x) = sin(2*n*pi*x)
.
To evaluate this function you need to first fix n
, let's say n = 3
,
then evaluate f3(x)
.
To sum up
To use the fourth varargin
in quadtx
, update f
, for instance
f = @(x, n) = (1./sin(n*sqrt(abs(t))))
--> quadtx(f, -1, 2, 1.e-6, 3)
As reference you may check Bessel Function.
Upvotes: 1