output the nth term of the fibonacci sequence in matlab

Here's a MATLAB function that uses recursion to output the nth term of the Fibonacci sequence:

main.m
function result = fibonacci(n)

    if n == 0
        result = 0;
    elseif n == 1
        result = 1;
    else
        result = fibonacci(n-1) + fibonacci(n-2);
    end

end
176 chars
12 lines

To use this function and output the nth term, simply call it with the desired value of n:

main.m
nthTerm = fibonacci(10) % outputs the 10th term of the sequence, which is 55
77 chars
2 lines

gistlibby LogSnag