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'),endif ~(b>a),error('upper bound must be greater than lower'),endif nargin<4|isempty(n),n=100;endx = a; h = (b - a)/n;s=func(a,varargin{:});for i = 1 : n-1 x = x + h; s = s + 2*func(x,varargin{:});ends = 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.
%
Best Answer