1

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[1] -> 5/2, 
  t$17013[2] -> 5/2, t$17013[3] -> 5/2}}

How Can I fix the rename of the local variables ?

developer2000
  • 165
  • 10
  • 1
    Use Block instead of Module. – Greg 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
  • @Szabolcs, yes of course, I remark that when i initilize anywhere x,y,z,t. They evaluated. It is amazing!! How can I use a local variables? – developer2000 Jan 16 '14 at 23:27
  • @developer2000 "Local" means that it is local to the Module: you can't use it outside of the Module. Mathematica has a quirk where you'll see these renamed x$123 symbols if you let them escape the Module, but just like in other languages, you should not try to use them outside their scope. It simply doesn't make sense to do so. (To be fair, there are uses for these symbols that escape from Modules but that's a more advanced use that I didn't want to confuse you with now.) – Szabolcs Jan 16 '14 at 23:31
  • @developer2000 To put it simply: you can either 1. decide to use these only inside the Module in which case Module will localize them and avoid conflict with global symbols of the same name. 2. or decide to use global symbols and not use Module at all. Looking at your code it seems to me that you don't actually need those symbols as global ones, so why don't you just return the numbers from the Module, but not the symbols? – Szabolcs Jan 16 '14 at 23:32
  • @Szabolcs, but as you see in my first question. I used Module – developer2000 Jan 16 '14 at 23:43

2 Answers2

2

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:

Mathematica graphics

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]

Mathematica graphics

(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 )

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • 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
0
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[1] -> 5/2, t[2] -> 5/2, t[3] -> 5/2}}
developer2000
  • 165
  • 10