필터 지우기
필터 지우기

Why is this function not working?

조회 수: 1 (최근 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 CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by