Turning to your first question: first you will need to determine the coordinates of your torus in toroidal coordinates (it seems natural!), And then convert it to Cartesian coordinates, since MATLAB expects all graphs to be plotted (unless you create a polar graph, of course) . So, we start by defining our toroidal coordinates:
aminor = 1.; % Torus minor radius Rmajor = 3.; % Torus major radius theta = linspace(-pi, pi, 64) ; % Poloidal angle phi = linspace(0., 2.*pi, 64) ; % Toroidal angle
We need only one surface of the torus, so a small radius is a scalar. Now make a two-dimensional grid of these coordinates:
[t, p] = meshgrid(phi, theta);
and convert to 3D Cartesian coordinates using the formulas given on the Wikipedia page related to the question:
x = (Rmajor + aminor.*cos(p)) .* cos(t); y = (Rmajor + aminor.*cos(p)) .* sin(t); z = aminor.*sin(p);
Now we have our torus defined in 3D, we can build it with surf
:
surf(x, y, z) axis equal
Change To solve the second question, it depends on how you saved your distortions, but say that you have a matrix defined at each toroidal and poloidal point, you just need to multiply the constant, the distortion is a small radius. In the future, I create a distortion matrix that has the same dimensions as my toroidal and poloidal angular coordinate matrices, and which is usually distributed around the average value of 1 with FWHM 0.1:
distortion = 1. + 0.1 * randn(64, 64); x = (Rmajor + aminor .* distortion .*cos(p)) .* cos(t); y = (Rmajor + aminor .* distortion .* cos(p)) .* sin(t); z = aminor.* distortion .* sin(p); surf(x, y, z)
Result: