Clear Filters
Clear Filters

A problem with a "Recursive Function"

1 view (last 30 days)
The following is a simple program using a "recursive function". As indicated, the code is designed to take the input value 5 initially and solve the recursive relation to get the expression for "a". The answer is supposed to be a=2*b^(1/3), but the program produces a=2b. Actually, it gives the same output which is 2b for any "n" (n>=5), and simply gives 1 for any "n" (provided that n<5). As you can see, It solves b=0.5*a*Recursive(m-2) (The value of m decreases by 2).
Are there any points I missed in understanding the logic I constructed? I would greatly appreciate it if you would help me to redisign the code so that the program can work properly.
n=5;
x=Recursive(n);
disp(x)
function r=Recursive(m)
syms a b
if(m<1)
r=1;
else
temp=0.5*a*Recursive(m-2);
eqn=b==temp;
E=solve(eqn,a);
r=E;
end
end

Accepted Answer

Alan Stevens
Alan Stevens on 10 Jul 2020
Edited: Alan Stevens on 10 Jul 2020
You could try the following:
syms a b
n=5;
eqn=b==Recursive(n);
a=solve(eqn,a);
disp(a)
function r=Recursive(m)
syms a
if(m<1)
r=1;
else
r=0.5*a*Recursive(m-2);
end
end

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!