Skip to main content

Posts

export - convert Mathematica expressions to Matlab (ToMatlab and Bessel functions)

I have several expressions like the two followings: X11 = I omega rho0 phi HankelH1[m, k0 ra]; X12 = -b1 kp1^2 BesselJ[m, kp1 ra]; I want to covert these expressions to Matlab. To this end, I use the package ToMatlab (by the way is there somewhere available a more recent version of it?) X11 // ToMatlab X12 // ToMatlab (* "sqrt(-1).*omega.*phi.*rho0.*HankelH1(m,k0.*ra); " *) (* "(-1).*b1.*kp1.^2.*BesselJ(m,kp1.*ra); " *) However, this is not the syntax that Matlab uses of the Bessel and Hankel functions. For instance BesselJ[m,x] (Mathematica) => besselj(m,x) (Matlab). Any ideas how to overcome the problem and avoid modifying on my own the latter expressions? Answer You could modify the ToMatlab package. Find the section starting with (*** Symbols *****************************************************************) then add ToMatlabaux[BesselJ] = "besselj" ToMatlabaux[HankelH1] = "besselh" In these cases the translation was trivial. ...

plotting - Legend Matrix Plot

I have the following problem. I'd like to add a legend to MatrixPlot . Each colour should have a legend entry. I used PlotLegends , which in principle works. However, if I use more than five colours, this doesn't work anymore. a = RandomInteger[{1, 6}, {50}]; MatrixPlot[{a}, ColorRules -> {1 -> Red, 2 -> Blue, 3 -> Green, 4 -> Gray,5->Yellow,6->Orange}] Answer When Automatic fails the same thing can, luckily, be done manually: rules = {1 -> Red, 2 -> Blue, 3 -> Green, 4 -> Gray, 5 -> Yellow, 6 -> Orange}; a = RandomInteger[{1, 6}, {50}]; MatrixPlot[ {a}, ColorRules -> rules, PlotLegends -> SwatchLegend[rules[[All, 2]], rules[[All, 1]]] ] A horizontal version (this is an update in response to Bob's new answer) can be achieved thus: PlotLegends -> Placed[ SwatchLegend[rules[[All, 2]], rules[[All, 1]]], Bottom ]

plotting - Use different markers/colors in logarithmic plot depending on sign

Suppose you have the dataset xData = Table[i, {i, 1, 30}]; yData = RandomReal[{0.995, 1.005}, 30]; and want to plot the difference of yData-1 on a ListLogPlot . ListLogPlot[Transpose[{xData, yData - 1}], Joined -> True, Mesh -> All] Of course, there will be negative differences, hence not being plotted. If the differences' signs were unimportant one could just plot Abs[yData-1] . However, if the sign is important: What is a (I am sure there must be something) nice way to e.g. plot the Abs but use different markers for different signs. The only way I can come up with is pre-processing the data into two sets corresponding to the signs and then plot both seperately into the same graph. Edit: I decided to accept MichaelE2's answer because I did not know anything about VertexColor and it could be very useful for future plotting issues. However, also all other answers are great solutions and I don't mean to depreciate their value by not accepting them - I just think that...

equation solving - Is there a better way to get the eight points of intersection?

I want to get the eight points of intersection from the equations 2 Abs[x] + Abs[y] == 1 and Abs[x] + 2 Abs[y] == 1 . To solve these equations, I tried Solve[{2 Abs[x] + Abs[y] == 1, Abs[x] + 2 Abs[y] == 1}, {x, y}] but could only get the four points. I then tried y /. Quiet@Solve[#, y] /. Abs[x_] -> {x, -x} & /@ {2 Abs[x] + Abs[y] == 1, Abs[x] + 2 Abs[y] == 1} p = ({x, y} /. Solve[y == #] & /@ {{1 - 2 x, (1 - x)/2}, {(1 - x)/2, ( 1 + x)/2}, {(1 + x)/2, 1 + 2 x}, {1 + 2 x, -1 - 2 x}, {-1 - 2 x, 1/2 (-1 - x)}, {1/2 (-1 - x), 1/2 (-1 + x)}, {1/2 (-1 + x), -1 + 2 x}, {-1 + 2 x, 1 - 2 x}})~ Flatten~1 {{1/3, 1/3}, {0, 1/2}, {-(1/3), 1/3}, {-(1/2), 0}, {-(1/3), -(1/3)}, {0, -(1/2)}, {1/3, -(1/3)}, {1/2, 0}} It works, but I don't like it. Could you recommend a better method? Answer Actually, the way I interpret your ContourPlot , there are only 4 points of intersection (red/blue curves). So I do interpret your question such that you want to include i...

bugs - Numerical Functions Compatible with Regions Fail with MeshRegions and BoundaryMeshRegions

Bug introduced in 10.0.0 and fixed in 10.0.2 Given a MeshRegion : region = DelaunayMesh[RandomReal[1, {50, 3}]] We can numerically Integrate over it easily: NIntegrate[x^2 y^2 z^2, {x, y, z} ∈ region] 0.0169908561 We can also Integrate over its boundary: NIntegrate[x^2 y^2 z^2, {x, y, z} ∈ RegionBoundary@region] 0.151404597 if you replace DelaunayMesh with ConvexHullMesh , which yields a BoundaryMeshRegion , the same process works fine. This does not come as a surprise as the documentation for NIntegrate suggests that integrating over regions in this way is possible. Now we turn our attention to NArgMin . We can mimic the built-in RegionNearest as follows: dist[x_?VectorQ, y_?VectorQ] /; Length[x] == Length[y] := Sqrt @ Total[(x - y)^2] regN[region_, point_] := NArgMin[{dist[point, x], x ∈ region}, x] We can use it as follows: regN[Disk[], {2, 3}] {0.55470039, 0.832050166} regN[Sphere[], {2, 3, 4}] {0.371392166, 0.557086686, 0.742780105} Note that Disk[] and Sphere[] are Regio...

piecewise - What does True mean in this case?

I use the following code to find the expansion for small $x$: Assuming[x \[Element] Reals, FullSimplify@ Series[-Sqrt[-x] I^(n + 1/2) HankelH1[n, -x], {x, 0, 1}]] I find the solutions: Analogously: Assuming[x \[Element] Reals, FullSimplify@ Series[-Sqrt[-x] I^(n + 1/2) HankelH2[n, -x], {x, 0, 1}]] What does True mean in the last lines? Answer From the documentation of Piecewise : The $\text{cond}_i$ are evaluated in turn, until one of them is found to yield True . The last condition is always True , so that Piecewise can return a value even when all the preceding conditions evaluated to False . In a math textbook, this last case would be written as "otherwise". When you write math notation for humans, you would make sure that the conditions are all disjoint, and there is an "otherwise" at the end. A CAS like Mathematica needs to handle junk input reasonable gracefully. What if some conditions overlap, or what if there's no "otherwise"? Symbol...