Related
I want to get a continuous square wave use the software mathematica.
Plot[SquareWave[{0, 10}, x], {x, 0, 10},
ExclusionsStyle -> Directive[Dotted, Red]]
However, if I set the xmax to be 100,the picture will be out of expectation.
Plot[SquareWave[{0, 10}, x], {x, 0, 100},
ExclusionsStyle -> Directive[Dotted, Red]]
Here is the result:
(http://i.stack.imgur.com/NfWVK.png)
What about another way to generate a square wave? I didn't remember where I saw,but I know we can use UnitStep.
However, it still exists the problem out of expectation.
Plot[10 UnitStep[-Sin[0.4 Pi t]], {t, 0, 200}, Exclusions -> None,
PlotStyle -> Thick]
Plot[10 UnitStep[-Sin[0.4 Pi t]], {t, 0, 500}, Exclusions -> None,
PlotStyle -> Thick]
(http://i.stack.imgur.com/iPRLc.png)
Actually,the square wave generate is used as the clock signal, so I hope it stable, continuous.
Hope you could help me to slove this problem. Hear from you soon.
There is nothing wrong with SquareWave. The trouble is Plot does a fairly coarse initial sampling and doesn't detect all the discrete regions. You simply need to give Plot a PlotPoints specification:
Plot[SquareWave[{0, 10}, x], {x, 0, 100}, PlotPoints -> 1000,
ExclusionsStyle -> Directive[Dotted, Red]]
PlotPoints needs to be set large enough so that the initial sampling hits every interval.
Alternately you can explicitly provide the points to plot and use ListLinePlot
ListLinePlot[
Flatten[Table[
{{i,10},{i+1,10},{i+1,0},{i+2,0}} ,
{i, 0, 100, 2}], 1]]
I don't see straight away how to apply the dashing in this case though.
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...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I tried to use it. And it's really nice for some Plots, but when its about making for example a triangle I found it quite complicated. I figured out how to draw a triangle but how to add that angle marks, those curved lines?
And since I'm beginner into this job, of writing a book, can anyone recommend me which is the best way to accomplish good looking graphics, for example as in the picture below. Which programs are best to use.
Thanks for any suggestions and recommendations.
Here is a simple/basic way to do the first one:
Graphics[{
(* The dashed circle segment *)
{
Dashing[{.04, .01}],
Darker[Orange],
AbsoluteThickness[2],
Circle[{0, 0}, 1, {1, 2 \[Pi]}]
},
(* The solid circle segment *)
{
Orange,
AbsoluteThickness[2],
Circle[{0, 0}, 1, {0, 1}]
},
(* The radial lines and the small circle segment *)
Line[{{0, 0}, {1, 0}}],
Line[{{0, 0}, {Cos[1], Sin[1]}}],
Circle[{0, 0}, .2, {0, 1}],
(* Various text labels *)
{
Text[Style["\[Theta]", 24], .3 {Cos[.5], Sin[.5]}],
Text[Style["s", 24], 1.1 {Cos[.5], Sin[.5]}],
Text[Style["r", 24], {.5, -.1}]
}
}]
The following is the exact same thing, but wrapped in Manipulate and parameterized
on the angle alpha:
Manipulate[
Graphics[{
{Dashing[{.04, .01}], Darker[Orange], AbsoluteThickness[2],
Circle[{0, 0}, 1, {\[Alpha], 2 \[Pi]}]},
{Orange, AbsoluteThickness[2], Circle[{0, 0}, 1, {0, \[Alpha]}]},
Line[{{0, 0}, {1, 0}}],
Line[{{0, 0}, {Cos[\[Alpha]], Sin[\[Alpha]]}}],
Circle[{0, 0}, .2, {0, \[Alpha]}],
{Text[Style["\[Theta]",
24], .3 {Cos[\[Alpha]/2], Sin[\[Alpha]/2]}],
Text[Style["s", 24], 1.1 {Cos[\[Alpha]/2], Sin[\[Alpha]/2]}],
Text[Style["r", 24], {.5, -.1}]}
}],
{{\[Alpha], 1}, 0, 2 \[Pi]}]
If you move the slider, the content will change accordingly:
Edit You can get inspiration from the Demonstrations project too. These are the triangle-related demonstrations. After taking a quick look, I think you should see the geometry-related demonstrations by Jay Warendorff. He has made a lot of these, and they use a structured set of geometry-related functions that you most likely can reuse.
Here's an angleArc function to get you started. This is just a small example of a helper function you could use, there's a lot of room for improvement.
angleArc[Polygon[vertices_List, ___], r_, i_] :=
Module[{a, b, c, phi1, phi2},
{a, b, c} = Take[RotateLeft[vertices, i-2], 3];
{phi1, phi2} = Sort#N[{ArcTan ## (a - b), ArcTan ## (c - b)}];
If[phi2 - phi1 > Pi, phi1 += 2 Pi];
Circle[b, r, {phi2, phi1}]
]
poly = Polygon[{{0, 0}, {1, 2}, {2, 1}}];
Graphics[{EdgeForm[Thick], FaceForm[None], poly,
Table[angleArc[poly, .2, i], {i, Length[poly[[1]]]}]}]
Manipulate[
With[{poly = Polygon[{a, b, c}]},
Graphics[
{EdgeForm[Thick], FaceForm[None], poly,
Table[angleArc[poly, .2, i], {i, Length[poly[[1]]]}]},
PlotRange -> 2 {{-1, 1}, {-1, 1}}, Frame -> True
]
],
{{a, {0, 0}}, Locator}, {{b, {1, 0}}, Locator}, {{c, {0, 1}}, Locator}
]
One excellent, Wolfram-supported product is Geometrica. It's not a cheap add-on at $495, but it will produce diagrams like yours far easier than doing them in raw MMA Mathematica. It's basically a very large extension of Szabolcs approach in the comments - a library of functions to draw stuff.
I like Mathematica a lot. But there are better options for geometric drawing.
Here you have a five minutes sketch done with Geometry Expressions (very low resolution used):
Geometry Expressions does some nice planar geometry calculations and can export the results to Mma online.
Yes, Mathematica's built-in graphics tools are a bit quirky in places, and there are some annoying omissions. But there's a good range of basic graphics, and, on the plus side, you're only a few keystrokes away from the mathematical tools you need to rectify some of the deficiencies of the graphics editor. Need an arc? Just calculate it below and copy/paste it in. Can't do that with Adobe Illustrator! There's broad access to equations and greek characters too. It could be helpful when there's nothing else to hand.
Here's the first one - it's not totally unacceptable ... :)
Presentations is an excellent and low-cost ($50) graphics (and much more) package in MMA, written by David Park.
http://home.comcast.net/~djmpark/DrawGraphicsPage.html
It is mentioned as resource n.2 in our stackoverflow tool bag
What is in your Mathematica tool bag?
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.
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.