## MATLAB Corrected Trapezoidal Rule

Find help with Math software, Engineering software, Graph software, Scientific software, Maple, Mathematica, Matlab, Pari/GP, calculators: Casio Classpad / TI Voyage / HP ...

### MATLAB Corrected Trapezoidal Rule

I have this function:

function y = corrtrap(fname, fpname, a, b)
% Corrected trapezoidal rule y.
% fname - the m-file used to evaluate the integrand,
% fpname - the m-file used to evaluate the first derivative
% of the integrand,
% a,b - endpoinds of the interval of integration.
h = b - a;
y = (h/2).*(feval(fname,a) + feval(fname,b))+ (h.^2)/12.*( ...
feval(fpname,a) - feval(fpname,b));

Not sure how to compute the m-files for fname and fpname...help please?
wale06
Newcomer

Posts: 7
Joined: Mon Oct 24, 2011 7:42 pm

### Re: MATLAB Corrected Trapezoidal Rule

Well, you should give more information. You could do something like this, where I assumed a sample function

$-\ln{(x)}\sinh{(x)}$

where I compute the integral from 0,1.

Code: Select all
del = sqrt(eps); % derivative stepfname = @(x) -log(x)*sinh(x); % anonymous functionfprime = @(x) ((-log(x+del)*sinh(x+del))+log(x)*sinh(x))/(del); %approximate derivativep=10^3; % grid pointsa=0.1;  % left integration limitb=1;    % right integration limitg=linspace(a,b,p); % discrete spacefint = 0; % initial valuefor i=1:p-1fint(i+1) = fint(i) + (g(i+1)-g(i))/2*(fname(g(i+1))+fname(g(i)))...                    - (g(i+1)-g(i))^2/12*(fprime(g(i+1))+fname(g(i)));endsurf = fint(end)% Built in matlab command trapz: X=linspace(a,b);Y=-log(X).*sinh(X);trapo = trapz(X,Y)

cheers
maxiim
Newcomer

Posts: 12
Joined: Tue Dec 13, 2011 4:55 am