Skip to main content

Posts

plotting - Plot 3D boolean Array or matrix in Mathematica

I have been attempting several methods to plot a 3D diagram of a n by n boolean array in Mathematica but none of them produced satisfactory result. I want the $3D$ plot to have a cube when M[i,j,k] == 1 and when M[i,j,k] == 0 just leaves as blank. Below is the best result I got. L=10 M = RandomReal[{0, 1}, {L, L, L}]; For[k = 1, k <= L, k++, For[j = 1, j <= L, j++, For[i = 1, i <= L, i++, If[M[[i, j, k]] < 0.5, M[[i, j, k]] = 1, M[[i, j, k]] = 0 ]]]]; Image3D[M, ImageSize -> 500, Boxed -> True] The Image3D function gives a very fluffy and opaque look on the cubes which I couldn't change!! Is there someway I can change the opacity and color of the Image3D function in this case? Or is there a better way to plot this matrix?

performance tuning - How to find all the positions of max value of a list efficiently?

For Example, list=RandomInteger[{1,100},2000] . Yes,I know Position[list,Max[list]] can do. But it's based on pattern matching! Ordering[list,-1] could find one position but not all. So how to find all the positions of max value of list in a more efficient way?Thank you. Answer A fast uncompiled alternative without pattern matching is to use the NonzeroPositions property of SparseArray , as long as you're dealing with numerical data. list = RandomInteger[{1, 100}, 10^7]; SparseArray[Unitize[list - Max[list]] - 1]["NonzeroPositions"]; // RepeatedTiming (* 0.0855 *) Position[list, Max[list]] // RepeatedTiming (* 0.509 *) compPos[list, Max[list]] // RepeatedTiming (* Marius' solution *) (* 0.0366 *) SparseArray[Unitize[list - Max[list]], Automatic, 1][ "NonzeroPositions"]; // RepeatedTiming (* MichaelE2's solutions *) (* 0.0663 *)

differential equations - Plotting a Bifurcation diagram

I have the following system equation v'(t)=2*G*J1[v(t-τ)]cos(w*τ)-v(t) How do you plot the bifurcation diagram, τ in the x axis, Vmax in the y axis? I have written these lines but how can one plot using the following Table[NDSolve[{v'[t] == 2*G*BesselJ[1, v[t - τ + i]]*Cos[ω*(τ + i)] - v[t], v[0] == 0.001}, v, {t, 0, 500}], {i, 0, 4, 0.01}] τ is varied from 1 to 4 using step 0.01,G=3.55, ω=2*Pi*12*10^6 Answer An alternative representation is G = 3.55; ω = 2*Pi*12*10^6; s = ParametricNDSolveValue[{v'[t] == 2*G*BesselJ[1, v[t - τ]] Cos[ω*τ] - v[t], v[t /; t <= 0] == 0.001}, {v, v'}, {t, 0, 120}, {τ}]; Manipulate[ParametricPlot[{s[τ][[1]][t], s[τ][[2]][t]}, {t, 60, 120}, AxesLabel -> {v, v'}, AspectRatio -> 1], {{τ, 2}, 1, 4}] Note that the diagram becomes progressively more complex as τ is increased, and the run time increases correspondingly. Addendum The bifurcations can be seen even more clearly from a return map, for instance, tab = T

core language - What is the fastest way to get a list of subexpressions and their positions?

I have spent quite some time trying to figure out what the fastest way is to get a list lists of all subexpressions and their positions. I have tried things with MapIndexed, which seems ideal in cases where we have an expression in which no functions with Hold-Attributes are present that we can let evaluate freely. For Held expressions, however, I found using MapIndexed complicated and I was unable to get a practical solution using this. Furthermore, I have tried things with Extract and Position. All these things were hinted at in the comments of/answers to the positionFunction question by Mr.Wizard , which caused me to be interested in this. Because I have spent time on this and because I feel this is quite a fundamental question, I would like to get some feedback. To be fair, I am also quite glad with my own answer, as it is considerably faster than the alternatives I found (although it took me a long time), but if there are better alternatives I would be even gladder to know. Lastly

calculus and analysis - Integrate and NIntegrate disagreeing over branch cut of Sqrt?

Bug introduced after 8.0.4 and before 9.0.1, and fixed in 11.1.0. In Mathematica 10.2 I'm trying to integrate this piecewise continuous function, but Integrate and NIntegrate seem to disagree on the branch cut of Sqrt[] : Integrate[Sqrt[Exp[I*t]^2 - 1], {t, 0, Pi}] (* 2 Sqrt[2] - I Pi *) NIntegrate[Sqrt[Exp[I*t]^2 - 1], {t, 0, Pi}] (* 1.06568 - 4.51028*10^-16 I *) It seems that NIntegrate 's answer is correct if Sqrt[] has its branch cut on the negative real axis. Is there a way to force Integrate to use the same branch cut?

graphics3d - How do I keep my Animate running at high resolution?

I'm building an Animate where I have a 3D plot with some of its parameters changing: Animate[SphericalPlot3D[Sin[(3/z)*x], x, y, PlotStyle -> Opacity[0.7]], {z, 1, 5}] Whilst it is running through the changes though, the entire plot goes very low-res. How can I ensure that the resolution stays high throughout the animation, and not just when it stops running? Answer Also fix BoxRatios, PlotRange... 1-way: PerformanceGoal -> "Quality": Animate[SphericalPlot3D[Sin[(3/z)*x], x, y, PlotStyle -> Opacity[0.7], PerformanceGoal -> "Quality", BoxRatios -> 1, PlotRange -> 1], {z, 1, 5}] 2-way: specify explicitly options that set the quality: Animate[SphericalPlot3D[Sin[(3/z)*x], x, y, PlotStyle -> Opacity[0.7], BoxRatios -> 1, PlotRange -> 1, PlotPoints -> 30, Mesh -> 10], {z, 1, 5}]