function hopfplot(a,b,c,color)
% HOPFPLOT Plots the stereographic projection of a Hopf circle
% The inverse image of the point (a,b,c) in S2, under the Hopf map, is 
% parametrized in S3 sitting in R4. A stereographic projection takes
% these points to points (u1,u2,u3) in R3. The color of the plot is an optional input.
% Written by Oskar Henrikson, May 2018
% 
% Example: 
%   hopfplot(0,1,0) gives a circle.
%   hopfplot(0,cos(theta),sin(theta)) for theta in [0,2*pi] gives a
%   torus, and hopfplot(1,0,0) gives a straight line parallel to the u1
%   axis ("a circle through the point at infinity").

tt = [0:1000]*2*pi/1000;

for j = 1:length(tt)  
    % Generate point of Hopf circle in S3
    w = 1/sqrt(2*(1+a))*(-sin(tt(j)))*(1+a);
    x = 1/sqrt(2*(1+a))*(-cos(tt(j)))*(1+a);
    y = 1/sqrt(2*(1+a))*(b*cos(tt(j))+c*sin(tt(j)));
    z = 1/sqrt(2*(1+a))*(c*cos(tt(j))-b*sin(tt(j)));

    % Make a stereographic projection onto R3
    u1(j) = 1/(1-w)*x;
    u2(j) = 1/(1-w)*y;
    u3(j) = 1/(1-w)*z;
end

if ~exist('color','var') || isempty(color)
    plot3(u1,u2,u3)
else
    plot3(u1,u2,u3,color)
end

xlabel('$u_1$','Interpreter','latex')
ylabel('$u_2$','Interpreter','latex')
zlabel('$u_3$','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
grid on