Introduction to MATLAB Exercise 3

Notice: for more complicated function (that can not be defined using @), you have to create a file and save it. For example, you have to create the file quadroot.m and save it (in the same directory where you use it).
  1. The formula x±=a±a242 for the two roots of the special quadratic function x2ax+1=0 is well-known. When the absolute value of a is large, one of the root may not be accurate as expected. For example, if a=1e8, the smaller root computed from (a-sqrt(a^2-4))/2 is 7.450580596923828e-09 (using the default double precision in MATLAB), which is wrong in the first digit. In stead, we should use the equivalent formula x=2a+a24 to avoid cancellation in the significant digits between a and a24. Similarly, if a is negative, the two roots are computed from x+=2aa24,x=aa242. Create a function quadroot by continuing the following block of code (save it as a matlab file "quadroot.m"). You can assume that the input argument a satisfies a2>4, and there is no need to discuss the case when a24.
    function y=quadroot(a)
    %QUADROOT Roots of the quadratic function x^2-a*x+1=0
    % quadroot(a) returns the two roots of the quadratic function x^2-a*x+1=0
    % such that both roots are accurate even if the absolute value of a is large
  2. The number 2 is irrational, but can be approximated through various iterations using elementary operations. Compute xn and yn which are defined by xn=xn12+22xn1,x0=1 and yn=yn1+2yn1+1,y0=1. Find the number of iterations needed such that |xn2| or |yn2| is less than 1012. You can just use the same variable during the iteration, like x = (x^2+2)/2/x;, instead of saving all the history. Which iteration is faster (you can counter the number of steps, by adding the counter by one inside each iteration)?
  3. During the lecture, the following function is used to determine whether the input is a leap year:
    function isleap(year)
    %% for the input year, determine whether this is a leap year

    if mod(year,4)~=0
      disp('This is not a leap year');
    elseif mod(year,100)~=0
      disp('This is a leap year');
    elseif mod(year,400)==0
      disp('This is a leap year');
    else
      disp('This is not a leap year');
    end
    This function can be written in alternative ways to make it clear, using the following figure (the shaded set indicates leap year).

    You can test your function by trying the following leap years (2000 and 2016) and non-leap years (1900 and 2015).
  4. Approximate π by tiling a quarter disk. There are many ways to approximate the irrational number π, for instance by counting the number of tiles (with unit size) of the following quarter disk with radius n (assumed to be an integer).
  5. Collatz Conjecture. Consider the following operation on an arbitrary positive integer from n to f(n) nf(n)={3n+1,if n is odd,n/2,if n is even. That is, if the number is even, divide it by two; if the number is odd, triple it and add one. For example, by applying this operation successively, we get 3  10  5  16  8  4  2  1  4  2  1  . It was conjectured that, starting with any positive integer, this operation eventually ends up with the periodic sequence 1  4  2  1  4  2  1. Write a function to determine the number of steps such that the number 1 first appears in the sequence.
    function counter = collatz(N)
    %% For input positive integer N, return of the number of steps, such that "1" first appears
    counter = 0; % the initial value
    n = N;
    while (n ~= 1)
      % complete this part, perform one step of the iteration on n, and increase the counter by one
    end
    You can test your function, for the following values: (1) the output of collatz(1) should be 0; (2) the output of collatz(2) should be 1; (3) the output of collatz(3) should be 7. Some small numbers may take many iterations to terminate. For example, the output for collatz(27) should be larger than 100.