Mathematica [Mathematica] Baffling bug in LogLinearPlot[]

  • Thread starter Thread starter guerom00
  • Start date Start date
  • Tags Tags
    Bug Mathematica
AI Thread Summary
The discussion centers on issues encountered while using the LogLinearPlot function in Mathematica. A user expresses frustration over unexpected evaluations of the variable omega, which initially returns the logarithm of a small value instead of the expected range. Other participants suggest simplifying the code to identify the problem, emphasizing the importance of creating a "simplest working example" to facilitate troubleshooting. They recommend avoiding uppercase letters for user-defined variables to enhance code readability. Additionally, they clarify that LogLinearPlot evaluates functions on a logarithmic scale, which can lead to confusion if not properly understood. Participants also discuss the need to ensure that variables are evaluated within the specified range and suggest hard-coding functions to isolate issues. Overall, the conversation highlights best practices for debugging in Mathematica and the nuances of using LogLinearPlot effectively.
guerom00
Messages
90
Reaction score
0
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

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

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:
Physics news on Phys.org
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:
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:
 
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:
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:

LogLinearPlot[myf[x],{x,a,b}]

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.
 
There you go

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

Trivial example, odd behavior for the first two evaluation of the variable z.
 
Last edited by a moderator:
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];

Plot[myfun[x],{x,0,1}]

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.
 
Actually looking at it there is NOTHING weird about it.

Table[Log[z], {z, 1/100., 10}]
{-4.60517,0.00995033,0.698135,1.10194,1.38879,1.61144,1.79342,1.94734,2.08069,2.19834}

So that's 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}
 
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.
 
  • #10
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?
 
  • #11
Hepth said:
In your range expression the lower limit is the negative 4.

There is an Exp[] mapped onto the whole Range[]. The lower limit is 0.01 :smile:
 
  • #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];
LogLinearPlot[MyFun[1,1,z],{z,1*^-2,10}]

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.
 

Similar threads

Replies
1
Views
1K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
6
Views
4K
Replies
3
Views
4K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
5
Views
9K
Replies
1
Views
2K
Back
Top