# Module and Local variable

Hello,

GetIntersectionPoint[p1_, p2_] :=
Module[{pts, vecs, n, vars, distance, sol, x, y, z, t}, (

pts = p2;
vecs = p1 - pts;
n = Length[pts];
vars = Array[t, n];
{distance, sol} =
Minimize[
Total[({x, y, z} - Transpose[pts + vecs vars])^2, 2], {x, y, z}~
Join~vars]

)]


I call this function with p1 and p2 where

    p1={{100, 100, 100},{100, 0, 100},{0, 100, 100}};
p2={{500/3, 500/3, 0},{500/3, 0, 0},{0, 500/3, 0}};


I obtain like this:

{0, {x$17013 -> 0, y$17013 -> 0, z$17013 -> 250, t$17013 -> 5/2,
t$17013 -> 5/2, t$17013 -> 5/2}}


How Can I fix the rename of the local variables ?

• Use Block instead of Module. – Chip Hurst Jan 16 '14 at 18:23
• @RiemannZeta, Thanks, it is going. – developer2000 Jan 16 '14 at 18:26
• Or remove the x,y,z,t from the local variables declaration. – bill s Jan 16 '14 at 18:26
• @bills, I am thinking also like you, but I am developping a large code and I declared x,y,z,t in another place in the code. So if I do as you think, values of x,y,z,t will be change. – developer2000 Jan 16 '14 at 18:30
• Do you actually need to return the symbols x,y,z,t from this function? If yes, then there's no point using Module and localizing them. You will need to make sure they have no values anyway, otherwise they will be evaluated to their values as soon as they're returned. If no, then get rid of them inside the Module, and only return the numerical result. Using Block doesn't really help here: if the symbols have values, they'll be evaluated as soon as they're returned from the Block. – Szabolcs Jan 16 '14 at 23:17

The correct solution is to do it as Mathematica own functions do it, which is to pass the symbols you want to appear in the resulting expression as part of the call parameters.

For example, look at DSolve signature: The y and x are in the user context, and they are passed in to DSolve, so that DSolve can use them to build the expression with. That is why you do not see DSolve result having those \$ in the solution it returns.

So, for your case, the call will become

 getIntersectionPoint[p1_, p2_, x_, y_, z_, t_] :=
Module[{pts, vecs, n, vars, distance, sol},.....];

getIntersectionPoint[p1, p2, x, y, z, t] (and it is not a good idea to use UpperCaseFirstLetterInFunctionName since that can make the reader think it is part of Mathematica own commands. lowerCaseIsBetter )

• Your solution can be create a problem if you define x,y,z,t anywhere in the code. – developer2000 Jan 16 '14 at 22:31
• About function name, I know that. – developer2000 Jan 16 '14 at 22:33
• @developer2000 it is not "my solution". It is the solution used by Mathematica itself for its own commands as I explained. The idea is to pass those symbols to the function as well. However, you are not obliged to use this method if you do not want. use global variables or Blocks or any other method that you prefer. I just do not think those are the correct solution to this problem. – Nasser Jan 16 '14 at 22:35
• if you use x,y,z,t anywhere, you will get an error. x = 0; y = 0; z = 0; t = 50; getIntersectionPoint[p1, p2, x, y, z, t]  – developer2000 Jan 16 '14 at 22:55
• Clear[x,y,z,t]; getIntersectionPoint[p1, p2, x, y, z, t] you are basically showing a problem that will show up with using Mathematica own functions. Try x=0; DSolve[y'[x]==x,y[x],x] and see what happens. Any way, as I said, please do not use this method. Use anything you like. – Nasser Jan 16 '14 at 22:58
use Block instead of Module

GetIntersectionPoint[p1_, p2_] :=
Blcok[{pts, vecs, n, vars, distance, sol, x, y, z, t}, (

pts = p2;
vecs = p1 - pts;
n = Length[pts];
vars = Array[t, n];
{distance, sol} =
Minimize[
Total[({x, y, z} - Transpose[pts + vecs vars])^2, 2], {x, y, z}~
Join~vars]

)]
GetIntersectionPoint[p1, p2]

{0, {x -> 0, y -> 0, z -> 250, t -> 5/2, t -> 5/2, t -> 5/2}}

• This is just a workaround. It is not a good solution to avoid using Module. – Nasser Jan 16 '14 at 22:21