How to plot |z-1| = 2 in a specified domain in Mathematica? - wolfram-mathematica

How do I plot |z-1| = 2, from -10 to +10 in the real line and from -10i to +10i on the complex line? I've been trying for ages and seems like I can't get it right. Z stands for a complex number!
Also, could I use as well the x+iy notation in mathematica? or a+ib?
Thanks

For a contour plot:
ContourPlot[Abs[x + I y] == 2, {x, -10, 10}, {y, -10, 10}]
To just plot a (real-valued) function:
Plot3D[Abs[x + I y], {x, -10, 10}, {y, -10, 10}]
Choice of variable names is, of course, completely arbitrary.
Just for fun: with some smart choices, you can plot a complex-valued complex function, for example by piecing together Plot3D or ContourPlot3D with Animate.

Related

Labeling points of intersection between plots

First of all I would like to apologize for the newbie question.
I am just starting up with mathematica and I have 2 simple plots. What i want to do is have Mathematica automatically find the intersections, label them and show me the coordinates.
I have searched this forum and there are a lot of similar questions, but they are too advanced for me.
Can someone explain how i can do this the easiest way?
Solve for equality. Get values for the points using replacement : points = {x, x^2} /. sol would work just as well. Offset the labels and set as text in epilog.
sol = Solve[x^2 == x + 2, x];
points = {x, x + 2} /. sol;
offset = Map[# + {0, 3} &, points];
Plot[{x^2, x + 2}, {x, -6, 6},
Epilog -> {Thread[Text[points, offset]],
Blue, PointSize[0.02], Point[points]}]

Use Manipulate function in Mathematica to fit function to data

I want to use the Manipulate function in Mathematica to fit an analytical function to a set of (x,y) data. I want to plot the dataset on the same axes that I use to manipulate the function (so I can get a visual check of how manipulating the parameters improves the fit, but I cannot find the correct syntax to draw the points behind the manipulated curve. Any solutions to this? Many thanks!
Show[plot1,plot2,...] will overlay the plots, see the docs on Show.
In[1]:= data = Table[{x, x^2+2*x+RandomReal[{-.1,.1}]}, {x,-3,3}];
Manipulate[
Show[ListPlot[data], Plot[a*x^2 + b*x + c, {x, -3, 3}]],
{{a, 0}, -4, 4}, {{b, 0}, -4, 4}, {{c, 2}, -4, 4}]
Out[1]= ...PlotSnipped...

mathematica Plot with Manipulate shows no output

I was initially attempting visualize a 4 parameter function with Plot3D and Manipulate sliders (with two params controlled by sliders and the other vary in the "x-y" plane). However, I'm not getting any output when my non-plotted parameters are Manipulate controlled?
The following 1d plot example replicates what I'm seeing in the more complex plot attempt:
Clear[g, mu]
g[ x_] = (x Sin[mu])^2
Manipulate[ Plot[ g[x], {x, -10, 10}], {{mu, 1}, 0, 2 \[Pi]}]
Plot[ g[x] /. mu -> 1, {x, -10, 10}]
The Plot with a fixed value of mu has the expected parabolic output in the {0,70} automatically selected plotrange, whereas the Manipulate plot is blank in the {0, 1} range.
I was suspecting that the PlotRange wasn't selected with good defaults when the mu slider control was used, but adding in a PlotRange manually also shows no output:
Manipulate[ Plot[ g[x], {x, -10, 10}, PlotRange -> {0, 70}], {{mu, 1}, 0, 2 \[Pi]}]
This is because the Manipulate parameters are local.
The mu in Manipulate[ Plot[ g[x], {x, -10, 10}], {{mu, 1}, 0, 2 \[Pi]}] is different from the global mu you clear on the previous line.
I suggest using
g[x_, mu_] := (x Sin[mu])^2
Manipulate[Plot[g[x, mu], {x, -10, 10}], {{mu, 1}, 0, 2 \[Pi]}]
The following works too, but it keeps changing the value of a global variable, which may cause surprises later unless you pay attention, so I don't recommend it:
g[x_] := (x Sin[mu])^2
Manipulate[
mu = mu2;
Plot[g[x], {x, -10, 10}],
{{mu2, 1}, 0, 2 \[Pi]}
]
It may happen that you Clear[mu], but find that it gets a value the moment the Manipulate object is scrolled into view.
Another way to overcome Manipulate's localization is to bring the function inside the Manipulate[]:
Manipulate[Module[{x,g},
g[x_]=(x Sin[mu])^2;
Plot[g[x], {x, -10, 10}]], {{mu, 1}, 0, 2 \[Pi]}]
or even
Manipulate[Module[{x,g},
g=(x Sin[mu])^2;
Plot[g, {x, -10, 10}]], {{mu, 1}, 0, 2 \[Pi]}]
Both of which give
Module[{x,g},...] prevents unwanted side-effects from the global context. This enables a simple definition of g: I've had Manipulate[]ed plots with dozens of adjustable parameters, which can be cumbersome when passing all those parameters as arguments to the function.

Mathematica: How to obtain data points plotted by plot command?

When plotting a function using Plot, I would like to obtain the set of data points plotted by the Plot command.
For instance, how can I obtain the list of points {t,f} Plot uses in the following simple example?
f = Sin[t]
Plot[f, {t, 0, 10}]
I tried using a method of appending values to a list, shown on page 4 of Numerical1.ps (Numerical Computation in Mathematica) by Jerry B. Keiper, http://library.wolfram.com/infocenter/Conferences/4687/ as follows:
f = Sin[t]
flist={}
Plot[f, {t, 0, 10}, AppendTo[flist,{t,f[t]}]]
but generate error messages no matter what I try.
Any suggestions would be greatly appreciated.
f = Sin[t];
plot = Plot[f, {t, 0, 10}]
One way to extract points is as follows:
points = Cases[
Cases[InputForm[plot], Line[___],
Infinity], {_?NumericQ, _?NumericQ}, Infinity];
ListPlot to 'take a look'
ListPlot[points]
giving the following:
EDIT
Brett Champion has pointed out that InputForm is superfluous.
ListPlot#Cases[
Cases[plot, Line[___], Infinity], {_?NumericQ, _?NumericQ},
Infinity]
will work.
It is also possible to paste in the plot graphic, and this is sometimes useful. If,say, I create a ListPlot of external data and then mislay the data file (so that I only have access to the generated graphic), I may regenerate the data by selecting the graphic cell bracket,copy and paste:
ListPlot#Transpose[{Range[10], 4 Range[10]}]
points = Cases[
Cases[** Paste_Grphic _Here **, Point[___],
Infinity], {_?NumericQ, _?NumericQ}, Infinity]
Edit 2.
I should also have cross-referenced and acknowledged this very nice answer by Yaroslav Bulatov.
Edit 3
Brett Champion has not only pointed out that FullForm is superfluous, but that in cases where a GraphicsComplex is generated, applying Normal will convert the complex into primitives. This can be very useful.
For example:
lp = ListPlot[Transpose[{Range[10], Range[10]}],
Filling -> Bottom]; Cases[
Cases[Normal#lp, Point[___],
Infinity], {_?NumericQ, _?NumericQ}, Infinity]
gives (correctly)
{{1., 1.}, {2., 2.}, {3., 3.}, {4., 4.}, {5., 5.}, {6., 6.}, {7.,
7.}, {8., 8.}, {9., 9.}, {10., 10.}}
Thanks to Brett Champion.
Finally, a neater way of using the general approach given in this answer, which I found here
The OP problem, in terms of a ListPlot, may be obtained as follows:
ListPlot#Cases[g, x_Line :> First#x, Infinity]
Edit 4
Even simpler
ListPlot#Cases[plot, Line[{x__}] -> x, Infinity]
or
ListPlot#Cases[** Paste_Grphic _Here **, Line[{x__}] -> x, Infinity]
or
ListPlot#plot[[1, 1, 3, 2, 1]]
This evaluates to True
plot[[1, 1, 3, 2, 1]] == Cases[plot, Line[{x__}] -> x, Infinity]
One way is to use EvaluationMonitor option with Reap and Sow, for example
In[4]:=
(points = Reap[Plot[Sin[x],{x,0,4Pi},EvaluationMonitor:>Sow[{x,Sin[x]}]]][[2,1]])//Short
Out[4]//Short= {{2.56457*10^-7,2.56457*10^-7},<<699>>,{12.5621,-<<21>>}}
In addition to the methods mentioned in Leonid's answer and my follow-up comment, to track plotting progress of slow functions in real time to see what's happening you could do the following (using the example of this recent question):
(* CPU intensive function *)
LogNormalStableCDF[{alpha_, beta_, gamma_, sigma_, delta_}, x_] :=
Block[{u},
NExpectation[
CDF[StableDistribution[alpha, beta, gamma, sigma], (x - delta)/u],
u \[Distributed] LogNormalDistribution[Log[gamma], sigma]]]
(* real time tracking of plot process *)
res = {};
ListLinePlot[res // Sort, Mesh -> All] // Dynamic
Plot[(AppendTo[res, {x, #}]; #) &#
LogNormalStableCDF[{1.5, 1, 1, 0.5, 1}, x], {x, -4, 6},
PlotRange -> All, PlotPoints -> 10, MaxRecursion -> 4]
etc.
Here is a very efficient way to get all the data points:
{plot, {points}} = Reap # Plot[Last#Sow#{x, Sin[x]}, {x, 0, 4 Pi}]
Based on the answer of Sjoerd C. de Vries, I've now written the following code which automates a plot preview (tested on Mathematica 8):
pairs[x_, y_List]:={x, #}& /# y
pairs[x_, y_]:={x, y}
condtranspose[x:{{_List ..}..}]:=Transpose # x
condtranspose[x_]:=x
Protect[SaveData]
MonitorPlot[f_, range_, options: OptionsPattern[]]:=
Module[{data={}, plot},
Module[{tmp=#},
If[FilterRules[{options},SaveData]!={},
ReleaseHold[Hold[SaveData=condtranspose[data]]/.FilterRules[{options},SaveData]];tmp]]&#
Monitor[Plot[(data=Union[data, {pairs[range[[1]], #]}]; #)& # f, range,
Evaluate[FilterRules[{options}, Options[Plot]]]],
plot=ListLinePlot[condtranspose[data], Mesh->All,
FilterRules[{options}, Options[ListLinePlot]]];
Show[plot, Module[{yrange=Options[plot, PlotRange][[1,2,2]]},
Graphics[Line[{{range[[1]], yrange[[1]]}, {range[[1]], yrange[[2]]}}]]]]]]
SetAttributes[MonitorPlot, HoldAll]
In addition to showing the progress of the plot, it also marks the x position where it currently calculates.
The main problem is that for multiple plots, Mathematica applies the same plot style for all curves in the final plot (interestingly, it doesn't on the temporary plots).
To get the data produced into the variable dest, use the option SaveData:>dest
Just another way, possibly implementation dependent:
ListPlot#Flatten[
Plot[Tan#t, {t, 0, 10}] /. Graphics[{{___, {_, y__}}}, ___] -> {y} /. Line -> List
, 2]
Just look into structure of plot (for different type of plots there would be a little bit different structure) and use something like that:
plt = Plot[Sin[x], {x, 0, 1}];
lstpoint = plt[[1, 1, 3, 2, 1]];

how to animate 3d plot given a rotation axis in mathematics

If given a rotation axis normalized, such as {1/Sqrt[3],1/Sqrt[3],1/Sqrt[3]}, and a 3d plot, for example,
z[x_, y_] := Exp[-(Sqrt[x^2 + y^2]/Power[4, (3)^-1]) +
Power[4, (3)^-1]*Sqrt[1/2*(Sqrt[x^2 + y^2] + x)]];
Plot3D[2*z[x, y], {x, -5, 5}, {y, -5, 5}]
I want to create an animation for this plot about the axis {1/Sqrt[3],1/Sqrt[3],1/Sqrt[3]} (could be any other arbitary one), and then export it as an animated gif. Would anyone please help? Many thanks.
Edit
I also left out one degree of freedom in specifying the rotation. Could any one please help, if also given the coordinate of a point which the rotational axis must pass, how to do the visualization/animation?
Thanks again.
Copying what Daniel did, just prepared for exporting.
axis = {1, 1, 1};
l = {-7, 7};
s = Table[
Plot3D[2*z[x, y], {x, -5, 5}, {y, -5, 5}, PlotRange -> {l, l, l}] /.
gg : GraphicsComplex[___] :> Rotate[gg, theta, axis], {theta, 0., 2. Pi}];
Export["c:\\test.gif", s]
The following parameters are available for the gif export (as per the docs):
"AnimationRepetitions" how many times the animation is played before stopping
"Background" background color shown in transparent image regions
"BitDepth" bits used to represent each color channel in the file
"ColorMap" color reduction palette, given as a list of color values
"GlobalColorMap" default color palette for individual animation frames
"DisplayDurations" display durations of animation frames, given in seconds
"ImageCount" number of frames in an animated GIF
"ImageSize" overall image size
"RawData" array of color map indices
"Comments" user comments stored in the file
I used "DisplayDurations" in the past, and it worked.
Could do as below.
axis = {1, 1, 1};
Animate[
Plot3D[2*z[x, y], {x, -5, 5}, {y, -5, 5}] /.
gg : GraphicsComplex[___] :> Rotate[gg, theta, axis],
{theta, 0., 2.*Pi}]
Daniel Lichtblau
Wolfram Research

Resources