代做MATH1013 COMPUTATIONAL MATHEMATICS, EXERCISES 4, 2024/25代做Java程序
- 首页 >> Java编程MATH1013 COMPUTATIONAL MATHEMATICS,
EXERCISES 4, 2024/25
Floating-point numbers (paper exercise, not for credit, do not submit).
Consider a 6-bit toy system in which each floating-point number x is represented by
x = (−1)s × m × 2 e−σ
where 1 bit is assigned to s, 3 bits to m, 2 bits to e, and where σ = 2.
So, m = 1.m0m1m2 in base 2, where each mi is either 0 or 1, and e = e1e0 in base 2.
(1) List all numbers which can be written in this form. (There should be 64 of them).
(2) In this scheme, which number represents 1/3?
In general, we want you to write your own functions, rather than import them from external modules. The template file contains all the modules and functions you are allowed to import — in this exer-cise sheet, just atan, sin, cos, sqrt and pi from the math module. Pleases don’t import any others!
Exercise 4A Write functions to perform. the following tasks efficiently. Do not change the function names in the template file.
(1) Take x and return x 3 − x 2 .
(2) Take two inputs x and y and return their sum and product, in that order. E.g. inputs 3, 4 should return 7, 12.
(3) Take a list of numbers and returns their arithmetic mean, geometric mean and harmonic mean, in that order. For example, the input [4,16] should return 10.0, 8.0, 6.4.
(4) Take an integer n and returns a list of the first n of the Narayana’s cows sequence Cn. (This is a variant of the Fibonacci numbers, defined by C1 = C2 = C3 = 1, and Cn = Cn−1 + Cn−3 for n ≥ 4.) For example, the input 5 should return [1,1,1,2,3].
(5) Take a pair r ≥ 0 and θ representing a point’s polar coordinates and return a pair x, y representing its Cartesian coordinates.
(6) Take a pair x, y representing a point’s Cartesian coordinates and return a pair r, θ representing its polar coordinates, where θ ∈ [0, 2π). Check that your function works for (x, y) in all quadrants.
Exercise 4B
(1) Write a function which takes a list of the coefficients of a polynomial
P(x) = a0 + a1x + a 2 2 + . . . + anx n
of arbitrary degree n, and a value of x0, and returns P(x0). You can use the function given in lectures, ensuring you understand how it works.
(2) Use the function to evaluate
(a) P1(x) = x 3 + x 2 + 5x + 1 at x = 2.
(b) P2(x) = 5 − x 2 at x = √ 3.
Are these answers exact? Explain why or why not.
(Use a print statements to show the evaluation of your function, and answer the question in a comment.)
(3) The Maclaurin series for the natural logarithm ln(1+x) is given by
for all x. Use the first five terms in this series in the Horner evalu-ation function at a suitable value of x to give an approximation of ln 3/2.
(4) (a) Use your Horner’s method function to evaluate the polynomial (x − 2)4 at the point x = 2.0001.
(b) Is this answer correct?
(c) Give brief reasoning for this answer.
(5) In week 3 we wrote a function to convert from binary to decimal. The efficiency of this function can be improved us-ing the same principle as Horner’s method. Write such a func-tion (horner ternary to dec) using the ideas of Horner’s method which takes a list containing 0s, 1s and 2s (representing a base- 3 number) and returns the corresponding decimal integer (so the input [1,2,0] returns the integer 15).
Advanced, not for credit.
If you didn’t do it above, write a function which uses Horner’s method to compute the derivative P ′ (x) of a polynomial P(x).
Write a function to calculate the indefinite integral f(x) = R P(x)dx, given that f(0) = A. i.e. your function also needs to determine the constant of integration. With P3 as above, f3(x) = R P3(x)dx, and A = 0, find f3(1) and f3(π).