[SOLVED] CS-SE4X03 Assignment 1

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon A1-96xhw1.zip (164.1 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
Rate this product
  1. Problem 1  Explain the output of this Matlab code
    a = -1.10714946411818e+17;
    b = -8.039634349988262e-01;
    c = 1.107149464118181e+17;
    a+b+c
    (a+b)+c
    a+(b+c)
    (a+c)+b
    a+(c+b)
    

Consider a floating-point (FP) system with 4 decimal digits and

Problem 2
rounding to the nearest. Give an example for each of the following:

a. (a+b)/2∈/[a,b]

1

Do Not Share this document

b. a+(b+c)̸=(a+b)+c

c. a∗(b∗c)̸=(a∗b)∗c

where a, b, c are FP numbers in this system.
[4 points] Let a and b be IEEE-754 FP numbers and a ≤ b. Can (a + b)/2 ∈/ [a, b] occur?

Explain.
Hint: for x ≤ y, fl(x) ≤ fl(y).

Problem 3 [8 points] Consider the series expansion of ex: x x2x3

e =1+x+2!+3!+···. Write Matlab functions as follows

function s = expsum1(x) computes an approximation to ex by adding terms until the sum does not change.

function s = expsum2(x) returns expsum1(x) for x ≥ 0 and 1/expsum1(−x) for x < 0. function s = expsum3(x) accumulates the positive and negative terms separately and

then adds the corresponding sums.
Write a main program main_expsum.m that calls these functions for

x = −20,−15,−5,−1,1,5,15,20 and outputs for each function results in the form

x -20.0 -15.0

. . .

20.0

   accurate value
2.061153622439e-09
3.059023205018e-07
  approx. value
6.138259738609e-09
3.059300523747e-07
abs. error  rel. error

(If y is an accurate value and y􏰃 is an approximation, the absolute (abs.) error is |y − y􏰃| and the relative (rel.) error is |y − y􏰃|/|y|.)

a. [6 points] Explain the accuracy of expsum1, expsum2, expsum3.
b. [2 points] Can expsum3 produce accurate results for x < 0? Explain.

Submit

• Your Matlab files to Avenue.
• PDF should contain Matlab code, output, and discussion.

2

4.08e-09
2.77e-11
1.98e+00
9.07e-05

Do Not Share this document

Problem 4 [6 points] We are not going to do C/C++ in this course, but this is a good exercise to learn about benchmarking and performance.

Read about
• the LINPACK benchmark, https://en.wikipedia.org/wiki/LINPACK_benchmarks.

• long double and __float128, https://en.wikipedia.org/wiki/Long_double

I have modified a bit the original benchmark, see the file linpack.cc. In Linux, save the given files linpack.cc and makefile and type make. This should produce the output file benchmark.out.

  • Modify linpack.cc, see lines 48 and 52, so it can produce results for these two datatypes.
  • Then uncomment in the makefile

    TESTS = single double #longdouble float128 all: ${TESTS}

            ./single > benchmark.out
            ./double >> benchmark.out
            #./longdouble >> benchmark.out
            #./float128 >> benchmark.out
    

    Executing make should produce results for the four data types. Submit in the PDF

    • [4 points] the output benchmark.out
    • [2 points] discussion about the performance of these data types.

    Problem 5 [10 points] Given a scalar function f, from
    f(x + h) = f(x) + f′(x)h + O(h2),

    we can approximate the first derivative
    f′(x)≈g1(x,h):= f(x+h)−f(x),

where the error in this approximation is O(h). From

f(x ± h) = f(x) ± f′(x)h + f′′(x)h2 + O(h3), 2

f′(x)≈g2(x,h):= f(x+h)−f(x−h) 2h

3

h

we can approximate

where the error is O(h2).

Do Not Share this document

  1. [4 points] Let f(x) = xex and x0 = 1. Write a Matlab program that computes the errors |f ′ (x0 ) − g1 (x0 , h)| and |f ′ (x0 ) − g2 (x0 , h)| for each h = 10−k , k = 1, 2, . . . , 16.

    Using loglog, plot on the same plot these errors versus h.

  2. [2 points] For what h is the minimum of the errors achieved in each of these two approx-

    imations?

  3. [4 points] Explain the behaviour of these errors. In particular, discuss the role of the cancellation and truncation errors.

Submit

  • Avenue: your Matlab code. Provide a main program with name main_deriv.m such that when it is executed it produces the required plot and the two values for h.
  • PDF: plot, the two values for h, and explanations.
    Problem 6 [6 points] Consider three methods for computing the sum of n floating-floating

    point numbers:

  1. summation is in decreasing order of magnitude
  2. summation is in increasing order of magnitude
  3. using Kahan’s summation algorithm
    see https://en.wikipedia.org/wiki/Kahan_summation_algorithm

Apply these three methods to compute the sum 􏰂n=10000 1/i. How would you compute an i=1

accurate result for this sum? Using such a result, report the errors in a., b., c.. in the form

decreasing order increasing order Kahan’s sum

error error error

(Report in the format format short e)
Name your main program main_sum.m. When executed, it should produce the above

output.

  • A1-96xhw1.zip