How to display name of variables in plots in Mathematica - wolfram-mathematica

Hi guys I have a list of 2-D coordinates that I assigned one to one to a list of variables (i.e. x[i]), but i want show the variable names on list plots but don't know how to.
Please help. Thank you!

When you want Mathematica's default behavior, for example: a variable name is immediately replaced with the value assigned to that variable, things are often fairly simple. But when you want to change that default behavior things are usually much more complicated.
Try this
values = {{1, 5}, {2, 7}, {3, 4}};
SetAttributes[fun, {HoldFirst, Listable}];
fun[v_] := ToString[Unevaluated[v]];
g1 = Show[ListPlot[values],
Graphics[Text[#[[1]], #[[2]], {0, 1}] & /#
Transpose[{Flatten[fun[{x[1], x[2], x[3]}]], values}]]]
Usually I recommend new users study each part of an answer, read the help pages, take it apart, see how each piece works, make little changes until you understand how you can adapt that in the future. But in this case I think just hope that it works for you as is.

You may use the Labeled data wrapper syntax of ListPlot.
With
values = {{1, 5}, {2, 7}, {3, 4}};
then
ListPlot[
MapIndexed[Labeled[#1, StringJoin["x[", ToString#First##2, "]"]] &]#values
]
Hope this helps.

Related

Stop different Manipulate Panels from triggering each other

This should be a simple question, but I have given up on it after a while now.
I have different input lines which I have evaluated in a single notebook. Two of these have manipulate statements:
The first one
Manipulate[{ Some Plot Func }, {{\[Omega]0, 3}, 0, 3}, {{\[Tau], 5}, -1, 20}]
The second One:
Manipulate[{ Some Plot Func }, {Twave, 1, 10}, {{Tenv, 10}, 0, 100}, TrackedSymbols :> True]
When I change parameters in the second one, i can see the first one evaluating as well.
I have tried specifying with TrackedSymbols to disentangle the two, but this does not succeed. I hope someone can point me to the obvious solution. It is slowing down evaluation a lot.
Thank you!
How about this?
End each of your manipulates with a semicolon.
Create a third "master" manipulate which does not end with a semicolon.
Within the master manipulate, use a TabView. With each pane of the tab, run one of your manipulates by calling it like a function. It is convenient to create a list of your manipulates and pick the one to execute with a "tab index" of sorts.
I've gotten this to work well with several manipulates, rules for moving between them via the tabs, and so forth. It's proven to be a useful approach for notebook applications,as the complexity of one giant manipulate gets out of hand.

Mathematica: A way to set global NIntegrate options?

I would like to be able to set a list of NIntegrate options globally (or somehow have to only write them once, and have them apply to a set of NIntegrate commands). For example
WorkingPrecision -> 300, PrecisionGoal -> 5, Method ->{"SymbolicPreprocessing", "OscillatorySelection" -> False}
Sometimes when I am experimenting, the list gets much longer. Is there a way I can do this? Thanks!
Looks like this will do the trick:
SetOptions[NIntegrate, WorkingPrecision -> 300, PrecisionGoal -> 5,
Method -> {"SymbolicPreprocessing", "OscillatorySelection" -> False}]
See: http://reference.wolfram.com/mathematica/ref/SetOptions.html
PS. You can post to https://mathematica.stackexchange.com/ next time. You might get a faster response.

How to extract only the data points from BodePlot plot?

I am trying to fix the Phase plot part of BodePlot, as it does not wrap correctly. And there is no option that I can use to tell it to wrap.
So, instead of doing the full plot myself, (I can do that if I have to) I am thinking of first making the BodePlot, grab the data points, do the wrapping on the data (once I get the x,y data, the rest is easy), then I need to put the new list of points back into the plot, and then use Show to display it.
The part I am stuck at, is extracting the points from FullForm. I can't get the correct Pattern to do that.
This is what I go to so far:
hz=z/(z^2-z+0.3);
tf=TransferFunctionModel[hz,z,SamplingPeriod->2];
phasePlot=BodePlot[tf,{0.001,2 Pi},
ScalingFunctions->{Automatic,{"Linear","Degree"}},PlotLayout->"List"][[2]]
You see how it does not wrap at 180 degrees. It is more common in dsp that Bode phase plot wraps. Here is what it 'should' look like:
So, this is what I did:
FullForm[phasePlot]
Graphics[List[
List[List[], List[],
List[Hue[0.67, 0.6, 0.6],
Line[List[List[0.0010000243495554542, -0.2673870119911639],
List[0.0013659538057574799, -0.36521403872250247],
List[0.0017318832619595053, -0.46304207336414027],
....
I see the data there (the x,y) But how to pull them out? I tried this:
Cases[FullForm[phasePlot], List[x_, y_] -> {x, y}, Infinity];
But the above matches in addition to the list of point, other stuff that I do not need.
I tried many other things, but can't get only the list of points out.
I was wondering if someone knows how to pull only the (x,y) points from the above plot. Is there a better way to do this other than using FullForm?
Thanks
Update:
I just find a post here which shows how to extract data from plot. So I used it:
points = Cases[Normal#phasePlot, Line[pts_] -> pts, Infinity]
You could do try nesting the replacement rules, for example
phase2 = phasePlot /.
Line[a_] :> (Line[a] /. {x_?NumericQ, y_?NumericQ} :> {x, Mod[y, 360, -180]});
Show[phase2, PlotRange -> {Automatic, {-180, 180}}, FrameTicks -> Automatic]
Output:
The list you are looking for appears to be wrapped by Line[], and it seems to be the only case in your plot. So you could use
Cases[phasePlot, Line[list_] :> list, Infinity]
Edit:
When I posted my response, the page refreshed and I saw that you came across precisely what I had proposed. I'll leave my response posted here anyway.
Edit2:
Szabolics pointed out that FullForm[] has no effect, so I removed it from my original posting.

Mathematica: AbsoluteOptions not working properly

According to the documentation AbsoluteOptions[expr,name] "gives the absolute setting for the option name".
"AbsoluteOptions gives the actual settings for options used internally by Mathematica when the setting given is Automatic or All."
Here is an example they show:
<< AbsoluteOptions[Plot[Sin[x], {x, 0, 2 Pi},
PlotRange -> Automatic], PlotRange]
>> {PlotRange -> {{0., 6.28319}, {-1., 1.}}}
Here I use << to denote Input and >> to denote output.
This seems to work fine. However, when I try to obtain the AspectRatio of a plot it simply gives me Automatic. Try this,
AbsoluteOptions[
ListPlot3D[{{1, 1, 1, 1}, {1, 2, 1, 2}, {1, 1, 3, 1}, {1, 2, 1, 4}},
AspectRatio -> Automatic],
AspectRatio
]
Similar format as the example they show but the output I get is simply
{AspectRatio->Automatic}
Does this mean that the argument expr can only be a Graphics object and not Graphics3D? This doesn't make sense because it works fine if I try to obtain the PlotRange of a Graphics3D object. Is this a bug or my mathematica is broken? Another thing could be that the documentation is not being very specific. Which one is it?
Here is a link to the documentation:
http://reference.wolfram.com/mathematica/ref/AbsoluteOptions.html
I'm using Mathematica 7.0.1.0.
You can apply ImageDimensions directly to Graphics3D to get its ImageSize.
It seems that AbsoluteOptions just was not modified since Mathematica 4 (see bottom of the corresponding documentation page). This function is very buggy (often gives wrong value for PlotRange for 2D graphics generated by Show, for example) and sometimes does not work at all as you pointed in your question. Compare it with FullOptions and FullGraphics those are seemed to be not modified since version 2... Sometimes FullOptions gives right value when AbsoluteOptions gives wrong or does not work at all.
Maybe, BoxRatios is the one you are looking for. HTH.

Way to deal with large data files in Wolfram Mathematica

I wonder if there exists way to work with large files in Mathematica ?
Currently I have a file about 500Mb with table data.
Import["data.txt","Table"];
What is alternate way?
Use OpenRead["file"] which gives you an InputStream object on which you can use Read[stream]. Depending on the formatting of the data file you may need to set custom option values in Read[] for RecordSeparators.
Example:
In[1]:= str = OpenRead["ExampleData/USConstitution.txt"]
Out[1]= InputStream["ExampleData/USConstitution.txt", 24]
In[2]:= Read[str, Word]
Out[2]= "We"
In[3]:= Read[str, Word]
Out[3]= "the"
In[4]:= Read[str, Record]
Out[4]= "People of the United States, in Order to form a more perfect Union,"
You could also load your data into a database (for example MySQL) and access it from Mathematica using DatabaseLink
The function DumpSave can also be helpful for saving large datasets. It saves data in Mathematica's internal format, so it's more efficient in both time and file size.

Resources