Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Mathematica] Baffling bug in LogLinearPlot[]

  1. May 18, 2010 #1
    Hello everyone,

    This drives me nuts… I want to plot a function using LogLinearPlot. Take a look at what's happening :

    [PLAIN]http://img208.imageshack.us/img208/8346/picture4b.jpg [Broken]

    I printed the values of the variable passed to my function because I didn't understand what happened… Although omega must vary from 1*^-2 to 10, it evaluates to the Log of that ! (log[1*^-2] is -4.605…)

    More interesting, I change other parameters in my function (I put kx=1, kz=0 instead of 3) and look what happens :

    [PLAIN]http://img687.imageshack.us/img687/9155/picture5c.jpg [Broken]

    The very first instance of omega is still evaluated as Log[1*^-2] (sic) and after that, it's fine and omega is evaluated according to its specified range.

    This is incomprehensible if you ask me…
    Last edited by a moderator: May 4, 2017
  2. jcsd
  3. May 20, 2010 #2
    Your code is indecipherable to me. Here's some ideas though: right off you're using advanced constructs like @@ and all. If my code isn't working, the first thing I do is break it down and make it simpler without any functional constructs. Also, the word "Integrand" is not a standard function in Mathematica but I suppose you could create a function with that name although user-defined functions should begin with lower case letters. Also the GoldGratingSiInPlate option does not seem to exist as a standard color options in Mathematica 7. So first just strip it down to the basic syntax as possible even if it's messy. For example, construct a function for your data outside of LogPlot and then just try and plot it with Plot to see if the function is constructed correctly. Then start encapsulating it with functional constructs and other options if you want.
    Last edited: May 20, 2010
  4. May 20, 2010 #3
    Ah, you misunderstood… :smile:
    The whole Integrand[] and so on : I defined that. This is not built-in Mathemartica stuffs :smile:
    Replace it with whatever function, that's not the point :smile:
  5. May 20, 2010 #4
    I think what jackmell is trying to say is that you should try to come up with the "simplest working example" of your problem, so that we can try to advise you how to avoid it. Try to come up with the absolute simplest example you can find which reproduces your issue.

    Without knowing how you've defined all your functions, it's impossible for any of us to figure out where the problem is coming from.

    Edit: He's also giving you some style advice. It will help the readability of your code (for others) if you refrain from capitalising user-defined variables.

    Edit2: Breaking errors down into the simplest working example is a good practise to get into. Often you will find the problem yourself by doing that. At the very least, you make it easier for people to help you, and if there actually is a bug in Mathematica, it will make it easier for them to track it down.
    Last edited: May 20, 2010
  6. May 20, 2010 #5
    Ok Devin. You said it better than me. What I'd do is hard-code my function:

    myf[x_]=some reasonable expression here

    then run the log plot:


    and see if that works. Then start building it up and also begin encapsulating various features along with the short-cuts of functional programming constructs. But I would do these one at a time to see where along the design I start having problems.
  7. May 20, 2010 #6
    There you go

    [PLAIN]http://img268.imageshack.us/img268/8893/screenshot20100520at210.jpg [Broken]

    Trivial example, odd behavior for the first two evaluation of the variable z.
    Last edited by a moderator: May 4, 2017
  8. May 20, 2010 #7
    I think you're using a function in a manner that it was not designed for: LogLinearPlot is a graphics function for plotting a real-valued function as a function of the log of the independent variable. Also, first rule of thumb in Mathematica is do not use upper-case letters for user-defined variables or functions. Finally, it's way better if you just cut-and paste "raw code" in here so we can in turn cut-and-paste it directly into Mathematica. To do that, select the cell, then chose Cell/Convert To/Raw Input form and then cut and paste it.

    Even when I run the much simpler routine:

    myfun[x_]:=Print["x= ",x];


    amazingly it doesn't just say NO but rather does the same thing LogLinearPlot does and prints out a bunch of numbers beginning with "x" which I think is the iteration of the values of x, and then proceeds to print out a blank graph.
  9. May 20, 2010 #8


    User Avatar
    Gold Member

    Actually looking at it there is NOTHING weird about it.

    Table[Log[z], {z, 1/100., 10}]

    So thats the correct "z" plot I guess. Its most likely how it does the internal calculation converting from "z" to Log[z] for the plot.

    Basically what you're doing is letting it determine how many "z"'s its going to calculate between 1/100 and 10, and itll probably do it logarithmically. You can force it though:

    Table[Log[z], {z, 1/100., 10, 1/100.}]
    {-4.60517, -3.91202, -3.50656, -3.21888, -2.99573, -2.81341, -2.65926, -2.52573, -2.40795, -2.30...

    It basically chose : {1/100, 1+1/100, 2+1/100, ..., 9+1/100, 10}
  10. May 20, 2010 #9
    You're missing the point…
    Here is how LogLinearPlot is _supposed_ to work : if the range of the plot is from 0.01 to 10 like in my example, it will evaluate the function on a regular grid in Log[x] but irregular grid in x i.e. at values that are the result of Range[Log[1*^-2],Log[10],(Log[10]-Log[1*^-2])/(PlotPoints-1)]//Exp[#]&/@#&. This indeed strictly goes from 0.01 to 10 as it should. LogLinearPlot should _never_ evaluates the variable outside the specified range.
  11. May 20, 2010 #10


    User Avatar
    Gold Member

    They aren't I thought? Log of 1/100 is the x position of the plot. In your range expression the lower limit is the negative 4. Why is that wrong?
  12. May 21, 2010 #11
    There is an Exp[] mapped onto the whole Range[]. The lower limit is 0.01 :smile:
  13. May 29, 2010 #12
    Which version of MMA are you using?
    Under Version 5 your simple example

    In[1]:= <<Graphics`Graphics`
    In[2]:= MyFun=Function[{x,y,z},Print["x= ",x," y= ",y," z= ",z];1];

    From In[2]:= x= 1 y= 1 z= 0.0100004
    From In[2]:= x= 1 y= 1 z= 0.0100004
    From In[2]:= x= 1 y= 1 z= 0.415264
    and other similarly constructed examples do not show your problem.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook