My code is not running spherical coordinates

Problem description

  • Program not running
    -This is my code
    t = linspace(0, 12.857);
    vtheta = linspace(0, (1.5707 + ((0.0873)(t)(cos((0.1222)(t))))));
    vphi = linspace(-(1.0995 + ((0.0611)(t))), (1.0995 + ((0.0611)(t))));
    vrho = 0.5;
    [vtheta, vphi] = meshgrid(vtheta, vphi);
    [x,y,z] = sph2cart(vtheta,vphi,vrho);
    surf(x,y,z)
  • (How to create a Minimal, Reproducible Example - Help Center - Stack Overflow).
    -I expected to get a plot but just errors instead

My system

  • OS: Windows 10 PRO(version 21H2
  • Octave version: latest on Gnu website
  • Installation method: e.g. Downloaded and installed “octave-7.1.0-w64-installer.exe” from Download

((0.0873)(t)(cos((0.1222)(t))))))

Multiplication sign * and / or .* is required. Like this : (0.0873 * t .* cos(0.1222*t).

There might be other errors also.
Please copy and paste the errors in your posts.

linspace(0, (1.5707 + ((0.0873)(t)(cos((0.1222)(t))))));

Also, linspace is being fed with an array like linspace(0, some_array). This is probably unintended. linspace is usually called like linspace(scalar, scalar, number_of_points).

Please explain what each line of code is supposed to do so that we can find if the code actually does that.

1 Like

As octavecontrib points out you have syntax errors in your code, mainly the excess of parenthesis where you should have multiplication operators * or .*, also some confusion in the usage of some functions

going line by line is as it follows

t = linspace(0, 12.857);

linspace creates regularly linearly-spaced points in an interval, so it requires the specification of (beginning of interval, ending of interval, number of points), preferably you needed to give a number of points for it to work in a more controlled way, let’s add some points, lets say 50 between [0, 12.857], the code should be:

t = linspace(0, 12.857, 50);

if you omit the number of points linspace creates 100 points in the interval

t = linspace(0, 12.857);

now, you seem to be creating some kind of angular parameters by applying some functions to each point of the parameter t like 1.5707 + (0.0873*t.*cos(0.1222*t)), but at the same time you are creating a linear space of points for each element resulting from that function application by using the linspace, I don’t know if that should be done that way, again if you omit the desired number of points linspace uses 100, putting the appropriate multiplication operators and deleting the excess of parenthesis the code should be

vtheta = linspace(0, (1.5707 + (0.0873*t.*cos(0.1222*t))) );
vphi = linspace(-(1.0995 + (0.0611*t)), (1.0995 + (0.0611*t)) );
vrho = 0.5;

now since the result of applying linspace to the functions that uses the t array parameters, create a linear spaced array for each element the result is that vtheta and vphi are matrices instead of vectors so meshgrid also fails to be applied i’m going to omit it

% [vtheta, vphi] = meshgrid(vtheta, vphi);

then you can already use the spherical to cartesian transformation

[x,y,z] = sph2cart(vtheta,vphi,vrho);
surf(x,y,z)

The complete corrected code is:

t = linspace(0, 12.857);
vtheta = linspace(0, (1.5707 + (0.0873*t.*cos(0.1222*t))) );
vphi = linspace(-(1.0995 + (0.0611*t)), (1.0995 + (0.0611*t)) );
vrho = 0.5;
%[vtheta, vphi] = meshgrid(vtheta, vphi);


[x,y,z] = sph2cart(vtheta,vphi,vrho);
surf(x,y,z)
axis square
axis tight
shading flat

i used the parameter shading flat for the surface plot since with 100 points it looks too dense

1

i would recommend using fewer points, like 50 in each linspace and looking at how it looks with plots like or mesh

you also need to check if this coincides mathematically with what you were trying to do