Mth20513051 introduction to computational mathematics


Introduction to Computational Mathematics Assignment - Matlab and Finite Precision Arithmetic

1. Matlab: prime numbers.

Write a function sol=myprime(n), which returns 1 if natural number n is prime and 0 otherwise. Note that, by convention, 0 and 1 are not considered prime numbers. Use the command mod to check whether a number divides n. Test your code with n=0, n=1, n=5 and n=25 and report the results.

2. Matlab: comparing elements of two matrices.

Write a function num=howmanybigger(A,B) that does the following. If the dimensions of the matrices A and B do not match, it uses the error command to break with an appropriate error message. Otherwise it returns the number of elements in A which are bigger than the elements in B at the corresponding positions.

Submit a script test howmanybigger.m that tests your code with

2266_figure.png

and report the results.

3. Floating point number system.

On a computer with a binary number system, the distance between 7 and the next larger floating point number is 2-12. What is the (exact) distance between 71 and the next largest floating point number?

4. Avoiding catastrophic cancellation.

Consider the expression - zA = 1/(√(1 + x2) -√(1 - x2)).

(a) Explain why the formula for zA is susceptible to catastrophic cancellation errors for x close to 0.

(b) Use reformulation to find an alternative expression zB for expression zA in a way that avoids catastrophic cancellation for x close to 0. (Hint: p2 - q2 = (p-q)(p+q).)

(c) Illustrate how algorithm B that uses the reformulated expression zB you propose improves the accuracy for calculations in the floating point system F[b = 10, m = 4, e = 1] with rounding, compared to algorithm A based on the original expression zA. Use x = 1.234 · 10-3.

(c1) Find a highly accurate approximate value for zA(x) = zB(x) (for example by calculating the expression in Matlab; for our purposes it does not matter much whether you use expression zA or zB to compute this highly accurate value).

(c2) Compute the machine epsilon for the floating point system (with rounding!).

(c3) Find the value of z¯A, obtained by evaluating the original expression zA in the floating point system.

(c4) Find the value of z¯B, obtained by evaluating the reformulated expression zB in the floating point system.

(c5) Compute the relative errors in the approximations of zA and zB relative to the highly accurate value for zA = zB,

|δzA| = |(zA - z¯A)/zA|

and

|δzB| = |(zB - z¯B)/zB|.

Compare the two relative errors to each other, and compare them to the machine epsilon. Explain how this illustrates that the alternative expression succeeds in avoiding the catastrophic cancellation step, showing that algorithm B is a numerically stable algorithm to compute zA = zB, while algorithm A based on the original expression is numerically unstable.

5. Matlab: recursion

It can be convenient to solve a problem recursively. A recursive function is a function that calls itself. The following function sums up the numbers from m to n in an unusual (recursive) way.

function rs=recursivesum(m,n)

if (n>m)

rs=m+recursivesum(m+1,n)

elseif (n==m)

rs=m;

else

rs=0;

end

end

(a) Copy this code into function recursivesum.m. Report and explain what gets printed to the screen when you invoke recursivesum(1,5), and why it is printed in that order.

(b) Next, write a function nf=recursivefactorial(n), which computes the factorial of n in a recursive way.

(c) Test your code with n=0, n=1 and n=5 and report the results.

Request for Solution File

Ask an Expert for Answer!!
MATLAB Programming: Mth20513051 introduction to computational mathematics
Reference No:- TGS02878598

Expected delivery within 24 Hours