A problem with a "Recursive Function"

3 visualizzazioni (ultimi 30 giorni)
onsagerian
onsagerian il 9 Lug 2020
Commentato: onsagerian il 10 Lug 2020
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

Risposta accettata

Alan Stevens
Alan Stevens il 10 Lug 2020
Modificato: Alan Stevens il 10 Lug 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

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by