Wolfram Mathematica: Direction fields graphing [closed] - wolfram-mathematica

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Trying to figure out this question here:
Plot direction field for the differential equation
dy/dx=sin(x^2+y^2)log(|x+y|) when -1<x,y<1.
You will have full grade if you will make intervals of the plot
all of the same length. Make the grid 20 X 20.
Here's my non working code:
StreamPlot[{1, (Sin[y^2+x^2]*Log[Abs[x+y]])}, {x, -10, 10}, {y, -10, 10}, RegionFunction->Function[{x,y}, {{-1<x<1}&&{-1<y<1}}]]
Can't understand Wolfram syntax. Any tips much appreciated!

Not sure to understand all what you asked. If your aim is to plot the direction field with a grid involving 20x20=400 points, a possible way to do this is:
grid = Range[-1, 1, 2/19]
points = Apply[Join, Outer[{#1, #2} &, grid, grid]];
VectorPlot[{1, (Sin[y^2 + x^2]*Log[Abs[x + y]])},
{x, -1, 1}, {y, -1, 1},
VectorPoints -> points,
GridLines -> {grid, grid}]
grid, of length=20, is:
{-1, -(17/19), -(15/19), -(13/19), -(11/19), -(9/19), -(7/19), -(5/19),
-(3/19), -(1/19), 1/19, 3/19, 5/19, 7/19, 9/19, 11/19, 13/19, 15/19,
17/19, 1}
points is the grid Cartesian product:
{{-1, -1}, {-1, -(17/19)}, {-1, -(15/19)}, {-1, -(13/19)}, ...}
The vectors (1,f(x,y)) of your ODE y'=f(x,y) are evaluted for each point of points. The resulting plot is:
Below, some clarifications about the Mathematica syntax & your question:
The RegionFunction option allows you to plot in some specified region. For instance, you can restrict the vector field to be plotted in the unit disk:
VectorPlot[{1, (Sin[y^2 + x^2]*Log[Abs[x + y]])},
{x, -1, 1}, {y, -1, 1},
VectorPoints -> points, GridLines -> {grid, grid},
RegionFunction -> Function[{x, y}, x^2 + y^2 < 1]]
Your code does not work because your boolean expression (the body part of the pure function):
Function[{x, y}, {{-1<x<1}&&{-1<y<1}}]
does not respect the Mathematica syntax.
The Mathematica way to write it is:
Function[{x, y}, (-1<x<1)&&(-1<y<1)]

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]}]

labeling a plot in increments of pi [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to choose the numbers shown on the axes of a plot in mathemetica?
How can I create a plot of a function where the scale will show things in terms of pi (or any other constant for that matter)?
i.e. under the ticks in the axis it will show 0, pi/2, pi/4 etc.
example:
Plot[Sin[x], {x, -Pi, Pi}, Ticks -> {Range[-Pi, Pi, Pi/4], Automatic}]
What you mean by labeling and scale is indeed the Ticks option of Plot. You can specify them by hand or use Table to create the values you want to use
Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi},
Ticks -> {Table[x, {x, 0, 2 Pi, Pi/4}], {-1, 0, 1}}]

Simple programming techniques / tricks in Mathematica for making graphics for the math book? [closed]

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?

How to export ContourPlot3D surface and regenerate it in Excel, Originlab or some other similar softwares

I tried this, but failed.
fig3D = ContourPlot3D[ x^2 + y^3 - z^2 == 0, {x, -2, 2}, {y, -2, 2}, {z, -2, 2},
PlotPoints -> 100]
pts = (InputForm#fig3D)[[1, 1, 1]];
ListSurfacePlot3D[pts]
The regenerated surface is very poor. Any suggestions? thanks!
Not too bad if you specify MaxPlotPoints
ListSurfacePlot3D[pts, Mesh -> None, MaxPlotPoints -> 100]
Compare with
ListSurfacePlot3D[pts]
Edit
Regarding the export to Excel, please consider that the Excel surface plot is a very basic construction and requires a matrix whose first file and column are the XY values with the Z values in the inner cells. Example:
So, exporting a working dataset to Excel may require (an unspecified amount of) data massaging.

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]];

Resources