Question: Write a Matlab function to compute f(x) = 1 cos(x) for inputs in the range 1 < x < 1 with fractional error under 10.

Write a Matlab function to compute f(x) = 1 cos(x) for inputs in the range 1 < x < 1 with fractional error under 10. Check your function by verifying that, to 14 significant digits, it returns f(104 ) = 4.9999999958333 109 and f(0.9) = 0.37839003172934.

function y = oneminuscos (x) %Compute 1 - cos(x) for scalar real inputs in the range -1 < x < 1 %with fractional error under 10*eps. % %For example, to 14 significant digits, it should return %oneminuscos(1E-4) = 4.9999999958333E-9 and oneminuscos(0.9) = 0.37839003172934

Hint: Subtracting 1 cos(x) directly incurs subtractive cancellation when x is close to 0, but for such values of x, the truncation error from using the first few terms of the Taylor series of cos(x) with a = 0 and cancelling out the 1 analytically is very small.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!