Graphics Programs Reference
In-Depth Information
function Ih = trapezoid(func,a,b,I2h,k)
% Recursive trapezoidal rule.
% USAGE: Ih = trapezoid(func,a,b,I2h,k)
% func = handle of function being integrated.
% a,b = limits of integration.
% I2h = integral with 2ˆ(k-1) panels.
% Ih = integral with 2ˆk panels.
ifk==1
fa = feval(func,a); fb = feval(func,b);
Ih=(fa+fb)*(b-a)/2.0;
else
n = 2ˆ(k -2 );
% Number of new points
h=(b-a)/n;
%Spacingofnewpoints
x=a+h/2.0;
%Coord.of1stnewpoint
sum=0.0;
fori=1:n
fx = feval(func,x);
sum=sum+fx;
x=x+h;
end
Ih = (I2h + h*sum)/2.0;
end
Simpson's Rules
Parabola
f ( x )
Figure 6.4. Simpson's 1
/
3rule.
ξ
h
h
x
x = a
x 2
x = b
3
1
3;
that is, by passing a parabolic interpolantthroughthree adjacent nodes, as shown in
Fig. 6.4. The areaunder the parabola,which represents anapproximation of a
Simpson's 1
/
3 rule can beobtained from Newton-Cotes formulas with n
=
f ( x ) dx ,
is (see derivation in Example 6.1)
f ( a )
4 f a
f ( b ) h
3
+
b
I
=
+
+
(a)
2
 
Search WWH ::




Custom Search