In: Computer Science
Computing Assignment 1 You must upload both your code (to Assignment 1 scripts/codes) and your report (to Assignment 1 computing report). The assignment is due at 11:00pm. I have set the due time in Canvas to 11:05pm and if Canvas indicates that you submitted late, you will be given 0 on the assignment. Your computing report must be exactly 1 page. There will be a penalty given if your report is longer than one page. • Please read the Guidelines for Assignments first. • Keep in mind that Canvas discussions are open forums. • Acknowledge any collaborations and assistance from colleagues/TAs/instructor. Computing Assignment { Floating Point Arithmetic Required submission: 1 page PDF document and Matlab scripts uploaded to Canvas. 1. Part 1 - warmup Use Matlab and issue the following two commands on the command line: sin(0); sin(π). State your answer and explain the answer you see. This should be done in one or two sentences at the beginning of your report. 2. Part 2 Let 8 ≤ x ≤ 10 be an arbitrary number and n a nonnegative integer. In exact arithmetic, the following computation leaves x unchanged: 1 for i=1:n
2 x=5 log(x);
3 end
4
5 for i=1:n
6 x=exp(x/5.0);
7 end
However, in finite-precision arithmetic the results may be
dramatically different for large n.
The purpose of this assignment is to investigate the output of this
computation in Matlab for
various values of n and for x in the range 8 ≤ x ≤ 10.
Your conclusions should be explained in a one-page report. Your
report must include the
following:
(a) Representative plots of the output as a function of x, with
each plot corresponding to a
different value of n.
(b) A discussion of the smallest value of n after which the result
of the finite-precision
computation begins to differ from exact arithmetic
computation.
1
(c) A discussion of the limiting behaviour for large n.
(d) A brief explanation as to why computing in floating point
arithmetic leads to the results
you have found.
Partial code for this assignment can be found in the file FloatPt.m
on Canvas. I suggest
using this as your starting point. If you have questions about
Matlab or other aspects of
the assignment or course, then I strongly encourage you to attend
the tutorials and drop-in
workshops.
----------------Codes given----------------------
% MACM 316 - Homework 1 % Floating Point Arithmetic with logarithm function % Description: Performs n-fold logarithm followed by n-fold exponentiation % File name: FloatPt_log.m clear %format long; %this changes the number of decimal digits that DISPLAY n=30; st=0.001; % Define a stepsize x=8:st:10; % x is a row vector of numbers between 1.3 and 2.3 of increments st y=x; for i=1:n y=5*log(y); end %y(1) %this is how you print the 1st element of y to the screen %y(11) %y(101) %y(1001) %pause %this stops your program until you press a key for i=1:n y=exp(y/5.0); end %y(1) %y(11) %y(101) %y(1001) % Plot the output y versus the input x plot(x,y) %you can change the title and axis labels in this manner title(['Output of the Computation with n = ' num2str(n)],'fontsize',14) xlabel(['Input x'],'fontsize',12) ylabel(['Output y'],'fontsize',12)
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.
Updated code:
% MACM 316 - Homework 1
% Floating Point Arithmetic with logarithm function
% Description: Performs n-fold logarithm followed by n-fold
exponentiation
% File name: FloatPt_log.m
clear
%format long; %this changes the number of decimal digits that
DISPLAY
st=0.001; % Define a stepsize
for n=30:40
x=8:st:10; % x is a row vector of numbers between 8 and 10 of
increments st
y=x;
fprintf("Original Value of last element of y is
%f\n",y(end));
for i=1:n
y=5*log(y);
end
fprintf("Value of last element of y=log(x) for n = %d is
%.31f\n",n,y(end));
for i=1:n
y=exp(y/5.0);
end
fprintf("Getting back original value of last element for n = %d is
%.31f\n\n",n,y(end));
plot(x,y,'LineWidth',2,'DisplayName', ['n=' num2str(n)])
hold on
end
title(['Output of the Computation with different n values '])
xlabel(['Input x'],'fontsize',12)
ylabel(['Output y'],'fontsize',12)
legend('show','Location','NorthEastOutside');
Part 1:
Matlab works of floating-point arithmetic, 0 can be accurately represented, and sin(0) can also be exactly represented in Matlab. Some floating-point numbers cannot be represented exactly in binary forms like pi and the way Matlab calculates sin internally. So that's why we are seeing the very small but non zero result.
Part 2:
a)
b) As we can see for value o n=33 it starts getting shaky, and after that beyond n=37 we start getting direct result due to precision error.
c) and d) As we can see in the right figure as we increase the value of y due to increase in floating-point error we are deviating far away from the original value. It is because Matlab performs floating-point arithmetic and perform round off to present the result. However, when floating-point error increases too much due to results going beyond the precision we are getting too much deflection from the original value.