Extra Matlab Programming Session - Week 3

  1. (Demonstration by the teacher on the LCD-projector)

    Review diagnostic test.

  2. (Demonstration by the teacher on the LCD-projector)

    Write a function fact(n) which implements the factorial function:

    $\displaystyle n! = \begin{cases}1& n = 0\\ 1 * 2 * ... * n& n > 0 \end{cases}$    

    Use an if-statement and a loop.

  3. Explain to your comrades what the following function does (without trying it in Matlab).

    function v2 = albert(v1)
    % albert(v1)
    %
    % Mystery function, explain what it does!
    % v1 and v2 are vectors
    
      % Make v2 an empty vector
      v2 = [];
      j = 1;
      for i = 1:length(v1)
        if v1(i) > 13
          v2(j) = v1(i);
          j = j + 1;
        end
      end
    

  4. Write a function sumseries1(N) which computes the sum of the N first terms in the sequence:

    $\displaystyle 1, \frac{1}{2}, \frac{1}{4}, \frac{1}{8}, ..., \frac{1}{2^N}$    

  5. Write a function sumseries2(tol) which computes the sum of the numbers in the sequence:

    $\displaystyle 1, \frac{1}{2}, \frac{1}{4}, \frac{1}{8}, ..., \frac{1}{2^N}$    

    and stops when the next term is less than tol. Use a while-loop.

  6. Write a function sumdivisible7(N) which computes the sum of all numbers from 0 to N which are divisible by 7. Hint: the rem(x, y)-function computes the remainder of $ \frac{x}{y}$ in integer division.



Johan Jansson 2004-09-14