# passing system of equations as argument

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[], yE == O5[], zE == O5[]} /.
{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[], O5[], O5[] 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[], eqlin[], eqlin[], 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[], paramCost[], paramCost[],
paramCost[], paramCost[], paramCost[], 5, 0, 20, eqlin]]


 {-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.

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

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[], yE == O5[], zE == O5[]} /. {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[]; (*uso costanti definiti a inizio script*)
z1 = paramCost[];
a1 = paramCost[];
A0 = paramCost[];
L1 = paramCost[];
L2 = paramCost[];
xE = xObiettivo;
yE = yObiettivo;
zE = zObiettivo;
LinearSolution = NSolve[
{eqlin[], eqlin[], eqlin[], eqAggiuntive[],
eqAggiuntive[], eqAggiuntive[]}, {var[], var[],
var[], var[], var[], var[]}];
temp = {ArcTan[var[], var[]] - A0,
ArcTan[var[], var[]],
ArcTan[var[], var[]] - ArcTan[var[], var[]]} /.
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.

• 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]. Dec 8, 2017 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? Dec 8, 2017 at 15:59
• I don't think I understand what your question is. Would you kindly rephrase your question, perhaps, with an example? Thanks! Dec 8, 2017 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 Dec 9, 2017 at 0:27