Filter löschen
Filter löschen

A problem with a "Recursive Function"

1 Ansicht (letzte 30 Tage)
onsagerian
onsagerian am 9 Jul. 2020
Kommentiert: onsagerian am 10 Jul. 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

Akzeptierte Antwort

Alan Stevens
Alan Stevens am 10 Jul. 2020
Bearbeitet: Alan Stevens am 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

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by