# Plot compiled function with LogLinearPlot

I have a compiled function which I can plot without issue using Plot. However, when I use LogLinearPlot I get the message:

Argument x at position 1 should be a machine-size real number.

The following MWE gets the same message:

compFn = Compile[{{x, _Real}}, x^2 + 2]
LogLinearPlot[compFn[x], {x, 1, 100}]


Ultimately my goal is to plot the function with a logarithmic scale for the $x$-axis.

• does this work as a work-around: Plot[cf[x], {x, 0, 100}, ScalingFunctions -> {{Log, Exp}, None}]?
– kglr
May 27, 2018 at 11:44
• .. or this: Plot[cf[Exp@x], {x, Log@1, Log@100}, Ticks -> {ChartingScaledTicks[{Log, Exp}], Automatic}]?
– kglr
May 27, 2018 at 11:48
• What version and OS are you using? Your code works as expected with v11.3 on a Mac. May 27, 2018 at 12:44
• A wild guess, maybe you need to Clear[x]. BTW, your code runs no warning and no error on my side. May 27, 2018 at 12:48
• @BobHanlon Windows 10 and Mathematica 10.2. I have v11 on another computer so may try to transfer it over and run it there. May 28, 2018 at 10:09

In 11.3, your code works without issue. I guess in an earlier version, Mathematica might try to evaluate your compiled function with a symbolic x which leads to the error. Try the following, which wraps your compiled code in a function that does only allow numerical values

With[{compFn = Compile[{{x, _Real}}, x^2 + 2]},
f[x_?NumericQ] := compFn[x]
];
LogLinearPlot[f[x], {x, 1, 100}]


• This worked for me. Thanks! May 28, 2018 at 10:10

In MMA 11 code is working.

compFn = Compile[{{x, _Real}}, x^2 + 2]
LogLinearPlot[compFn[x], {x, 1, 100}, Frame -> True, PlotStyle -> Blue,
FrameStyle -> Directive[Black, Thick], PlotRange -> All]
`