%% Apply Rotation and Translation to Implicit Surface Plot
% Apply rotation and translation to the implicit surface plot of a torus.
% A torus can be defined by an implicit equation in Cartesian coordinates as
% f\left(x,y,z\right)={\left(x^2 +y^2 +z^2 +R^2 -a^2 \right)}^2 -4R^2 \left(x^2 +y^2 \right)
% where
% a is the radius of the tube
% R is the distance from the center of the tube to the center of the torus
% Define the values for a and R as 1 and 5, respectively. Plot the torus using fimplicit3.

syms x y z
a = 1;
R = 4;
f(x,y,z) = (x^2+y^2+z^2+R^2-a^2)^2 - 4*R^2*(x^2+y^2);
fimplicit3(f)
hold on

% Apply rotation to the torus around the x-axis. Define the rotation matrix. Rotate the torus by 90 degrees or \pi/2 radians. Shift the center of the torus by 5 along the x-axis.

alpha = pi/2;
Rx = [1 0 0;
      0 cos(alpha) sin(alpha);
      0 -sin(alpha) cos(alpha)];
r = [x; y; z];
r_90 = Rx*r;
g = subs(f,[x,y,z],[r_90(1)-5,r_90(2),r_90(3)]);

% Add a second plot of the rotated and translated torus to the existing graph.

fimplicit3(g)
axis([-5 10 -5 10 -5 5])
hold off

%
% Copyright 2019 The MathWorks, Inc.