Recurrence. Consider the following 3-term recurrence:
T_(0)(x)=1,
T_(1)(x)=x
T_(n)(x)=2xT_(n-1)(x)-T_(n-2)(x) for n>1
This recurrence is valid for any x.
(a) Create a Matlab (or python) function that computes Tn(x) iteratively with the recurrence, taking n and x as inputs. (Hint: modify fibonacci_direct2.m)
fibonacci_direct2.m
function F= fibonacci_direct2(n)
if n==0
F=0;
else
Fn1=0;F=1;
for k=2:n
Fn2=Fn1;Fn1=F;
F=Fn1+Fn2;
end
end
fibonacci_direct2.m is based off of this:
F_(0)=0,
F_(1)=1,
F_(n)=F_(n-1)+F_(n-2) for n> 1
fibonacci_direct2.m Generate Fibonacci number
function F = fibonacci direct2n) if n==0 F=0; else Fn1=0;F=1; fork=2:n Fn2=Fn1;Fn1=F; F=Fn1+Fn2; end end