A problem with a "Recursive Function"

1 次查看(过去 30 天)
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

采纳的回答

Alan Stevens
Alan Stevens 2020-7-10
编辑:Alan Stevens 2020-7-10
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by