0% found this document useful (0 votes)
34 views9 pages

Recursion3

Uploaded by

k.s.jagan2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views9 pages

Recursion3

Uploaded by

k.s.jagan2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Recursion

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:

Recursion- The pros and cons

•expressive & powerful

•difficult to grasp

•can lead to elegant formulation of algorithms

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

What is wrong with Recursive functions?.

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

Polynomial evaluation with Recursive functions?.

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

, and is the value of the polynomial .

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

Recursive Polynomial evaluation-3

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

Maximum value in an array

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=randi([1 5], 1,8)

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

function res = fact(n)


if n == 0 res = 1;
else
res = n * fact(n-1);
end
end

String Reversal reversing string, #1:

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

String Reversal reversing string, #2

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

towers(3, 'A', 'C','B')

move disk 1 from peg A to peg C


move disk 2 from peg A to peg B
move disk 1 from peg C to peg B
move disk 3 from peg A to peg C
move disk 1 from peg B to peg A
move disk 2 from peg B to peg C
move disk 1 from peg A to peg C

function [] = towers(n, frompeg, topeg, auxpeg)


if (n == 1)
fprintf('\t move disk 1 from peg %c to peg %c \n', frompeg, topeg);
else
towers(n-1, frompeg, auxpeg, topeg);

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

x1=[0 10 20]'; y1=[0 10 0]'; L1=5;


Sierpinsky(x1,y1,L1)

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

Playing with Circles

New Matlab command : viscircles([x, y], r )

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)

function drawCircles1(x, y, radius)


viscircles([x, y], radius ); % built in Matlab function
if (radius > 4)
drawCircles1(x + radius / 2, y, radius / 2);
drawCircles1(x - radius / 2, y, radius / 2);
% Draw circle itself twice.
% For every circle, a smaller circle is drawn to the left and the right.
end
end

clf
drawCircles2(0,0,20)

8
axis off

function drawCircles2(x, y, radius)


viscircles([x, y], radius);
if (radius > 1)
drawCircles2(x + radius / 2, y, radius / 2);
drawCircles2(x - radius / 2, y, radius / 2);
drawCircles2(x, y + radius / 2, radius / 2);
drawCircles2(x, y - radius / 2, radius / 2);
end
end

You might also like