Graphics Programs Reference
In-Depth Information
% Example 6.4 (Recursive trapezoidal rule)
format long
% Display extra precision
I2h=0;
fork=1:20
Ih = trapezoid(@fex6
4,0,pi,I2h,k);
if(k>1&abs(Ih-I2h)<1.0e-6)
Integral = Ih
No
_
_
of
_
func
_
evaluations = 2ˆ(k-1) + 1
return
end
I2h=Ih;
end
error('Too many iterations')
The M-file containing the function to be integratedis
functiony=fex6
4(x)
% Function used in Example 6.4
y = sqrt(x)*cos(x);
_
Here is the output:
>> Integral =
-0.89483166485329
No
_
of
_
func
evaluations =
32769
_
Rounding to six decimal places, wehave 0 x cos x dx
894 832
The number of function evaluations is unusually large in this problem. The slow
convergence is the result of the derivatives of f ( x ) being singular at x
=−
0
.
=
0.Conse-
c 1 h 2
c 2 h 4
quently, the errordoes not behave as shown in Eq. (6.7): E
, but is
unpredictable.Difficulties of this na tu recan oftenbe rem e diedbyachange in vari-
able. In thiscase, we introduce t
=
+
+···
= x ,sothat dt
(2 x )
=
dx
/
=
dx
/
(2 t ), or dx
=
2 tdt .
Thus
π
π
x cos x dx
2 t 2 cos t 2 dt
=
0
0
Evaluation of the integralon the right-hand side wouldrequire4097 function
evaluations.
Search WWH ::




Custom Search