Visualizing a geometric puzzle with mathematica - math

I am trying to figure out a way to move two points, X and Y, independently of one another along the edges of an equilateral triangle with vertices A, B, and C. There are also some collision rules that need to be taken into account:
(1) If X is at a vertex, say vertex A, then Y cannot be on A or on the edges adjacent to it. i.e., Y can only be on vertices B or C or the edge BC.
(2) If X is on an edge, say AB, then Y cannot be on A, nor B, nor any of the edges adjacent to A and B. i.e., Y must be on vertex C
I have figured out how to move the two points along the triangle using a pair of sliders, but I can't figure out how to implement the collision rules. I tried using the Exclusions option for Slider but the results are not what I expect. I would prefer to drag the points along the triangle rather than using sliders, so if someone knows how to do that instead it would be helpful. Ideally, I would be able to
move the two points from a vertex to either one of the edges instead of coming to a stop at one of them. Here is my code so far.
MyTriangle[t_] :=
Piecewise[{{{-1, 0} + (t/100) {1, Sqrt[3]},
100 > t >= 0}, {{0, Sqrt[3]} + (t/100 - 1) {1, -Sqrt[3]},
200 > t >= 100},
{{1, 0} + (t/100 - 2) {-2, 0}, 300 >= t >= 0}}]
excluded[x_] := \[Piecewise] {
{Range[0, 99]~Join~Range[201, 299], x == 0},
{Range[0, 199], x == 100},
{Range[101, 299], x == 200},
{Range[0, 199]~Join~Range[201, 299], 0 < x < 100},
{Range[1, 299], 100 < x < 200},
{Range[0, 99]~Join~Range[101, 299], 200 < x < 300}
}
{Dynamic[t], Dynamic[x]}
{Slider[Dynamic[t], {0, 299, 1}, Exclusions -> Dynamic[excluded[x]]], Dynamic[t]}
{Slider[Dynamic[x], {0, 299, 1}, Exclusions -> Dynamic[excluded[t]]], Dynamic[x]}
Dynamic[Graphics[{PointSize[Large], Point[MyTriangle[t]],
Point[MyTriangle[x]],
Line[{{-1, 0}, {1, 0}, {0, Sqrt[3]}, {-1, 0}}]},
PlotRange -> {{-1.2, 4.2}, {-.2, 2}}]]

How about something like:
MyTriangle[t_]:=Piecewise[{
{{-1,0}+t {1,Sqrt[3]},1>t>=0},
{{0,Sqrt[3]}+(t-1) {1,-Sqrt[3]},2>t>=1},
{{1,0}+(t-2) {-2,0},3>=t>=0},{0,True}}]
and
Column[{
{Slider[Dynamic[x], {0, 3, .01}], Dynamic[x]},
{Slider[Dynamic[y], {0, 3, .01}], Dynamic[y]},
Dynamic[x = Mod[x, 3]; Which[
x==0.,Which[0.<=y<1.,y=1.,2.<y<=3.,y=2.],0.<x<1.,y=2.,
x==1.,Which[1.<=y<2.,y=2.,0.<y<=1.,y=0.],1.<x<2.,y=0.,
x==2.,Which[2.<=y<3.,y=0.,1.<y<=2.,y=1.],2.<x<3.,y=1.];
Graphics[{PointSize[Large], Point[MyTriangle /# {x, y}],
Line[{{-1, 0}, {1, 0}, {0, Sqrt[3]}, {-1, 0}}]}]]}]

Related

How to calculate the convolution of a function with itself multiple times in Wolfram?

Excuse me I am new to Wolfram. I have seen people asking questions about how to do convolution of a function with itself in Wolfram. However, I wonder how to do it multiple times in a loop. That is to say I want to do f20* i.e. f*f*f*f*....f totaling 20 f. How to implement it?
Here is my thinking. Of course do not work....
f[x_] := Piecewise[{{0.1`, x >= 0 && x <= 10}, {0, x < 0}, {0, x > 10}}];
g = f;
n = 19;
For[i = 1, i <= n, i++, g = Convolve[f[x], g, x, y]]; Plot[
g[x], {x, -10, n*10 + 10}, PlotRange -> All]
Could anybody help me?
My new code after revising agentp's code
f[x_] := Piecewise[{{0.1, x >= 0 && x <= 10}, {0, x < 0}, {0,x > 10}}];
n = 19;
res = NestList[Convolve[#, f[x], x, y] /. y -> x &, f[x], n];
Plot[res, {x, -10, (n + 1)*10 + 10}, PlotRange -> All,PlotPoints -> 1000]
My buggy image
maybe this?
Nest[ Convolve[#, f[x], x, y] /. y -> x &, f[x] , 3]
If that's not right maybe show what you get by hand for n=2 or 3.
res = NestList[ Convolve[#, f[x], x, y] /. y -> x &, f[x] , 10];
Plot[res, {x, 0, 100}, PlotRange -> All]
this gets very slow with n, I don't have the patience to run it out to 20.
Your approach is nearly working. You just have to
make sure to copy f by value before entering the loop, because otherwise you face infinite recursion.
Assign the result of Convolve to a function which takes a parameter.
This is the code with the mentioned changes:
f[x_] := Piecewise[{{0.1, x >= 0 && x <= 10}, {0, x < 0}, {0, x > 10}}];
g[x_] = f[x];
n = 20;
For[i = 1, i <= n, i++, g[y_] = Convolve[f[x], g[x], x, y]];
Plot[g[x], {x, -10, n*10 + 10}, PlotRange -> All]
Edit: While this works, agentp's answer is more consise and i suspect also faster.

Mathematica plotting based on all previous equation results

I have a plot
Plot[40500*x^(-0.1), {x, 1, 100}, PlotRange -> {0, 50000}]
I'm trying to plot the cumulative of these y values. I'll try to explain with an example:
I'm trying to get
for x=1: 40500*1^(-0.1)
for x=2: 40500*(2^(-0.1)+1^(-0.1))
for x=3: 40500*(3^(-0.1)+2^(-0.1)+1^(-0.1))
and so on up to x=100.
Is there a way to do that?
Running some examples for x = 3
for x=3: 40500*(3^(-0.1)+2^(-0.1)+1^(-0.1))
114574.
This can be found using Sum:
Sum[40500*i^(-0.1), {i, 3}]
or using Fold
Fold[#1 + 40500*#2^(-0.1) &, 0, {1, 2, 3}]
114574.
FoldList outputs the intermediate steps.
FoldList[#1 + 40500*#2^(-0.1) &, 0, {1, 2, 3}]
{0, 40500., 78287.8, 114574.}
Accumulating to 100 and discarding the initial zero value:
ListLinePlot[Rest[FoldList[#1 + 40500*#2^(-0.1) &, 0, Range[100]]]]

Mathematica: Filling under an infinite function

Mathematica: Filling an infinitely deep potential well
Comment: The proper page for Mathematica questions is this one
I would like to visualize a potential well for a particle in a box in Mathematica similar to the second picture from Wikipedia here.
I have defined my function piecewise
(*Length of the box*)
L = 4;
(*Infinitly deep potential well between 0 and L*)
V[x_] := Piecewise[{
{\[Infinity], x <= 0},
{0, 0 < x < L},
{\[Infinity], L <= x}}]
and would like to obtain a plot function which gives a filled area where the potential goes to infinity.
Unfortunately my tries end up in shaded areas between the "zero region" of the potential, while I would like to have the shading in the infinity region.
Table[Plot[V[x], {x, -5, 10},
Filling -> f], {f, {Top, Bottom, Axis, 0.3}}]
The problem is that Infinity is too much for plot. So let's just give it some other big number. But to prevent it from rescaling the y axis we need to be specific with the upper plot range
Block[{\[Infinity] = 1*^1},
Plot[V[x], {x, -5, 10}, Filling -> Bottom,
PlotRange -> {Automatic, 1}]
]
Alternatively you could plot V[x]/.\[Infinity]->1*^1 instead of Block but I like Block's way better
Just give it values instead of infinity:
(*Length of the box*)L = 4;
(*Infinitly deep potential well between 0 and L*)
V[x_] := Piecewise[{{1, x <= 0}, {0, 0 < x < L}, {1, L <= x}}]
Plot[V[x], {x, -5, 10}, Filling -> Bottom]
Another way using graphic primitives:
wellLeft = 0;
leftBorder = wellLeft - 1;
rightBorder = L + 1;
wellRight = L;
top = 5;
Graphics[{
Hue[0.67, 0.6, 0.6],
Opacity[0.2],
Rectangle[{leftBorder, 0}, {wellLeft, top}],
Rectangle[{wellRight, 0}, {rightBorder, top}]
}, Axes -> True]

Generating topological space diagram in Mathematica

I have mathematica code to check whether a collection of sets satisfies the definition of a topology, I would now like to programmatically generate diagrams like these:
How can this be done?
I'm not familiar with your problem but to create diagrams from primitives, that look kind of like the ones you have pasted, you can do this:
start with the "base" case --
base = {Circle[{-0.4, 0.4}, 0.1], Disk[{0, .125}, 0.05],
Text[Style["1", 24], {0, -0.1}],
Disk[{0.5, .125}, 0.05], Text[Style["2", 24], {0.5, -0.1}],
Disk[{1., .125}, 0.05], Text[Style["3", 24], {1., -0.1}],
Circle[{.5, 0}, {.9, .5}]};
Graphics[{base}, ImageSize -> 220]
From here just add elipses to the base case:
Graphics[{base, Circle[{0, 0}, {.15, .3}]}, ImageSize -> 220]
Graphics[{base, Circle[{0, 0}, {.15, .3}],
Circle[{0.5, 0}, {.15, .3}], Circle[{0.25, 0}, {.58, .38}]},
ImageSize -> 220]
Graphics[{base, Circle[{0.5, 0}, {.15, .3}],
Circle[{0.25, 0}, {.58, .38}], Circle[{0.75, 0}, {.58, .38}]},
ImageSize -> 220]
Graphics[{base, Circle[{0.5, 0}, {.15, .3}],
Circle[{1, 0}, {.15, .3}], Red, AbsoluteThickness[6],
Line[{{-0.4, -0.5}, {1.4, 0.55}}],
Line[{{-0.4, 0.55}, {1.4, -0.5}}]}, ImageSize -> 220]
Graphics[{base, Circle[{0.25, 0}, {.58, .38}],
Circle[{0.75, 0}, {.58, .38}], Red, AbsoluteThickness[6],
Line[{{-0.4, -0.5}, {1.4, 0.55}}],
Line[{{-0.4, 0.55}, {1.4, -0.5}}]}, ImageSize -> 220]
Note that I set Frame->True while tweaking these so I could see the coordinates.
To complement Mike's cool diagrams, here is a way to check if an arbitrary finite list of lists is a topology, that is, (1) if it contains the empty set, (2) the base set, (3) closed under finite intersections, and (3) closed under union:
topologyQ[x_List] :=
Intersection[x, #] === # & [
Union[
{Union ## x},
Intersection ### Rest##,
Union ### #
] & # Subsets # x
]
Applied to the six examples
list1 = {{}, {1, 2, 3}};
list2 = {{}, {1}, {1, 2, 3}};
list3 = {{}, {1}, {2}, {1, 2}, {1, 2, 3}};
list4 = {{}, {2}, {1, 2}, {2, 3}, {1, 2, 3}};
list5 = {{}, {2}, {3}, {1, 2, 3}};
list6 = {{}, {1, 2}, {2, 3}, {1, 2, 3}};
like
topologyQ /# {list1, list2, list3, list4, list5, list6}
gives
{True, True, True, True, False, False}
EDIT 1: For a further refinement of the formulation, note that the operator
topoCover := (Union ## {Union ### #, Intersection ### Rest##} &)#Subsets## &
gives the collection obtained by taking all unions and intersections of the elements of a collection of sets. A collection of sets list is a topology if it is a fixed point of the operator topoCover. So one can define an alternative function to check if list is topology:
topologyQ2 := (topoCover## === #) &
If list is not a topology, topoCover gives the smalles superset of list which is a topology. So
Complement[topoCover##,#]&
gives the elements to be added to list to make it a topology.
One can also consider largest subset(s) of list which is a topology and the element(s) to be deleted from list to topologize it. This is done by using
maxTopoSubset := (If[{} == #, None, Last##] &)#(GatherBy[
Select[Subsets##, topologyQ], Length[#] &]) &
Applied, for example, to list6 as
maxTopoSubset#list6
we get the two topologies
{{}, {1, 2}, {1, 2, 3}}, {{}, {2, 3}, {1, 2, 3}}}
To get the elements to be removed to get a topology from list, one can use
removeToTopologize := Table[Complement[#, Part[maxTopoSubset##, i]], {i,
Length#maxTopoSubset##}] &
Using with list6 as
removeToTopologize#list6
we get
{{{2, 3}}, {{1, 2}}}
that is, removing {2,3} or {1,2} from list6 gives a topology.
I wont be able to give a mathematica specific solution however i might share some insight considering finding all the topologies on a given finite set.
The naive algorithm (the one that checks topological space axioms) runtime would be around $2^2^n$. We will reduce the search space considerably. Key point to realize is that for every preorder on a finite set there is a topology and vice versa. Given a topology one can create a relation where $x \leq y$ iff $y$ is element of all the open sets which $x$ belongs to. I believe this is called specialization preorder. From a given preorder one can recover the topology by finding the upper sets.
So if we can find all the preorders on a given set, we can recover all the topologies. Finding preorders is considerably easier. A preorder is a binary relation which is transitive and reflexive. So the search space is $2^n^2$.
There are also cool algorithms (Floyd-Warshall ) to find the transitive closure of any given relation. Finding the reflexive closure is also easy (just add the identity matrix to the adjacency matrix represantation)

How do you solve for the positive roots of a function and graph them as points on a plot of the function in mathematica?

I am attempting to graph the following function and indicate on the plot where the function passes 45 degree slope. I have been able to graph the function itself using the following code:
T = 170 Degree;
f[s_, d_] = Normal[Series[Tan[T - (d*s)], {s, 0, 4}]];
r[h_, d_] = Simplify[Integrate[f[s, d], {s, 0, h}]];
a[h_] = Table[r[h, d], {d, 1, 4, .5}];
Plot[a[h], {h, 0, 4}, PlotRange -> {{0, 4}, {0, -4}}, AspectRatio -> 1]
I need to display the point on each curve where the slope exceeds 45 degrees. However, I have thus far been unable to even solve for the numbers, due to something odd about the hadling of tables in the Solve and Reduce functions. I tried:
Reduce[{a'[h] == Table[-1, {Dimensions[a[h]][[1]]}], h >= 0}, h]
But I apparently can't do this with this kind of function, and I am not sure how to add these results to the plot so that each line gets a mark where it crosses. Does anyone know how to set this up?
Here is your code, for completeness, with plot parameters slightly modified to zoom into the region of interest:
Clear[d,h,T,f,r,a];
T = 170 Degree;
f[s_, d_] = Normal[Series[Tan[T - (d*s)], {s, 0, 4}]];
r[h_, d_] = Simplify[Integrate[f[s, d], {s, 0, h}]];
a[h_] = Table[r[h, d], {d, 1, 4, .5}];
plot = Plot[a[h], {h, 0, 4}, PlotRange -> {{0, 0.8}, {0, -0.5}},
AspectRatio -> 1, Frame -> {False, True, True, False},
FrameStyle -> Directive[FontSize -> 10],
PlotStyle -> {Thickness[0.004]}]
Here is the code to get the solutions (h-coordinates):
In[42]:= solutions = Map[Reduce[{D[#, h] == -1, h >= 0}, h] &, a[h]]
Out[42]= {h == 0.623422, h == 0.415615, h == 0.311711, h == 0.249369,
h == 0.207807, h == 0.178121, h == 0.155856}
Now produce the plot:
points = ListPlot[MapIndexed[{#1, a[#1][[First##2]]} &, solutions[[All, 2]]],
PlotStyle -> Directive[PointSize[0.015], Red],
PlotRange -> {{0, 0.8}, {0, -0.5}}, AspectRatio -> 1,
Frame -> {False, True, True, False},
FrameStyle -> Directive[FontSize -> 10]]
Finally, combine the plots:
Show[{plot, points}]
Edit:
Responding to the request of cutting plots at the found points - here is one way:
plot =
With[{sols = solutions[[All, 2]]},
Plot[Evaluate[a[h]*UnitStep[sols - h]], {h, 0, 4},
PlotRange -> {{0, 0.8}, {0, -0.5}}, AspectRatio -> 1,
Frame -> {False, True, True, False},
FrameStyle -> Directive[FontSize -> 10],
PlotStyle -> {Thickness[0.004]}]]
and this should be executed after the solutions have been found.
Could find the points via:
slope45s =
h /. Map[First[Solve[D[#, h] == -1 && h >= 0, h]] &, a[h]]
Out[12]= {0.623422, 0.415615, 0.311711, 0.249369, 0.207807, 0.178121, \
0.155856}
Here we put together the list of relevant points.
pts = Transpose[{slope45s, Tr[a[slope45s], List]}]
Can now plot in any number of ways. Here is one such.
p2 = ListPlot[pts, PlotRange -> {{0, 4}, {0, -4}},
PlotStyle -> {PointSize[.01], Red}];
p1 = Plot[a[h], {h, 0, 4}, PlotRange -> {{0, 4}, {0, -4}},
AspectRatio -> 1];
Show[p1, p2]
(Being new to this modern world-- or rather, of an age associated with an earlier civilization-- I do not know how to paste in an image.)
(Okay, thanks Leonid. I think I have an image and also indented code.)
(But why are we talking in parentheses??)
Daniel Lichtblau
Wolfram Research
Edit: I did not much like the picture I gave. Here is one I think is more descriptive.
makeSegment[pt_, slope_, len_] :=
Rotate[Line[{pt + {-len/2, 0}, pt + {len/2, 0}}], ArcTan[slope]]
p2 = ListPlot[pts, PlotStyle -> {PointSize[.01], Red}];
p1 = Plot[a[h], {h, 0, 4}, PlotRange -> {{0, 2}, {0, -1}},
AspectRatio -> 1];
p3 = Graphics[Map[{Orange, makeSegment[#, -1, .2]} &, pts]];
Show[p1, p2, p3, AspectRatio -> 1/2, ImageSize -> 1000]

Resources