Why is this function not working?

3 views (last 30 days)
Aaron on 30 Oct 2013
Answered: Walter Roberson on 31 Oct 2013
I set the following function in my command window:
f=@(x) 0.2+25*x-200*x^2+675*x^3-900*x^4+400*x^5;
Then I created another function file:
function [q,ea,iter]=romberg(func,a,b,es,maxit,varargin)
% q = romberg(func,a,b,es,maxit,p1,p2,...):
% Romberg integration.
% input:
% func = name of function to be integrated
% a, b = integration limits
% es = desired relative error (default = 0.000001%)
% maxit = maximum allowable iterations (default = 30)
% pl,p2,... = additional parameters used by func
% output:
% q = integral estimate
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es), es=0.00000l;end
if nargin<5|isempty(maxit), maxit=50;end
n = 1;
I(1,1) = trap(func,a,b,n,varargin{:});
iter = 0;
while iter<maxit
iter = iter+l;
n = 2^iter;
I(iter+l,l) = trap(func,a,b,n,varargin{:});
for k = 2:iter+l
j = 2+iter-k;
I(j,k) = (4^(k-1)*I(j+1,k-1)-I(j,k-1))/(4^(k-1)-1);
end
ea = abs((I(1,iter+l)-I(2,iter))/I(1,iter+l))*100;
if ea<=es, break; end
end
q = I(1,iter+l);
This was copied and pasted from the ebook to use as a method. But its not working?!
I am getting the following error:
>> [q,ea,iter]=romberg(f,0,0.8)
Undefined function 'trap' for input arguments of type 'function_handle'.
Error in romberg (line 19)
I(1,1) = trap(func,a,b,n,varargin{:});
I've tried to make the "required" corrections but I end up with more errors. What did I mess up?

Image Analyst on 30 Oct 2013
Perhaps the name changes since it was written. Try trapz() instead.
Image Analyst on 31 Oct 2013
Sorry - I don't know. I've never used that function in MATLAB so I'd have to research it in the help just as you would have to. And since it 's your problem I'll let you do it.

Walter Roberson on 31 Oct 2013
trapz() does not accept a function as an argument.
It appears that your trap() reference might be to the following MATLAB File Exchange entry: http://www.mathworks.com/matlabcentral/fileexchange/3072-essential-matlab/content/Ch19/trap.m If so then you need to download and install that package.