Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Computing (e\" - 1)/x. Consider two algorithms for the evaluation of f(x) (e

ID: 2251650 • Letter: 2

Question

2. Computing (e" - 1)/x. Consider two algorithms for the evaluation of f(x) (e Algorithm A if x-0 else end Algorithm B else f end (y-1)/ logy Use Matlab to calculate f(x) for x-10-t,t = 5, , 16 with both algorithms Tabulate your results. Comment on the results. Which algorithm produces the better answers! lo of f(x) for x = 9 10-8 and a machine epsilon approximately 6 * 10-8 by both algorithms A and B. relative error not exceeding the machine epsilon and that one additional digit is used in the computation. Verify that Algorithm B performs well because of cancellation. try and explain what is happening consider the calculation Now assume that both expx and logy are computed with a

Explanation / Answer

%%% Matlab code %%%

clc;
clear all;
close all;
format long

%%% Algorithm A

t=5:16;
for n=1:length(t)
    x=10^(-t(n));
    if (x==0)
        F1(n)=1
    else
        F1(n)=(exp(x)-1)/x;
    end
end


%%% Algorithm B

t=5:16;
for n=1:length(t)
    x=10^(-t(n));
    y=exp(x);
    if (y==1)
        F2(n)=1;
    else
        F2(n)=(y-1)/log(y);
    end
end


fprintf('Algorithm A   Algorithm B   ');
for n=1:length(t)
    fprintf('%1.9f   %1.9f ',F1(n),F2(n));
end

OUTPUT:

Algorithm A   Algorithm B
1.000005000   1.000005000
1.000000500   1.000000500
1.000000049   1.000000050
0.999999994   1.000000005
1.000000083   1.000000001
1.000000083   1.000000000
1.000000083   1.000000000
1.000088901   1.000000000
0.999200722   1.000000000
0.999200722   1.000000000
1.110223025   1.000000000
0.000000000   1.000000000