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.

  • $\begingroup$ does this work as a work-around: Plot[cf[x], {x, 0, 100}, ScalingFunctions -> {{Log, Exp}, None}]? $\endgroup$ – kglr May 27 '18 at 11:44
  • $\begingroup$ .. or this: Plot[cf[Exp@x], {x, Log@1, Log@100}, Ticks -> {Charting`ScaledTicks[{Log, Exp}], Automatic}]? $\endgroup$ – kglr May 27 '18 at 11:48
  • 1
    $\begingroup$ What version and OS are you using? Your code works as expected with v11.3 on a Mac. $\endgroup$ – Bob Hanlon May 27 '18 at 12:44
  • $\begingroup$ A wild guess, maybe you need to Clear[x]. BTW, your code runs no warning and no error on my side. $\endgroup$ – Αλέξανδρος Ζεγγ May 27 '18 at 12:48
  • $\begingroup$ @BobHanlon Windows 10 and Mathematica 10.2. I have v11 on another computer so may try to transfer it over and run it there. $\endgroup$ – mg4w May 28 '18 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}]

Mathematica graphics

  • $\begingroup$ This worked for me. Thanks! $\endgroup$ – mg4w May 28 '18 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]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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