Is there someone who can explain why this manipulate does not work

v = {a, b, c}; s = sn;
amapp[v_, s_] := Module[{vv = v, ss = s}, d = Total[vv]/ss;
   q = N[vv/d];
  qq = \[LeftFloor]q\[RightFloor];
  res = q - qq;(*reste*)
  raa = ss - Total[qq];(*reste à attribuer*)
  raa1 = Table[1, {i, Length[vv]}];
  res1 = Table[
    If[MemberQ[Ordering[res, raa, #1 > #2 &], i], qq[[i]] + raa1[[i]],
      qq[[i]]], {i, Length[qq]}]]
 amapp[v, s], {a, 1, 1000}, {b, 1, 1000}, {c, 1, 1000}, {sn, 1, 1000}]

You can see the problem with:

Manipulate[{v, s}, {a, 1, 1000}, {b, 1,1000}, {c, 1, 1000}, {sn, 1, 1000}]

a,b,c remain unevaluated with your v and s defined outside your Manipulate. Why? Because the a,b,c in your Manipulate are locally scoped. Can see e.g. w/ Manipulate[{Hold[FullForm[a]], FullForm[v]}, {a, 1, 1000}]. Sometimes using Hold[FullForm[.]] can help you figure out the actual variable names at play are.

Can resolve e.g. (as Kuba comments) with:

Manipulate[amapp[{a, b, c}, sn], {a, 1, 1000}, {b, 1, 1000}, {c, 1, 1000}, {sn, 1, 1000}]

Or if you insist on defining a v and s, do so within the Manipulate:

Manipulate[v = {a, b, c}; s = sn; 
 amapp[v, s], {a, 1, 1000}, {b, 1, 1000}, {c, 1, 1000}, {sn, 1, 1000}]
  • $\begingroup$ Sometimes I feel so stupid. Thanks, Both of you. But I must underline that what, at the end, I want is to have the length of v variable. Do you know how to do this ? $\endgroup$ – cyrille.piatecki Aug 24 '17 at 9:25
  • $\begingroup$ Let me see if I understand. You want to manipulate the length and content of $v$? i.e. have a slider for size of your vector $v$ and a separate slider for each of those $v_1, \ldots , v_n$? $\endgroup$ – John Joseph M. Carrasco Aug 24 '17 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.