Why is this function not working?

2 ビュー (過去 30 日間)
Aaron
Aaron 2013 年 10 月 30 日
回答済み: Walter Roberson 2013 年 10 月 31 日
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)
% romberg: Romberg integration quadrature
% 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?

回答 (2 件)

Image Analyst
Image Analyst 2013 年 10 月 30 日
Perhaps the name changes since it was written. Try trapz() instead.
  2 件のコメント
Aaron
Aaron 2013 年 10 月 30 日
Here is my new error:
>> [q,ea,iter]=romberg(f,0,0.8)
Error using trapz
Too many input arguments.
Error in romberg (line 19)
I(1,1) = trapz(func,a,b,n,varargin{:});
Image Analyst
Image Analyst 2013 年 10 月 31 日
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
Walter Roberson 2013 年 10 月 31 日
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.

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by