Borrar filtros
Borrar filtros

A problem with a "Recursive Function"

1 visualización (últimos 30 días)
onsagerian
onsagerian el 9 de Jul. de 2020
Comentada: onsagerian el 10 de Jul. de 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

Respuesta aceptada

Alan Stevens
Alan Stevens el 10 de Jul. de 2020
Editada: Alan Stevens el 10 de Jul. de 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

Más respuestas (0)

Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by