# Spinning top: how to plot position on a sphere?

I am modelling a spinning top on a sphere and I want to obtain plots such as these I have solved the equations of motion

B = 1;
a = 1;
sol = NDSolve[{p'[t] == (B - a*Cos[th[t]])/(Sin[th[t]]^2),
th''[t] == (p'[t]*(p'[t]*Cos[th[t]] - a) + 1)*Sin[th[t]],
th == 1, p == 0, th' == 0}, {p, p', th, th'}, {t, 0, 10} ]


Then to plot the points I evaluate

a = Flatten[Table[th[t] /. sol, {t, 0, 10}]]
b = Flatten[Table[p[t] /. sol, {t, 0, 10}]]
res = Transpose[{Table[1, {t, 0, 10}], a, b
}] (*I want all points on a unit sphere*)


Then following what is in this question I do

Graphics3D@{Sphere[{0, 0, 0}, 1],
Point[CoordinateTransformData["Spherical" -> "Cartesian",
"Mapping", #] & /@ res]}


But I get back all sorts of errors as below Why so? It looks like this is because my angles phi get quite large as I got around the sphere (n rotations would be 2nPi).

The outputs of NDSolve are the following: I think the issue can be solved if all the coordinates theta and phi can be mapped into the range allowed by the coordinate transformation. Mathematica seems to require phi between -pi and pi. So theta should be between 0 and pi. How do I do this? (Very new to mathematica)

As you have already identified, the problem with CoordinateTransformData[ "Spherical" -> "Cartesian", "Mapping", {r, θ, ϕ}] is that it wants to treat spherical coordinates as invertible map. Thus, it wants the second argument to lie in the interval $$[0,\pi]$$ and the third argument to lie in the interval $$[-\pi ,\pi]$$. You can achieve this by using Modas follows:

res1 = res;
res1[[All, 2]] = Mod[res1[[All, 2]], Pi];
res1[[All, 3]] = Mod[res1[[All, 3]], 2 Pi, -Pi];


Now, the plot

Graphics3D[{
Sphere[{0, 0, 0}, 1],
Point[CoordinateTransformData["Spherical" -> "Cartesian", "Mapping", #] & /@ res1]
}]


should work.

There are some problems with CoordinateTransform and CoordinateTransformData that can be avoided by simply not using these functions. Here's the problem: say we want start the top spinning at $$\theta=0$$. Try putting the $$(r,\theta,\phi)$$ coordinates of $$(1,0,1)$$ into CoordinateTransform

CoordinateTransform["Spherical"->"Cartesian", {1,0,1} ]


and it gives an error because the transformation is not defined at $$\theta=0$$. Also, the Mod function is not exactly right for the $$(r,\theta,\phi)$$ coordinates of $$(1,1,\pi)$$ because

Mod[π, 2 π, -π]


gives $$-\pi$$, which is also an undefined value of the transformation. Try it with

CoordinateTransform["Spherical" -> "Cartesian",
{1, 1, Mod[π, 2 π, -π]}]


to see the error. Of course you may never see these error messages, but you could.

One way to avoid these problems is to not use the CoordinateTransform function at all. Consider the actual $$(x,y,z)$$ coordinates of your trajectory:

xyz = Table[{Cos[p[t]] Sin[th[t]],
Sin[p[t]] Sin[th[t]],
Cos[th[t]]} /. First[sol],
{t, 0, 10, 1/10}];


We can plot these coordinates like this:

Graphics3D[{Green, Point[First[xyz]],
Thick, Red, Line[xyz],
Black, Point[Last[xyz]],
Arrow[{{0, 0, -1}, {0, 0, 1}}],
Blue, Opacity[1/5], Sphere[]}, ImageSize -> 200]


The arrow in the plot is so we can see which way is up.

The advantage of using the $$(x,y,z)$$ coordinates is that we do not have to worry about those what happens when $$\theta$$ and/or $$\phi$$ are "out of range".
{"ft_lang_label":"__label__en","ft_lang_prob":0.8556362,"math_prob":0.99891853,"size":1175,"snap":"2023-40-2023-50","text_gpt3_token_len":391,"char_repetition_ratio":0.09906063,"word_repetition_ratio":0.009433962,"special_character_ratio":0.35829788,"punctuation_ratio":0.14606741,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9997384,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,8,null,8,null,8,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-11-28T23:14:28Z\",\"WARC-Record-ID\":\"<urn:uuid:fc304e64-010c-4db3-a4b8-39662e14045f>\",\"Content-Length\":\"171063\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e1929e1b-97d4-427a-9655-8c43e22234c1>\",\"WARC-Concurrent-To\":\"<urn:uuid:1d142146-66d0-46b8-ba02-6e61db8423e5>\",\"WARC-IP-Address\":\"104.18.43.226\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/184720/spinning-top-how-to-plot-position-on-a-sphere\",\"WARC-Payload-Digest\":\"sha1:NHPZVHTLD2FSNXI4TIGM2EHB5CKR5N2W\",\"WARC-Block-Digest\":\"sha1:RGPVJRPXEMQ3MEL6WBI56REYJAY753EK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100016.39_warc_CC-MAIN-20231128214805-20231129004805-00147.warc.gz\"}"}