0

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.

Kuba
  • 136,707
  • 13
  • 279
  • 740
DJappo
  • 11
  • 4
  • I don't understand, where are you using InverseCinem after its definition? – glS Dec 08 '17 at 12:16
  • I copied the wrong line, is in the last code comment: ss=N[InverseCinem[...]] – DJappo Dec 08 '17 at 12:23
  • please edit the question to correct it then – glS Dec 08 '17 at 12:24
  • Ok I corrected the question. Any suggest about how to solve it? – DJappo Dec 08 '17 at 12:51
  • No, the problem is LinearSolution is {} in this case, so no replacement is made. – user202729 Dec 08 '17 at 13:01
  • (at least for my set of paramCost). Just Print[LinearSolution] and check if it's empty. – user202729 Dec 08 '17 at 13:03
  • Yes, there seems to be a problem with NSolve inside the Module, because no solution is found (which is wrong, because there should be 4 solutions) – DJappo Dec 08 '17 at 13:03
  • Yes, I have the same result and LinearSolution is empty {} – DJappo Dec 08 '17 at 13:04
  • Try using Solve or Block? But no, I think if Mathematica said that the solution set is empty, it is probably actually empty. – user202729 Dec 08 '17 at 13:04
  • If you use Block you find 5 valid solutions, that is the strange part – DJappo Dec 08 '17 at 13:22
  • Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Dec 08 '17 at 14:12
  • To mark a question as being solved, the click the green tick on the answer that solves the problem rather than renaming the title of the question. Welcome to Mathematica.SE. – QuantumDot Dec 08 '17 at 15:33
  • Since I wrote the answer that solved the problem I cannot mark it as right before 3 days, so I decided for changing the title – DJappo Dec 08 '17 at 15:55
  • The reason for the 3-day wait is so that others can have a chance of providing a better answer. – QuantumDot Dec 08 '17 at 16:40
  • I know, I just didn't want people to waste time on a stupid problem I had already solved – DJappo Dec 08 '17 at 23:50

1 Answers1

1

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.

DJappo
  • 11
  • 4
  • 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]. – QuantumDot Dec 08 '17 at 15:31
  • 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? – DJappo Dec 08 '17 at 15:59
  • I don't think I understand what your question is. Would you kindly rephrase your question, perhaps, with an example? Thanks! – QuantumDot Dec 08 '17 at 16:51
  • I posted an example as an answer (not as a comment) for error, so take a look at that to understand my question – DJappo Dec 09 '17 at 00:27