Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"$" symbol in mathematica output

I am having some slight difficulty with the following code:

Lagrange[list_] := 
  Module[{points = list, length, k, j, m, x, g}, 
    length = Length[points]; 
    k = length - 1; 
    f = Sum[points[[j + 1,2]]*Product[If[j != m, (x - points[[m + 1,1]])/
          (points[[j + 1,1]] - points[[m + 1,1]]), 1], {m, 0, k}], {j, 0, k}]; 
    g = FullSimplify[Expand[f]]; 
    Return[f]]

The output I get is:

Out[101]= 0. -1.85698 (-1.5+x$26810) (-0.75+x$26810) (0. +x$26810) (0.75+x$26810)
         +0.490717 (-1.5+x$26810) (-0.75+x$26810) (0. +x$26810) (1.5 +x$26810)
         -0.490717 (-1.5+x$26810) (0. +x$26810) (0.75 +x$26810) (1.5 +x$26810)
         +1.85698 (-0.75+x$26810) (0. +x$26810) (0.75 +x$26810) (1.5 +x$26810)

My concern is with these "$" symbols. I don't know what they mean, I can't find documentation on them, and they are preventing the plotting of this polynomial.

like image 599
Matthew Kemnetz Avatar asked Dec 07 '11 05:12

Matthew Kemnetz


People also ask

What does := mean in Mathematica?

to clear a value) x == val — test equality or represent a symbolic equation (!= for unequal) lhs := rhs — function etc. definition.

How do you write Phi in Mathematica?

\[Phi] Unicode: 03D5. Aliases: ph , phi , f . Greek letter.


2 Answers

The $ in your output is from the unique variable generated by the lexical scoping of Module (see the More Information part of mathematica/ref/Module). This is why I made my LagrangePoly function accept the symbol that the polynomial is meant to be in. I used LagrangePoly[list_, var_:x], which defaults to the global symbol x.

A simple example of the problem is

In[1]:= Module[{x}, x]    
Out[1]= x$583

The number in the "local" variable x$nnn comes from the global $ModuleNumber.

If you don't understand this, then you should probably read the tutorial Blocks Compared with Modules.

like image 68
Simon Avatar answered Sep 17 '22 02:09

Simon


In your code, x is a local variable. However you are returning an expression containing x. The purpose of localized variables is that they shouldn't appear outside their context, which is the Module in this case.

Consider this simple example:

adder[x_] := Module[{y}, Return[x + y]]

adder[2] gives: 2 + y$1048

A good practice is to recognize that our function adder should actually itself return a function.

adder[x_] := Function[{y}, x + y]

twoAdder = adder[2];

twoAdder[3] gives: 5

like image 42
Searke Avatar answered Sep 21 '22 02:09

Searke