Search code examples
matlab3dcartesian-coordinates

I need help graphing a spherical equation in Cartesian coordinates in MATLAB


I know about the function sph2cart, but I feel like I must be using it wrong. In a Calculus textbook, I saw that this following spherical equation:

ρ = 1 + 1/5*sin(6θ)*sin(5Φ)

produces something that looks like this:

Bumpy Sphere

I wanted to reproduce this in a Matlab graph, so I wrote the following code

 [q,t] = meshgrid(linspace(0,pi,100),linspace(0,2*pi,100));
 rho = 1+1/5*sin(6*t)*sin(5*q);
 [x,y,z] = sph2cart(t,q,rho);
 surf(x,y,z)
 axis square, axis equal

And I got the following graph:

Not quite so bumpy as spiky

Why is this happening? Why am I not getting the bumpy sphere my calc textbook shows?


Solution

  • There are two problems:

    1. You need .* rather than * between your sin(6*t) and sin(5*q) since you want per element multiplication not matrix multiplication.

    2. You want q to run from 0-PI and t to run from 0-2PI

    This code produces the result you're looking for.

    [q,t] = meshgrid(linspace(0,2*pi,100),linspace(0,pi,100));
    rho = 1+(1/5*sin(6*t).*sin(5*q));
    [x,y,z] = sph2cart(t,q,rho);
    surf(x,y,z)
    axis square, axis equal
    

    enter image description here