Tag Info

Hot answers tagged

30

There are already good answers, but I'm going to improve the performance, generalize to any region in any dimensions and make the function more convenient. The main idea is to use DirichletDistribution (the uniform distribution on a simplex, e.g. triangle or tetrahedron). This idea was implemented by PlatoManiac and me in the related question obtaining ...


27

Metropolis algorithm Update: ~15x speedup with Compile! I propose an original solution, which consists in using the Metropolis algorithm. It is a very general approach, which is applicable for any probability density function in any dimensions. Metropolis /: Random`DistributionVector[ Metropolis[pdf_, u0_, s_: 1, n0_: 100, chains_: 200], n_Integer, ...


20

Lets call your plot res. res = RegionPlot[And @@ Table[ Dot[{Phi1, Phi2}, Eta[b]] <= Norm[NI[Pi] - Eta[b]]^2 + 2, {b, 0, 2 Pi, 2 Pi/10}], {Phi1, -7, 7}, {Phi2, -7, 7}]; Lets extract the mesh Mathematica is generating by default. Use more PlotPoints to get more triangular mesh of your 2D region. pts = res[[1, 1]]; (* Vertices *) {triangles, qd} = ...


19

It would be nice if UniformDistribution worked on arbitrary regions, then we could simply do RandomVariate[UniformDistribution[region]]. Someone at Wolfram should get on that. In the meantime, it seems we have to write our own sampling routines. @m_goldberg's answer is very nice (vote it up!) and uses rejection sampling, which works for arbitrary regions. ...


17

You can use Show to combine graphics of the same type: g1 = Plot3D[x^2 - y^2, {x, -3, 3}, {y, -3, 3}, RegionFunction -> Function[{x, y, z}, 2 < x^2 + y^2 < 9]]; g2 = SphericalPlot3D[ 1 + Sin[5 θ] Sin[5 φ]/5, {θ, 0, π}, {φ, 0, 2 π}, Mesh -> None, RegionFunction -> (#6 > 0.95 &), PlotStyle -> FaceForm[Orange, Yellow]]; ...


17

You can always hide away the coordinate transformations inside a function that calls RegionPlot3D. Here's a quick & dirty sphericalRegionPlot3D: sphericalRegionPlot3D[ ineq_, {r_, rmin_: 0, rmax_: 1}, {th_, thmin_: 0, thmax_}, {ph_, phmin_, phmax_}, opts___] := RegionPlot3D[With[{ r = Sqrt[x^2 + y^2 + z^2], th = ArcCos[z/Sqrt[x^2 + y^2 + ...


15

In principal you should be able to do r = RegionDifference[Ball[{0, 0, 0}], Ball[{0, 0, 1}]]; rp = RegionPlot3D[r, PlotPoints -> 50]; DiscretizeGraphics[rp] Unfortunately, this does not work and is hopefully improved in a future version. One thing you can do, however, is use the finite element mesh generator for this: Needs["NDSolve`FEM`"] m = ...


15

Update Compare two pictures. First is able to make mistake like you made the code. You need to do like this code using Mod[ArcTan[x, y], 2π]. h[r_,θ_] := 2 < r <= 5 && 3/4 π < θ < 3/2 π RegionPlot[ h[Sqrt[x^2 + y^2], Mod[ArcTan[x, y], 2π]], {x, -6, 6}, {y, -6, 6}] So I suggest to use ParametricPlot like this. rg = 6; mg = ...


14

While not positive, I believe the answer is that RegionPlot does not support spherical (or other non-Cartesian) coordinates natively. If correct, I guess the question becomes "What's the easiest way to plot a region defined in terms of spherical coordinates, without resorting to converting the equations by hand?" V9 has commands to ease this process. ...


14

Βαγγέλη, you can use an appropriate RegionFunction: V = 1/2*(x^2 + y^2 + z^2) + (x^2*y^2 + x^2*z^2 + y^2*z^2 - x^2*y^2*z^2); E0 = 7; S0 = ContourPlot3D[V == E0, {x, -4, 4}, {y, -4, 4}, {z, -4, 4}, PlotPoints -> 70, PerformanceGoal -> "Speed", Mesh -> None, ContourStyle -> Directive[Green, Opacity[0.3], Specularity[White, 30]], ...


14

Here is what I think the issue is: Let's look at what NDSolve parses. Needs["NDSolve`FEM`"] {state} = NDSolve`ProcessEquations[{op == 0, bc}, u, {x, y} ∈ reg, Method -> {"FiniteElement", "MeshOptions" -> {MaxCellMeasure -> 0.005}}]; femData = state["FiniteElementData"]; femData["PDECoefficientData"]["All"] {{{{0}}, {{{{0}, {0}}}}}, ...


14

Update: Using MeshFunctions and Mesh in RegionPlot: RegionPlot[Evaluate[Region`RegionProperty[Rationalize /@ blob, {x, y}, "FastDescription"][[1, 2]]], {x, -3, 3}, {y, -3, 3}, Mesh -> 50, MeshFunctions -> {#1 + #2 &, #1 - #2 &}, MeshStyle -> White, PlotStyle -> Directive[{Thick, Blue}]] With settings MeshStyle -> ...


14

Update Silvia proposed a much faster algorithm that I believe produces I uniform distribution. Here is my implementation of it. pointsInMask2[mask_Image, n_Integer, range : {_, _} : {0, 1/2}] := Reverse @ ImageData @ Binarize[mask, range]\[Transpose] // SparseArray[#]["NonzeroPositions"] & // RandomChoice[#, n] + RandomReal[{-1, 0}, {n, 2}] ...


12

regplt = RegionPlot[\[CapitalOmega], AspectRatio -> Automatic]; ContourPlot[{2 x^2 + 3 y^2 + 2 x y - 2, x^2 + y^2 - .1}, {x, -1.25, 1.25}, {y, -1.25, 1.25}, Contours -> {{0}}, BaseStyle -> Thick, GridLines -> {xg, yg}, Method -> {"GridLinesInFront" -> True}, MeshFunctions -> {#1 &, #2 &}, Mesh -> {xg, yg}, MeshStyle -> ...


12

Good news! Version 10.2 of Mathematica has this built-in with the function RandomPoint[]. From the documentation: RandomPoint can generate random points for any RegionQ region that is also ConstantRegionQ. RandomPoint will generate points uniformly in the region reg. The first example given is a simple disk, but there are a whole host of ...


11

The inertia tensor is defined as an integral of the following tensor over the body region vars = {x, y, z}; r2 = IdentityMatrix[3] Tr[#] - # &@Outer[Times, vars, vars]; r2 // MatrixForm It is very simple to do with integration over a region Integrate[r2, vars ∈ region] It can be wrapped in the following function inertiaTensor[reg_, assum_: {}] ...


11

I don't like to think too much :P Manipulate[ {#, Composition[ # - 1 &, Length, Union, Flatten, MorphologicalComponents, Binarize, Rasterize ]@#} &@ ParametricPlot[{Sin[ n t], Sin[m t]}, {t, 0, 2 Pi}, Axes -> False, PlotStyle -> Thick] , {n, 2, 10, 1}, {m, 1, 9, 1}]


10

I enjoy the solution of PlatoManiac and I want to improve it Eta[a_] := {Cos[a], Sin[a]} NI[a_] := {Cos[a], Sin[a]} res = RegionPlot[ And @@ Table[ Dot[{Phi1, Phi2}, Eta[b]] <= Norm[NI[Pi] - Eta[b]]^2 + 2, {b, 0, 2 Pi, 2 Pi/10}], {Phi1, -7, 7}, {Phi2, -7, 7}]; points = res[[1, 1]]; {id3, id4} = Cases[res[[1]], Polygon[{a___}] -> {a}, ...


10

Being this the first answer at the site using the Region context, I think it deserves an entry on its own: AppendTo[$ContextPath, "Region`"] z = 0; RegionPlot[ RegionProperty[ TransformedRegion[ ParametricRegion[{{p1, p2, p4}, 0 < p1 < 1 && ...


10

It sounds like you might like to use a stereographic projection. xy[ϕ_, λ_] := 2 Tan[(π - ϕ)/2] {Cos[λ], Sin[λ]}; Here is a cubic with a free parameter (for fun): cubic[{x_, y_}, b_] := y^3 - b x y + 4 x^3; For the sphere, use SphericalPlot3D of course. Although you could resort to ParametricPlot3D for the cubic curve, it's automatic and much simpler ...


10

Perhaps something like this? p = Plot[x^3/9, {x, -3, 3}, PlotStyle -> Thickness[0.01], Filling -> Axis, AspectRatio -> 1, ImageSize -> 500, BaseStyle -> {FontFamily -> "Calibri", 30}, AxesStyle -> Thick]; With[{k = 3}, SphericalPlot3D[1, {theta, 0, Pi}, {phi, 0, 2 Pi}, Mesh -> None, Boxed -> False, Axes -> False, ...


10

Following up on my comment and borrowing a method from Vectorizing an image like "Trace Bitmap" in Inkscape: mask = Binarize @ Import["http://i.stack.imgur.com/yoPNX.png"]; {row, col} = ImageDimensions[mask]; intf = ListInterpolation @ Reverse @ ImageData @ mask; region = DiscretizeGraphics @ RegionPlot[intf[c, r] < 1/2, {r, 1, row}, {c, ...


9

Here is a work-around that will let you extract as many randoms points from a region as you wish. SeedRandom[0]; region = DiscretizeRegion @ RegionUnion@Table[Disk[RandomReal[4, {2}], RandomReal[1]], {10}] randomFromRegion[region_, trials_] := Module[{bounds, randPts}, bounds = RegionBounds@region; randPts = Transpose @ ...


9

I suppose MeshRegion and Graphics3D have similar methods of the visualization. However, Graphics3D is more convenient and GraphicsComplex can increase the performance: mesh = ReadFSMesh@"fsmesh.bin"; brain = GraphicsComplex[MeshCoordinates@#, MeshCells[#, 2]] &@mesh; Graphics3D[{EdgeForm[], Gray, brain}, Lighting -> "Neutral", Boxed -> False] ...


9

Not so fancy as transforming an image, but I like how the mesh in ParametricPlot shows the deformation. {chvar} = Simplify[ Solve[u == x y && v == y - x && y > 0 && x > 0, {x, y}, Reals], 1 <= u <= 4 && 0 <= v <= 2] (* {{x -> (2 u)/(v + Sqrt[4 u + v^2]), y -> 1/2 (v + Sqrt[4 u + v^2])}} *) ...


9

RegionPlot[1 < 2 x + y < 4 && -1 < x - y < 1, {x, -1, 2}, {y, -1, 3}]


8

Use the inequality to sow all integer coordinates that are inside the boundary of the region when iterating through all integer pairs of the full range: pts = First@Last@Reap@Do[If[x >= 4 y && x <= 4 y + 3, Sow@{x, y}], {x, 0, 63}, {y, 0, 15}] RegionPlot[x >= 4 y && x <= 4 y + 3, {x, 0, 63}, {y, 0, 15}, Epilog -> {Red, ...


8

This is a bug, I think, and I filed it as such: The second region should not evaluate to a RegionQ BoundaryMeshRegion. A BoundaryMeshRegion is valid if it contains a closed surface. The subtle point about BoundaryMeshRegion is that this closed surface is a (sparse) representation of the entire region the surface encloses. Why the first one does not work, I ...


8

SeedRandom[0]; region = RegionUnion @ Table[Disk[RandomReal[4, {2}], RandomReal[1]], {10}]; DiscretizeRegion @ region points = RandomReal[{-1, 5}, {10000, 2}]; circles = List @@ (region /. Disk -> Circle); Graphics[{AbsolutePointSize[2], Transpose[{RegionMember[region, points] /. {False -> White, True -> Gray}, Point /@ points}], ...


8

ClearAll[mpbmdcg,ljr]; mpbmdcg[k_]:= Composition[MeshPrimitives[#,k]&, BoundaryMesh, DiscretizeGraphics, Graphics]; ljr = Composition[Line, Join[#, {#[[1]]}]&, Replace[#,Line[{a_,b_}]:>a, {0, Infinity}]&]; poly = First@mpbmdcg[2]@(Rectangle/@(-1+Normal[october2014[Values]])); fastdesc= FullSimplify[ Reduce[ ...



Only top voted, non community-wiki answers of a minimum length are eligible