Message Boards Message Boards

Problems with recursion

Posted 9 years ago

Hello all, with the news that I have problems with recursion , because the functions I built work perfectly and have checked in detail and work perfectly , I share my file Mathematica hoping someone of you tell me I'm doing wrong , thanks your help.

Any help is welcome :)

Attachments:
POSTED BY: Luis Ledesma
5 Replies
Posted 9 years ago

The purpose of your calculations may be clear to you, but it is perhaps not clear to others.

If you could include a few sentences clearly describing what the notebook is supposed to produce and include a sentence for each function stating what the possible inputs can be and another sentence stating what the function should produce that might help you and others more clearly see whether the code is correct or not. Please try to make those descriptions very simple and clear and still describe all possible cases for input and output.

I assume the function named average did not expect to be given input that would calculate a result 0/0. I cannot determine whether the input is incorrect or whether a result 0/0. should perhaps return 0.

I assume the function named insert did not expect to be given a first argument {}. I cannot determine whether the input is incorrect or whether when given a first argument of {} that it should return the value of grid unchanged.

POSTED BY: Bill Simpson
Posted 9 years ago

Bill Simpson thank you very much for taking time to help me solve my problem , but still do not understand why I keep being wrong, even when I made the changes you suggest me , I hope to make improvements or explain in more detail how the recursion should work, because I am currently annoyed with this problem , any help I can clear my thoughts, thanks in advance ;)

POSTED BY: Luis Ledesma
Posted 9 years ago

Modify your average function

average[a_, b_, c_, d_, main_] := Module[{total = 0, count = 0, x},
  If[a =!= Null, x = main[[a[[1]], a[[2]]]]; If[x != 0, total = total + x ; count++]];
  If[b =!= Null, x = main[[b[[1]], b[[2]]]]; If[x != 0, total = total + x ; count++]];
  If[c =!= Null, x = main[[c[[1]], c[[2]]]]; If[x != 0, total = total + x ; count++]];
  If[d =!= Null, x = main[[d[[1]], d[[2]]]]; If[x != 0, total = total + x ; count++]];
  If[count==0, Print["average[",a,",",b,",",c,",",d,",",main,"] -> Error total/N[count]==",total,"/",N[count]]];
  Ceiling[total/N[count]]
  ]

Modify your insert function

insert[miniums_, grid_, x_, y_] := Module[{maya = grid, profu, profd, noimp, r, j},
  If[Length[Dimensions[miniums]]<2, Print["insert[",miniums,",",grid,",",x,",",y,"] -> Error miniums==",miniums]];
  profu = Dimensions[miniums][[1]];
  profd = Dimensions[miniums][[2]];
  For[r = 1, r <= profu, r++,
    For[j = 1, j <= profd, j++,
     maya[[x + r, y + j]] = miniums[[r, j]];
  ]];
  maya]

Remove the assignment to main inside your dsfractal function. Parameters to functions may not have their values changed inside the function. It does not appear that you are using the result of that assignment and so perhaps no other changes are needed to store the value returned from the call to your insert function.

Now when you evaluate the notebook you should see error messages indicating what needs to be corrected.

POSTED BY: Bill Simpson
Posted 9 years ago

It is a good observation Frank, but has a simple explanation, I have not put with capitalize insert because this is a function I defined and is not a pre-built function from mathematica, thank you for the observation

POSTED BY: Luis Ledesma

taking a quick look at your code, I see you didn't capitalize the first letter of the Insert function.

POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract