0
$\begingroup$

I am trying to solve a 6-equation system with one self-built function. Problem is, 3 equation must be passed as argument while the other 3 are defined inside the function. I am quite new with Wolfram Mathematica and I don´t understand how I should pass arguments to properly build the equation system with the local variables inside the function.

    eqlin = {xE == O5[[1]], yE == O5[[2]], zE == O5[[3]]} /. 
    {Cos[\[Alpha]0 + \[Theta]1] -> C1, Sin[\[Alpha]0 + \[Theta]1] -> S1, 
    Cos[\[Theta]2] -> C2, Sin[\[Theta]2] -> S2, Cos[\[Theta]2 + \[Theta]3] -> C3,        
    Sin[\[Theta]2 + \[Theta]3] -> S3}

Here I define the 3 equations to be passed to the function. I previously defined O5[[1]], O5[[2]], O5[[3]] so mathematica gives me as output (when I evaluate this section of the code):

    {xE == -C1 (a1 - C2 L1 + C3 L2) + x0, yE == -(a1 - C2 L1 + C3 L2) S1, 
    zE == L1 S2 - L2 S3 + z1}

which is correct. Now I build a function to modify the answer (not important for my question, but I use it later):

    MinusPi[\[Alpha]_] := ArcTan[Cos[\[Alpha]], Sin[\[Alpha]]];

and then I define the main question:

    InverseCinem[x0_, z1_, a1_, \[Alpha]0_, L1_, L2_, xE_, yE_, zE_, eqlin_] := Module[
      {LinearSolution, C1, S1, C2, S2, C3, S3, temp},
      LinearSolution = NSolve[
        {eqlin[[1]], eqlin[[2]], eqlin[[3]], C1^2 + S1^2 == 1, 
        C2^2 + S2^2 == 1, C3^2 + S3^2 == 1},
        {C1, S1, C2, S2, C3, S3}];
      temp = {ArcTan[C1, S1] - \[Alpha]0, ArcTan[C2, S2], ArcTan[C3, S3] - ArcTan[C2, S2]} /. LinearSolution
      Map[MinusPi, temp]
    ]

then I call the function using arguments from a previusly built vector (no problem with this one, I used it somewhere else and it is correct):

     ss = N[InverseCinem[paramCost[[1]], paramCost[[2]], paramCost[[3]], 
        paramCost[[4]], paramCost[[5]], paramCost[[6]], 5, 0, 20, eqlin]]

but the answer is:

 {-1.0472 + ArcTan[C1$12808, S1$12808], ArcTan[C2$12808, S2$12808], -1.ArcTan[C2$12808, S2$12808] + ArcTan[C3$12808, S3$12808]}

Can someone help me? thanks a lot


For example:

f =x^2+y==3 (*function of argument x and y*) 

Funct[argument_] := Module[
{x,y}, Solve[{argument, x-y==-1},{x,y}]]

Funct[f]

This last command should not work because the x and y defined as local variables inside Module are not considered the same as the x and y used in f outside the Module. This is what I understood up to now about how Mathematica treats local and global variables. If this is right I suppose it must be pretty obvious for someone knowing how Mathematica works, but I have been working with Mathematica only for a few days and I find it waaaay more tricky than in other languages where managing local and global variables is easier.

$\endgroup$
15
  • $\begingroup$ I don't understand, where are you using InverseCinem after its definition? $\endgroup$
    – glS
    Dec 8, 2017 at 12:16
  • $\begingroup$ I copied the wrong line, is in the last code comment: ss=N[InverseCinem[...]] $\endgroup$
    – DJappo
    Dec 8, 2017 at 12:23
  • $\begingroup$ please edit the question to correct it then $\endgroup$
    – glS
    Dec 8, 2017 at 12:24
  • $\begingroup$ Ok I corrected the question. Any suggest about how to solve it? $\endgroup$
    – DJappo
    Dec 8, 2017 at 12:51
  • $\begingroup$ No, the problem is LinearSolution is {} in this case, so no replacement is made. $\endgroup$
    – user202729
    Dec 8, 2017 at 13:01

1 Answer 1

1
$\begingroup$

SOLVED: I was not able to find help from anyone nor to find a clear explanation of the problem on the internet, but I managed to understand Mathematica strictly separates local and global variables even if they have the same name and are in the same function. I modified the InverseCinem function to take only global variables (passed through argument) and now everything works properly. I will post here the right code and then close the question.

paramCost = {Cx0 = 20,Cz1 = 3,Ca1 = 2,CA0 = \[Pi]/3,CL1 = 20,CL2 = 10};

eqlin = {xE == O5[[1]], yE == O5[[2]], zE == O5[[3]]} /. {Cos[A0 + T1] -> C1, 
   Sin[A0 + T1] -> S1,Cos[T2] -> C2, Sin[T2] -> S2, 
   Cos[T2 + T3] -> C3, Sin[T2 + T3] -> S3}

With O5 previously defined, as in the original question.

varLin = {C1, S1, C2, S2, C3, S3}

eqAggiuntive = {C1^2 + S1^2 == 1, C2^2 + S2^2 == 1, C3^2 + S3^2 == 1}

TogliPi[A_] := ArcTan[Cos[A], Sin[A]];

Here the function which wasn´t working (now corrected):

cinemInv[parametri_, xObiettivo_, yObiettivo_, zObiettivo_, eqlin_, 
  eqAggiuntive_, var_] := Module[
  {LinearSolution, temp}, 
  x0 = paramCost[[1]]; (*uso costanti definiti a inizio script*)
  z1 = paramCost[[2]];
  a1 = paramCost[[3]]; 
  A0 = paramCost[[4]];
  L1 = paramCost[[5]];
  L2 = paramCost[[6]];
  xE = xObiettivo;
  yE = yObiettivo;
  zE = zObiettivo;
  LinearSolution = NSolve[
    {eqlin[[1]], eqlin[[2]], eqlin[[3]], eqAggiuntive[[1]], 
     eqAggiuntive[[2]], eqAggiuntive[[3]]}, {var[[1]], var[[2]], 
     var[[3]], var[[4]], var[[5]], var[[6]]}];
  temp = {ArcTan[var[[1]], var[[2]]] - A0, 
     ArcTan[var[[3]], var[[4]]], 
     ArcTan[var[[5]], var[[6]]] - ArcTan[var[[3]], var[[4]]]} /. 
    LinearSolution;
  Map[TogliPi, temp]
  ]

Before I created C1,S1...(the variable in which the system was being solved) inside the module, but that way the variable in the eqlin weren´t the same (even they had the same name) because the first were local and the other were global. Now everything is global and so NSolve is able to solve the system.

$\endgroup$
4
  • $\begingroup$ Although it appears that Mathematica strictly separates local and global variables, this is not true. This illusion is achieved by the kernel by first renaming the variables appearing in Module before proceeding with evaluation. Try this: Module[{var}, var]. $\endgroup$
    – QuantumDot
    Dec 8, 2017 at 15:31
  • $\begingroup$ This is interesting. But the point is: if a variable passed in the argument and a local one have the same name (or better: are defines with the same name) they are not considered a single variable, but two and so treated differently. Right? $\endgroup$
    – DJappo
    Dec 8, 2017 at 15:59
  • $\begingroup$ I don't think I understand what your question is. Would you kindly rephrase your question, perhaps, with an example? Thanks! $\endgroup$
    – QuantumDot
    Dec 8, 2017 at 16:51
  • $\begingroup$ I posted an example as an answer (not as a comment) for error, so take a look at that to understand my question $\endgroup$
    – DJappo
    Dec 9, 2017 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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