0
$\begingroup$

In a Module, I want to print the result of some data fit. I use something like this:

PlotDD[r_, n_, R_] :=
 Module[{d, fit, ft, pl, x}
       ,ft = Table[{d, PythAbst[5, r, n, d, R]}, {d, 0., 10, .1}];
        d =.;
        fit = ToString[Fit[ft, {1, d, d^2}, d]];
        Print["ft[d]=", fit];
        ...
       ];

The output of the print statement is:

ft[d]=                                                   2
0.000402667 + 0.11131 d$28040 + 0.000883443 d$28040

where the 2 in the first line is the exponent of d$28040.

I know, appending a Dollar sign plus some unique number is how local variables of Module are made. But I want it printed without this $-number, I would like it as

ft[d]=0.000402667 + 0.11131 d$ + 0.000883443 d^2

I avoid the exponent in an extra line applying InputForm first. I tried to enclose things in N[#,6] to limit digits to something physically meaningful but the latter does not work for me. Any idea about this?

$\endgroup$
4
  • 1
    $\begingroup$ You could perhaps use Block instead of Module. This will clear the values of the variables that you specify (and restore the old values when done), but not create new unique identifiers for them. Also, using LinearModelFit or NonlinearModelFit instead of Fit gives you better-formatted output directly. $\endgroup$
    – MarcoB
    Commented Feb 5, 2017 at 23:24
  • 2
    $\begingroup$ You could add d /: MakeBoxes[d, StandardForm] = "d" at the top of your Module. $\endgroup$
    – Carl Woll
    Commented Feb 5, 2017 at 23:44
  • $\begingroup$ Yes, MarcoB, after I changed Module to Block it changed the appearance of Print as desired. However with block, utmost care has to be applied if any global symbol is used in it! If any of the local variables from Block’s first parameter vars also appears in the current value of a global variable accessed inside Block, it immediately assumes the value of Block’s own local variables substituted into the global symbol’s expression: That's like a chameleon! They call this dynamic scoping (execution history scoping) rather than lexical scoping to which I am more used to. $\endgroup$ Commented Feb 6, 2017 at 16:13
  • $\begingroup$ No, Carl Woll, I made it PlotDD[r_, n_, R_] := Module[{d, fit, ft, pl, x}, d /: MakeBoxes[d, StandardForm] = "d"; ft = Table[{d, PythAbst[17, r, n, d, R]}, {d, 0., 10, 0.1}]; fit = ToString[InputForm[N[Fit[ft, {1, d, d^2}, d], 6]]]; Print["ft[d]=", fit]; ]; but I still got print results with d$8586 and with d$8586^2. - Any idea about how to coerce the numbers to 6 relevant digits? $\endgroup$ Commented Feb 6, 2017 at 16:26

2 Answers 2

4
$\begingroup$

The basic issue is the use of ToString and InputForm. There is no need for them, since Print works with expressions as well as strings. Here is a function that takes some data, and prints the quadratic fit, using my earlier suggestion in the comments:

PlotDD[data_] := Module[{d, fit},
    d /: MakeBoxes[d, StandardForm] = "d";
    fit = Fit[data, {1, d, d^2}, d];
    Print["ft[d]=", fit]
]

An example from the documentation of Fit

PlotDD[{{0,1},{1,0},{3,2},{5,4}}]

ft[d]=0.678392 -0.266332 d+0.190955 d^2

$\endgroup$
1
  • $\begingroup$ What if we do not tag? MakeBoxes[d, StandardForm] = "d"; $\endgroup$
    – Say OL
    Commented Aug 18, 2022 at 12:29
2
$\begingroup$

I use the following function

str[expr_]:=Module[{},StringReplace[ToString[expr,
   FormatType->TraditionalForm],c:LetterCharacter~~"$"~~DigitCharacter..:>c]];

To use, just wrap the content you want the $ removed from by this function. Example usage using Carl example below.

PlotDD[data_]:=Module[{d,fit},
  fit=Fit[data,{1,d,d^2},d];
  Print["ft[d]=",str@fit]]
PlotDD[{{0,1},{1,0},{3,2},{5,4}}]

Mathematica graphics

I did not write the above function. It was written by answer to my question on this many years ago by Hannes Kessler in this post at now closed group comp.soft-sys.math.mathematica and I've used it since and it works well.

$\endgroup$
1
  • $\begingroup$ Answer by Nasser did it for me. Although it works better if you change LetterCharacter for WordCharacter... That's to include variables which end in DigitCharacter as well... for example A1$91823 will be ignored if you use LetterCharacter $\endgroup$
    – lima
    Commented Apr 10, 2019 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.