Question: 4. What is machine epsilon in the default Octave real variable precision. 5. What is machine epsilon in the Octave real variable single precision. clc,

4. What is machine epsilon in the default Octave real variable precision. 5. What is machine epsilon in the Octave real variable single precision.

clc, clear, close all

% WRITING THE PROJECT DATE

printf("PROJECT 1 - Round-off vs Truncation Error ")

printf(" ")

display(date())

printf("======================================== ")

% f(x)= f

f=@(x) 12.5+3.5.*x.*cos(2.45.*x);

% df(x) is the first order derivative

df=@(x) -8.575*x*sin(2.45*x)+3.5*cos(2.45*x);

x = 2.75;

for i = 1:20

del (i,1)= 10^(-i);

%Backward finite difference

backward(i,1) = (f(x)-f(x - del(i,1)))/del(i,1);

%Forward finite difference

forward(i,1) = (f(x + del(i,1)) - f(x))/del(i,1);

%Central Finite Difference

central (i,1)= (f(x+del(i,1)) - f(x - del(i,1)))/(2*del(i,1));

%Calculate the errors compared to exact derivative

backward_error(i,1) = abs(backward(i,1) - df(x));

forward_error(i,1) = abs(forward(i,1) - df(x));

central_error(i,1) = abs(central(i,1) - df(x));

end

%plot the error vs del x

figure

hold on

plot(log10(del),log10(forward_error))

plot(log10(del),log10(backward_error))

plot(log10(del),log10(central_error))

xlabel('\Delta'), ylabel('Error')

title('Error on Forward, Backward, and Central')

legend('Forward','Backward','Central')

grid on, hold off

%assemble the results into a table (actually a matrix)

printf("===========Double precision results============== ")

printf(" del x forward backward central ")

Table = [del, forward_error, backward_error, central_error];

%print to screen

disp(Table)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%repeat everything in single precison

%select x value to evaluate derivative

x = single(x);

%evaluate the derivative

for i = single(1:20)

%Calculate delta x

del(i,1) = single(10^(-i));

%backward finite difference

backward(i,1) = single((f(x)-f(x - del(i,1)))/del(i,1));

%Forward finite difference

forward(i,1) = single((f(x + del(i,1)) - f(x))/del(i,1));

%Central Finite Difference

central (i,1)= single((f(x + del(i,1)) - f(x - del(i,1)))/(2*del(i,1)));

%Calculate the errors compared to exact derivative

backward_error(i,1) = single(abs(backward(i,1) - df(x)));

forward_error(i,1) = single(abs(forward(i,1) - df(x)));

central_error(i,1) = single(abs(central(i,1) - df(x)));

end

%Plot the error vs delta x for each method on the log scale

figure

hold on

plot(log10(del),log10(forward_error))

plot(log10(del),log10(backward_error))

plot(log10(del),log10(central_error))

xlabel('\Delta'), ylabel('Error')

title(' Error on Forward, Backward, and Central')

legend('Forward','Backward','Central')

grid on, hold off

printf("================================================= ")

%assemble the results into a table (actually a matrix)

printf("===========Single precision results============== ")

printf(" del x forward backward central ")

Table = [del, forward_error, backward_error, central_error];

%print to screen

disp(Table)

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!