error : Function integration trapezoidal

i want to calculate area from numerical integration of function f(x) with the limits a to b and vary n . by the way i use this code from my handbook, numerical chapra. to solve my problem. but the result was error.
function I = trap(func,a,b,n,varargin)
% trap: composite trapezoidal rule quadrature
% I = trap(func,a,b,n,p1,p2,...):
% composite trapezoidal rule
% input:
% func = name of function to be integrated
% a, b = integration limits
% n = number of segments (default = 100)
% p1,p2,... = additional parameters used by func
% output:
% I = integral estimate
if nargin<3,error('at least 3 input arguments required'),end
if ~(b>a),error('upper bound must be greater than lower'),end
if nargin<4|isempty(n),n=100;end
x = a; h = (b - a)/n;
s=func(a,varargin{:});
for i = 1 : n-1
x = x + h;
s = s + 2*func(x,varargin{:});
end
s = s + func(b,varargin{:});
I = (b - a) * s/(2*n);
the error :
??? function I = trap(func,a,b,n,varargin)
|
Error: Function definitions are not permitted in this context.
what should i do ?
--- my purpose is to make the table like below this text, to known how the error result with vary n parameter.
%

2 件のコメント

sixwwwwww
sixwwwwww 2013 年 12 月 3 日
you can't directly run this function. first save it in an m-file with name 'trap.m' and then call with appropriate input parameters from command prompt or from another script
jakv
jakv 2013 年 12 月 3 日
what the script that i can use ? sorry i'm newbie.

サインインしてコメントする。

 採用された回答

sixwwwwww
sixwwwwww 2013 年 12 月 4 日

0 投票

Maybe following links are helpful for you for this purpose:
These links shows how to use matlab functions. and how to input and get output values from them

4 件のコメント

jakv
jakv 2013 年 12 月 4 日
編集済み: jakv 2013 年 12 月 4 日
thanks, i could used the script correctly,
i still confuse about this script
if nargin<3,error('at least 3 input arguments required'),end
if ~(b>a),error('upper bound must be greater than lower'),end
if nargin<4|isempty(n),n=100;end
especially nargin and varargin.
what the means of above script ?
sixwwwwww
sixwwwwww 2013 年 12 月 4 日
'varargin' mean variable number of input arguments and 'nargin' mean number of input arguments. If you use 'varargin' in function definition then you can input any number of arguments to that function and by using 'nargin' you can know how many arguments are input to a function. For more information see:
Good luck!
Ahzabuddin
Ahzabuddin 2016 年 5 月 20 日
can you help me too??? I get trapezoidal single... but I have trouble unequal trapezoidal.
x = [0 .12 .22 .32 .36 .4 .44 .54 .64 .7 .8];
y = 0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;
>>trapuneq(x,y)
??? Undefined function or variable 'l'.
Error in ==> trapuneq at 18
s = s+(x(k+l)-x(k))*(y(k)+y(k+l))/2;
my formula M-File is
if nargin<2,error('at least 2 input arguments required'),end
if any(diff(x)<0),error('x not monotonically ascending'),end
n = length(x);
if length(y)~=n,error('x and y must be same length'); end
s = 0;
for k = 1:n-1
s = s+(x(k+l)-x(k))*(y(k)+y(k+l))/2;
end
I = s;
Sam
Sam 2023 年 4 月 14 日
Changing the 2 "l"s ("L"s; Capital shown for variable clarity) to a "1" ("One") in the line of the For loop fixes the error and allows the same anser to be attained as shown in the textbook (1.5948).

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2016 年 5 月 20 日
編集済み: Torsten 2016 年 5 月 20 日

0 投票

My guess is that the "l" in the formula
s = s+(x(k+l)-x(k))*(y(k)+y(k+l))/2;
should be a "1".
Best wishes
Torsten.

1 件のコメント

Sam
Sam 2023 年 4 月 14 日
Yes this appears to be correct, @Torsten.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

質問済み:

2013 年 12 月 3 日

コメント済み:

Sam
2023 年 4 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by