0 votos

¿Cómo describir este fractal con una función recursiva?

Ya hice otra pregunta sobre una función para un fractal como este: enlace Y recibió una función recursiva bien escrita con la forma

double cantor(double x, double a, double b, unsigned int depth)
{
if (depth == 0) // bottom of recursion
   return 0;
else if (x <= a + (1.0/3)*(b - a)) // x in first third of interval
  return cantor(x, a, a + (1.0/3)*(b - a), depth - 1);
else if (a + (2.0/3)*(b - a) <= x) // x in last third of interval
  return cantor(x, a + (2.0/3)*(b - a), b, depth - 1);
else // middle third
  return 1;
}

¿Cómo debo modificarlo para dar este ¿Fractal?

1voto

tom Puntos 23

Si entiendo bien lo que intentas hacer, necesitas modificar sólo el valor de retorno final, que en tu última cláusula else debería ser "depth/totalDepth", en lugar de 1. Es decir, con Maple:

> cantor:=proc(x,a,b, depth)   if depth=0 then 0
> elif (x <= evalf(a + (1/(3))*(b - a))) then
> cantor(x, a, a + (1/(3))*(b - a), depth - 1);
> elif (evalf(a + (2/(3))*(b - a)) <= x) then
> cantor(x, a + (2/(3))*(b - a), b, depth - 1);
> else depth/totalDepth;
> fi; end:

Da (modulo un factor de escala):

with(plots);
totalDepth:=4;
for depth from 1 to totalDepth do
 plot('cantor'(x,0,1,depth),x=0..1);
 od;

enter image description here enter image description here enter image description here enter image description here

i-Ciencias.com

I-Ciencias es una comunidad de estudiantes y amantes de la ciencia en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X