Unrecognized function or variable 'TrapComp'

조회 수: 3 (최근 30일)
Abdallah alsuod
Abdallah alsuod 2020년 12월 11일
댓글: Abdallah alsuod 2020년 12월 14일
Hello, every time i run this code it returns (Unrecognized function or variable 'TrapComp'.),
please help me,
function I = Romberg(f,a,b,n,n_levels)
%
% Romberg uses the Romberg integration scheme to find
% integral estimates at different levels of accuracy.
%
% I = Romberg(f,a,b,n,n_levels) where
%
% f is an inline function representing the integrand,
% a and b are the limits of integration,
% n is the initial number of equal-length
% subintervals in [a,b],
% n_levels is the number of accuracy levels,
%
% I is the matrix of integral estimates.
%
I = zeros(n_levels,n_levels); % Pre-allocate
% Calculate the first-column entries by using the
% composite trapezoidal rule, where the number of
% subintervals is doubled going from one element
% to the next.
for i = 1:n_levels,
n_intervals = 2^(i-1)*n;
I(i,1) = TrapComp(f,a,b,n_intervals);
end
% Starting with the second level, use Romberg scheme to
% generate the remaining entries of the table.
for j = 2:n_levels
for i = 1:n_levels - j+1,
I(i,j) = (4^(j-1)*I(i+1,j-1)-I(i,j-1))/(4^(j-1)-1);
end
end
  댓글 수: 3
Abdallah alsuod
Abdallah alsuod 2020년 12월 11일
I am using this code to get the results in here :
-------------------------------
For testing use a simple function: f = (x2 + 3x)2 over the interval of [0,1], with n = 2 and 3 levels of accuracy:
>> format long
>> f = inline('(x^2+3*x)^2');
>> I = Romberg(f,0,1,2,3)
I =
5.531250000000 4.700520833333 4.700000000000
4.908203125000 4.700032552083 0
4.752075195313 0 0
Star Strider
Star Strider 2020년 12월 11일
To quote from my previous Comment:
You need to go back to wherever you downloaded that file and download all the associated functions with it, and save them to the same directory.
That will likely solve your problem.

댓글을 달려면 로그인하십시오.

답변 (1개)

Uday Pradhan
Uday Pradhan 2020년 12월 14일
편집: Uday Pradhan 2020년 12월 14일
Hi,
As already mentioned in the comments, MATLAB cannot find the function "TrapComp", hence the error. Looking at the code, this function seems to be the composite Trapezoidal rule which itself is pretty simple to implement. You may use this as TrapComp:
function integral = TrapComp(f, a, b, n)
h = (b-a)/n;
result = 0.5*f(a) + 0.5*f(b);
for q = 1:(n-1)
result = result + f(a + q*h);
end
integral = h*result;
end
Results:
>> f = inline('(x^2+3*x)^2');
>> format long
>> I = Romberg(f,0,1,2,3)
I =
5.531250000000000 4.700520833333333 4.700000000000000
4.908203125000000 4.700032552083333 0
4.752075195312500 0 0
Hope this helps!
  댓글 수: 1
Abdallah alsuod
Abdallah alsuod 2020년 12월 14일
Thank you, that was the solution, I forgot about the function itself.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Downloads에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by