Using Matlab algorithm Find an approximating of integral0pi
Solution
Fortunately, expressions vulnerable to cancellation can often be recast in a mathematically equivalent form that is no longer affected by cancellation. There we studied several examples, and this problem gives some more.
We consider the function f1(x0, h) = sin(x0 + h) sin(x0).
It can the transformed into another form, f2(x0, h), using the trigonometric identity sin() sin() = 2 cos ( + 2 ) sin ( 2 ) .
Thus, f1 and f2 give the same values, in exact arithmetic, for any given argument values x0 and h. 1.
Derive f2(x0, h), which does no longer involve the difference of return values of trigonometric functions.
2. Suggest a formula that avoids cancellation errors for computing the approximation (f(x0 + h) f(x0))/h) of the derivative of
f(x) = sin(x) at x = x0. Integrate in between 0 to pi
a MATLAB program that implements your formula and computes an approximation of f (1.2), for h = 1 1020 , 1 1019 ,, 1.
Plot the error (in doubly logarithmic scale using MATLAB’s log plotting function) of the derivative computed with the suggested formula and with the naive implementation using f1.
