Hot answers tagged

36

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 ...


30

This assumes uniform distribution. See answer by @JimBaldwin for discussion on limitations (implicit assumptions) of my answer. Answer region = ImplicitRegion[0.5 < q < 1. && 0.5 < p < 0.5/q, {p, q}]; RandomPoint[region] (* {0.793318, 0.550934} *) Visual Show[ RegionPlot[region] , ListPlot[RandomPoint[region, 1000], PlotStyle -&...


29

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, ...


24

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. ...


22

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} = ...


22

The question does not state an essential piece of information which is the joint distribution of $p$ and $q$. All of the previous answers (so far) jump to a solution without making the joint distribution explicit (at least prior to what one sees in the code and the resulting figures). The answers using regions assume that $p$ and $q$ have uniform ...


21

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 ...


21

UPDATE: The previous version of my answer worked, but did not give control on the rounding radius, nor did it fully work with as a starting point for a geometric region for further calculations. Here is a version that is still based on spline curves, but it gives full control over the corner rounding radius. It also returns a FilledCurve object that in my ...


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]]; Show[...


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 + ...


17

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 = ...


17

Perhaps more a comment: note differences: r = RandomReal[{0.5, 1}, {10000, 2}]; Show[ListPlot[Sort@GatherBy[r, Times @@ # < 0.5 &], PlotStyle -> {{Red}, Blue}], Plot[1/(2 x), {x, 0.5, 1}, PlotStyle -> Green], Frame -> True] compared with: ListPlot[{#, RandomReal[{0.5, 1/(2 #)}]} & /@ RandomReal[{0.5, 1}, 10000], PlotStyle -&...


16

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 -> ...


15

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. ...


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


15

A simple alternative is to use Plot3D with both RegionFunction and Filling. Plot3D[y, {x, 0, 1}, {y, 0, 1}, RegionFunction -> Function[{x, y, z}, x^2 + y^2 <= 1 && x >= 0 && y >= 0 && z >= 0], Filling -> 0, FillingStyle -> Opacity[.75], PlotStyle -> Opacity[.5], AxesLabel -> (Style[#, 14, Bold] &...


15

Since you mention that you want to use the rounded polygon in NDSolve[] as a region, you might want to look at the following construction: With[{r = 1/5 (* rounding radius *)}, rp = DiscretizeRegion[ ImplicitRegion[RegionDistance[ Polygon[CirclePoints[{1 - 2 Sqrt[5 - 2 Sqrt[5]] r, π/10}, 5]], {x, y}] <= r Sqrt[(5 - ...


15

Just wanted to add purely mathematical approach using complex mapping technique. PolyMap[n_, z_] := z Hypergeometric2F1[1/n, 2/n, (n + 1)/n, z^n] (* Integrate[1/(1 - ξ^n)^(2/n), {ξ, 0, z}] *) g = GraphicsGrid[ Table[ ParametricPlot[ z = PolyMap[n, r (Cos[t] + I Sin[t])]; {Re[z], Im[z]}, {t, 0, 2 π}, PlotRange -> All, Axes -> False] /. ...


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

I'll show some examples of using the undocumemnted function Prism. Tetrahedron and Hexahedron are very similar. First some points: g = {{4, 2, 4}, {2, 2, 2}, {6, 2, 2}, {4, 6, 4}, {2, 6, 2}, {6, 6, 2}} p = Prism[g]; We can use p directly in Graphics3D e.g. Graphics3D[p] Or more fancy stuff: Graphics3D[{EdgeForm[{Thick, Darker@Green}], FaceForm[{...


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

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_: {}] :=...


14

FilledCurve will do the job because it can be styled by JoinForm: Graphics[{ EdgeForm[{JoinForm["Round"], Thickness[0.05]}], FilledCurve[Line /@ Partition[CirclePoints[5], 2, 2, 1]] }, PlotRange -> 1.2] MarcoB found that this simpler version also works (see comments): Graphics[{ EdgeForm[{JoinForm["Round"], Thickness[0.05]}], FilledCurve[...


13

I'd say that if the problem is to style the result of DiscretizeRegion, then you don't have a problem :-) You can use MeshCellStyle to indicate the styles to apply to each category of mesh component, in this format: MeshCellStyle -> {{dimensionality, index} -> style, ...}. Dimensionality is $0$ for mesh vertices, $1$ for mesh cell boundary lines, $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

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


12

You could use ListContourPlot3D and DiscretizeGraphics: Quiet @ DiscretizeGraphics @ Normal @ ListContourPlot3D[arr, Contours -> {0}, Mesh -> None]


12

r = N @ ImplicitRegion[ Sin[x Pi] > 0 || Sin[y Pi] > 0, {{x, 0, 9}, {y, 0, 9}} ] RegionPlot @ r r3 = N @ ImplicitRegion[ Sin[x Pi] > 0 || Sin[y Pi] > 0 || Sin[z Pi] > 0, {{x, 0, 9}, {y, 0, 9}, {z, 0, 9}} ] RegionPlot3D[r3, PlotStyle -> Opacity@.5] So you can play with translation and scaling with: Sin[2 x Pi] > 0 ...


11

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}, ...



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