there are three basic things you should be concerned about here.

1) the Math trig methods require Radians, not degrees. So convert your longitudes and latitudes to Radians (eq: PI * degree / 180)

2) the formula is actually taken from the spherical to cartesian conversion

x = ro * sin(phi) * cos(theta)

y = ro * sin(phi) * sin(theta)

z = ro * cos(theta)

phi can be considered latitude, and theta can be considered longitude, finally ro is radius.

3) lastly you have to consider what cartesion coordinate system you are in. There are multiple kinds, z forward, x forward, etc, And they can be left or right handed as well.

The previous method of conversion is for the standard conversion of spherical 0 180 phi pole to pole, over to the basic math z up right handed coordinate system. Well for starters the spherical system doesn't match earth, they stick the phi 0 degrees at the equator... and they go positive each direction (with designating north and south).

a pic of the standard continuous math cartesian coordinate system "Right handed Z up"

mapped to a lon/lat map of the earth, x forward is relatively where Nigeria's shore line is located.

so to convert to the z up right handed system you'd actually have to say...

What direction from 0 am I going... in the formula you have there you can only draw a quarter sphere (mainly the area in which Asia exists). from greenwich both Louisiana and Eastern India are 90 degrees away... just one is east, the other is west. They both happen to fall at 30 degrees north... Both locations will yield India and never yield Louisiana, unless you announce you've gone east or west. Same goes with north and south. Notice how Johannesburg South Africa and Rome were really close, well they both fall about the same distance from the equator, but one is north, the other south.

So lets first convert the longitude and latitude to radians, and then define it positive or negative by direction.

North is positive, south is negative

East is positive, west is negative

all longitudes are based where 0 degrees passes through England (like all maps have it), and latitude 90 north is the north pole, and latitude 90 south is the south pole.

now we can do this...

start with the z,

z = ro * sin(phi) //remember phi is latitude

there if you plug in all values of phi you range up and down the from north pole to south pole. (+1 to -1, for 90 degrees to -90 degrees)

x and y at 0 lat and 0 lon should yield x = 1, and y = 0. well sin(0) = 0, cos(0) = 1. Let's go with the circle/ellipse formula from polar coords to make this (trig * trig) which is a triginometric extension of the pythagorean theorem; so x is gonna consist of two cos's obviousily so that it remains 1.

x = ro * cos(phi) * cos(theta)

looking a lot like the formula you got... but we are considering the issue of north south east and west now. And we've converted them to radians.

what is y though? 0Lat and 0lon told us nothing. Lets try 0 lat and 90 lon, this should yield x = 0, y = 1. cos(0) = 1, sin(0) = 0, cos(90) = 0, sin(90) = 1. Y's formula has to yield a 1 * ro at the end. So lat must take the cos to stay 1, and lon must take the sin to stay 1.

y = ro * cos(phi) * sin(theta)

cross check amongst them all, make sure it works on all scenarios, like India and Louisiana from before (I assure you it does).

Your formula was right as long as you are converting to the coord system shown in the image above, you were putting in bad values.

... and hopefully now you understand how/why this all works.