1
$\begingroup$

So the thing is I have a SphericalPlot3D of a Potential function and I want to color it considering the image of the function.

The functions are:

fA[r_, θ_, ϕ_] = ((4/3 r^0 )*
     LegendreP[0, 0, Cos[θ]]) + ((-r^1 )*
     LegendreP[1, 0, Cos[θ]]) + ((1/6 r^2 )*
     LegendreP[2, 0, Cos[θ]]);
fB[r_, θ_, ϕ_] = ((38/3 r^(-(0 + 1)))*
     LegendreP[0, 0, Cos[θ]]) + ((-8 r^(-(1 + 1)))*
     LegendreP[1, 0, Cos[θ]]) + ((16/3 r^(-(2 + 1)))*
     LegendreP[2, 0, Cos[θ]]);
fδV[r_, θ_, ϕ_] = (Cos[θ] - 1)^2;

f[r_, θ_, ϕ_] = 
 Piecewise[{{fA[r, θ, ϕ], 
    r < 2}, {fB[r, θ, ϕ], 
    r > 2}, {fδV[r, θ, ϕ], r == 2}}]

And I'm trying to see a colored representation of f[r,θ_,ϕ_] projecting it onto the sphere.

How can I do it? I tried with

SphericalPlot3D[{fA[1, θ, ϕ], (Cos[θ - 1] - 1)^2, 
  fB[3, θ, ϕ]}, {θ, 0, π}, {ϕ, 0, 1.5 Pi},
  PlotRange -> All, 
 ColorFunction -> 
  Function[{x, y, z, θ, ϕ, r}, 
   ColorData["DarkRainbow"][f[r, θ, ϕ]]], 
 PlotPoints -> 10, 
 PlotLegends -> {"\!\(\*SubscriptBox[\(Φ\), \
\(δV\)]\)(r,θ,ϕ) para r=R=2", 
   "\!\(\*SubscriptBox[\(Φ\), \(B = 0\)]\)(r,θ,\
ϕ) para r=1", 
   "\!\(\*SubscriptBox[\(Φ\), \(A = 0\)]\)(r,θ,\
ϕ) para r=3"}, PlotTheme -> "Detailed", 
 AxesLabel -> {Style["X", Bold, 16], Style["Y", Bold, 16], 
   Style["Z", Bold, 16]}, ViewPoint -> {2, -2, 1.5}]

for 3 values of r.

$\endgroup$
4
  • $\begingroup$ What aspect do you want to show with the color? $\endgroup$ Commented Sep 1, 2020 at 17:29
  • $\begingroup$ @CATrevillian the value of the function (f = $\Phi (r,\theta,\phi)$) with corresponds to an electric potential $\endgroup$
    – nuwe
    Commented Sep 1, 2020 at 17:31
  • $\begingroup$ If Phi is a function, you should include this. Using ColorFunction, you are using the right syntax, save for a wrap of Evaluate around the function being used in Function. You don’t seem to have any Phi dependence what-so-ever in your functions, though. I can get it to plot with two different colors (one for outer shell & one for inner shell) but it is not clear how it should be in the end. Can you clarify any of this? $\endgroup$ Commented Sep 1, 2020 at 17:50
  • $\begingroup$ I want the color map to be the value of the function which depends on r, theta and phi: the function f composed by fA, fB, fdelta. This way I can visualize where the value of the potential is bigger on the shells $\endgroup$
    – nuwe
    Commented Sep 1, 2020 at 18:40

1 Answer 1

0
$\begingroup$

Indeed, as CA Trevillian stated, this whole code should work, and it does. An example is

fA[r_, θ_, ϕ_] = ((4/3 r^0 )*
     LegendreP[0, 0, Cos[θ]]) + ((-r^1 )*
     LegendreP[1, 0, Cos[θ]]) + ((1/6 r^2 )*
     LegendreP[2, 0, Cos[θ]]);
fB[r_, θ_, ϕ_] = ((8/3 r^(-(0 + 1)))*
     LegendreP[0, 0, Cos[θ]]) + ((-8 r^(-(1 + 1)))*
     LegendreP[1, 0, Cos[θ]]) + ((16/3 r^(-(2 + 1)))*
     LegendreP[2, 0, Cos[θ]]);
fD[r_, θ_, ϕ_] = (Cos[θ] - 1)^2
f[r_, θ_, ϕ_] = 
 Piecewise[{{fA[r, θ, ϕ], 
    r < 2}, {fB[r, θ, ϕ], 
    r > 2}, {fD[r, θ, ϕ], r == 2}}]


SphericalPlot3D[{f[4, θ, 1], f[2, θ, 1], 
  f[1, θ, ϕ 
]}, {θ, 0, \[Pi]}, {ϕ 
, 0, 
  1.7 \[Pi]}, PlotRange -> All, 
 ColorFunction -> 
  Function[{x, y, z, θ, ϕ 
, r}, 
   ColorData["RedGreenSplit"][fgrande[r, θ, ϕ 
]]], 
 ColorFunctionScaling -> True, PlotPoints -> 10, 
 PlotLabels -> {None, None, None}, PlotTheme -> "Detailed", 
 PlotLegends -> Placed["Expressions", Automatic] , 
 AxesLabel -> {Style["X", Bold, 16], Style["Y", Bold, 16], 
   Style["Z", Bold, 16]}, ViewPoint -> {2, -1.5, 1}, 
 ImageSize -> Large]

the resulting plot is the following:

enter image description here

in which, as you can see, the dependence is in the polar angle.

$\endgroup$
6
  • 1
    $\begingroup$ Can you show that this works with an image? Your function f depends on phi, at least as written in your OP. To my understanding, this shouldn’t evaluate as f shouldn’t evaluate? $\endgroup$ Commented Sep 24, 2020 at 4:30
  • 1
    $\begingroup$ @CATrevillian thanks very much for replying. The last code was wrong, I updated the solution and added a Plot to it. $\endgroup$
    – nuwe
    Commented Sep 24, 2020 at 18:41
  • 1
    $\begingroup$ shouldn’t your three spheres be different colors? At least, that’s what I thought. I could only ever get 2 to be different colors. $\endgroup$ Commented Sep 24, 2020 at 18:44
  • $\begingroup$ Indeed, they should. My guess is that the radial response of the potential, due to the form of Legendre polynomials, doesn't play a strong role when evaluating at $r=1,2,4$. You can try, e.g., with $r= 100, 90, 40$. One of the spheres will be pale-ish while the latters will be green-ish. $\endgroup$
    – nuwe
    Commented Sep 24, 2020 at 18:52
  • $\begingroup$ I notice you have ColorFunctionScaling->True, is this intentional? I would think if you set it to False, there would be a change? $\endgroup$ Commented Sep 24, 2020 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.