Recursion3
Recursion3
Recursive functions:
Functions that are defined in terms of themselves are called recursive functions, or recursively defined
functions. This is not as strange as it sounds - a number ofwell-known functions can be defined in recursive
form, and we are all familiar with so-called recursion relations (or difference equations), in which each term in a
sequence is defined in terms of previous terms. Some examples:
•difficult to grasp
MATLAB programming language, and most other high-level languages, allow functions to be recursively
defined, i.e. to use their function values in their own definitions, as long as there is sufficient information for
the function values to eventually be resolved. As an example, consider the Fibonacci sequence: We denote the
nth term by F(n) and write a function for F in recursive form
% Example1.
% Fibonacci series 1,1,2,3,5,8,.......
F(10)
ans = 89
function y=F(n)
if or (n==0, n==1)
y=1;
else
y=F(n-1)+F(n-2);
end
end
If we then ask for F(10), MATLAB executes the function F and discovers it needs to know F(9) and F(8) - then it
tries to evaluate F(9) and F(8) and discovers it needs to know F(7) - it follows this chain of function evaluations
until it finds F(2)=F(1)+F(0)=2 and then it works its way back up all the way to F(10). The number of unresolved
function calls that MATLAB will allow at any one time is large (equal to 500, but may be changed by the user)
1
but each such function call uses memory and you can run out of memory (not to mention have a very slow
running program) if there are too many. Try calculating F(800) with the program in the previous slide and see
what happens.
More customary (and more efficient) way to calculate F(n) would be:
F1(10)
ans = 89
function[y]=F1(n)
if or(n==0,n==1) y=1;
else
a=1;b=1;
for k=2:n
y=b+a; % ‘a and b are last two values in order
a=b;
b=y;
end
end
end
In computer programming, recursive calculations are the most natural form in which to express functions. When
we evaluate polynomials by nested multiplication we calculate with the recursion
Of course when we implement this, we just write p = because we have no need to retain the
intermediate terms of the sequence.
mypolyval([ 1 2 3 4],0.5);
3.2500
function y=mypolyval(c,x)
%c is the array of coefficients, from lowest to highest power
% x can be array of values at which evaluation is to be carried out
% let us assume x is scalar
n=length(c);
y=c(n);
for i=(n-1):-1:1
y=c(i)+x*y;
end
disp(y)
end
2
Polynomial evaluation-2
mypolyval1([ 1 1 1 1],0:0.1:1);
function y=mypolyval1(c,x)
%c is the array of coefficients, from lowest to highest power
% x can be array of values at which evaluation is to be carried out
% let us assume x is a vector of values
clf
n=length(c);
y=c(n)*ones(size(x)); % y is an array of values
for i=(n-1):-1:1
y=c(i)+x.*y; % y is an array of values
end
%plot(x,y)
end
polyvalr([ 1 2 3 4],0.5)
ans = 3.2500
function y=polyvalr(p,x)
%recursive formulation of polynomial evaluation
%p is the array of coefficients
if length(p)==0
y=zeros(size(x));
else y=p(1) + x.*polyvalr(p(2:end),x) ;
end
end
The maximum value in an array is the maximum of the first element and the maximum of all elements after the
first. This gives rise to
x = 1×8
1 4 2 4 4 4 3 1
y=maxr(x)
y = 4
function y=maxr(x)
%find maximum value of array x
if length(x)==1
y=x;
3
else
if x(1)>maxr(x(2:end))
y=x(1);
else
y=maxr(x(2:end));
end
end
end
Factorial
x=fact(10)
x = 3628800
s='ABCDE';
reverse1(s)
ans =
'EDCBA'
function s = reverse1(s)
if length(s) >= 1
s = [ s(end) reverse1(s(1:end-1)) ]; % Appending array
end
end
s='ABCDE';
y=reverse2(s)
y =
'EDCBA'
function s = reverse2(s)
if length(s) >= 1
s = [reverse2(s(2:end)) s(1) ]; % Appending array
end
end
4
Palindrome
s='ABCD'
s =
'ABCD'
y=reflect1(s)
y =
'ABCDDCBA'
function s = reflect1(s)
if length(s) >= 1
s = [ s(1) reflect1(s(2:end)) s(1) ]; % Appending array
end
end
5
fprintf('\t move disk %d from peg %c to peg %c \n', n,frompeg,topeg);
towers(n-1,auxpeg,topeg,frompeg);
end
end
Extended GCD
[a1,b1,c1]=egcd(35,15)
a1 = 5
b1 = 1
c1 = -2
function [ g, x, y]=egcd(a, b)
if a == 0
g=b;x=0;y=1;
else
[g,x1,y1]= egcd(mod(b,a), a);
x=y1-(floor(b/a))*x1;
y=x1;
end
end
6
function Sierpinsky(x,y,L)
% x and y are 3-vector s.
% L represent levels
if L==0
fill(x,y,'w','linewidth',1.5)
hold on
else
% A subdivision is called for...
% Determine the side midpoints (a(1),b(1)), (a(2),b(2)), and (a(3),b(3))
a = [(x(1)+x(2))/2 (x(2)+x(3))/2 (x(3)+x(1))/2];
b = [(y(1)+y(2))/2 (y(2)+y(3))/2 (y(3)+y(1))/2];
% Color the interior triangle blue...
fill(a,b,'b','linewidth',1.5)
hold on
% pause(0.5)
% Apply the process to the three "outer" triangles...
Sierpinsky([x(1) a(1) a(3)],[y(1) b(1) b(3)],L-1)
Sierpinsky([a(1) x(2) a(2)],[b(1) y(2) b(2)],L-1)
Sierpinsky([a(3) a(2) x(3)],[b(3) b(2) y(3)],L-1)
end
end
7
clf
drawCircles(0,0,20)
function drawCircles(x, y, r)
viscircles([x, y], r ); % built in Matlab function
if (r > 4)
r =r* 0.75;
hold on
drawCircles(x, y, r)
end
end
clf
drawCircles1(0,0,20)
clf
drawCircles2(0,0,20)
8
axis off