clf
set(0,'defaulttextinterpreter','latex')
f=@(x,y,z) -x.^2+y.^2+z.^2;
x=linspace(-3,3,31);
y=linspace(-3,3,30);
z=linspace(-3,3,30);
[X,Y,Z] = meshgrid(x,y,z);
clf
eh=patch(isosurface(X,Y,Z,f(X,Y,Z),4));
set(eh,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
hold on
%title('skissa andragradsytan -x^2+y^2+z^2=4 ','FontSize',14)
plot3([-3 3],[0 0],[0 0],'black')
text(3.2,0,0,'x')
grid
view([1 -2 1])
hold on
plot3([0 0],[-3 3],[0 0],'black')
text(0,3.2,0,'y')
plot3([0 0],[0 0],[-3 3],'black')
text(0,0,3.2,'z')
axis equal
title('\bf{Skissa andragradsytan} $-x^2+y^2+z^2=4$','FontSize',20 ,'Interpreter','latex')
pause
tgammal=-2;
plany0=patch(isosurface(X,Y,Z,Y,0));
set(plany0,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
p1=text(3,0,3,'\bf{planet} $y=0$','FontSize',15 ,'Interpreter','latex');
pause
h=0.1;
x=@(t) t;
y=@(t) 0;
z=@(t) sqrt(4+t.^2);
for n=1:40
    tny=-2+n*h;
    cy0=plot3([0 x(tny)],[0 y(tny)],[0 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(cy0, 'LineStyle', 'none' );
    tgammal=tny;
end
tgammal=-2;
z=@(t) -sqrt(4+t.^2);
for n=1:40
    tny=-2+n*h;
    dy0=plot3([0 x(tny)],[0 y(tny)],[0 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(dy0, 'LineStyle', 'none' );
    tgammal=tny;
end
set(plany0,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
delete(p1)
plany1=patch(isosurface(X,Y,Z,X,0));
set(plany1,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
p2=text(0,3,3,'\bf{planet} $x=0$','FontSize',15 ,'Interpreter','latex');
pause
thetagammal=0;
x=@(theta) 0;
y=@(theta) 2*cos(theta);
z=@(theta) 2*sin(theta);
allatheta=0:h:2*pi;
for n=1:length(allatheta)
    thetany=n*h;
    cy1=plot3([0 x(thetany)],[0 y(thetany)],[0 z(thetany)],'yellow');
    plot3([x(thetagammal) x(thetany)],[y(thetagammal) y(thetany)],[z(thetagammal) z(thetany)])
    pause(0.1)
    set(cy1, 'LineStyle', 'none' );
    thetagammal=thetany;
end
set(plany1,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
delete(p2)
plany2=patch(isosurface(X,Y,Z,X,-2));
set(plany2,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
p3=text(-2,3,3,'\bf{planet} $x=-2$','FontSize',15 ,'Interpreter','latex');
pause
thetagammal=0;
x=@(theta) -2;
y=@(theta) sqrt(8)*cos(theta);
z=@(theta) sqrt(8)*sin(theta);
allatheta=0:h:2*pi;
for n=1:length(allatheta)
    thetany=n*h;
    cy2=plot3([-2 x(thetany)],[0 y(thetany)],[0 z(thetany)],'yellow');
    plot3([x(thetagammal) x(thetany)],[y(thetagammal) y(thetany)],[z(thetagammal) z(thetany)])
    pause(0.1)
    set(cy2, 'LineStyle', 'none' );
    thetagammal=thetany;
end
set(plany2,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
delete(p3)
plany3=patch(isosurface(X,Y,Z,X,2));
set(plany3,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
thetagammal=0;
x=@(theta) 2;
y=@(theta) sqrt(8)*cos(theta);
z=@(theta) sqrt(8)*sin(theta);
allatheta=0:h:2*pi;
for n=1:length(allatheta)
    thetany=n*h;
    cy3=plot3([2 x(thetany)],[0 y(thetany)],[0 z(thetany)],'yellow');
    plot3([x(thetagammal) x(thetany)],[y(thetagammal) y(thetany)],[z(thetagammal) z(thetany)])
    pause(0.1)
    set(cy3, 'LineStyle', 'none' );
    thetagammal=thetany;
end
set(plany3,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
plany4=patch(isosurface(X,Y,Z,Z,2.5));
set(plany4,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
x=@(t) sqrt(t^2+(2.5)^2-4);
y=@(t) t;
z=@(t) 2.5;
tgammal=-sqrt(8-(2.5)^2);
allat=-sqrt(8-(2.5)^2):h:sqrt(8-(2.5)^2);
for n=1:length(allat)
    tny=-sqrt(8-(2.5)^2)+n*h;
    cy4=plot3([0 x(tny)],[0 y(tny)],[2.5 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(cy4, 'LineStyle', 'none' );
    tgammal=tny;
end
x=@(t) -sqrt(t^2+(2.5)^2-4);
y=@(t) t;
z=@(t) 2.5;
tgammal=-sqrt(8-(2.5)^2);
allat=-sqrt(8-(2.5)^2):h:sqrt(8-(2.5)^2);
for n=1:length(allat)
    tny=-sqrt(8-(2.5)^2)+n*h;
    dy4=plot3([0 x(tny)],[0 y(tny)],[2.5 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(dy4, 'LineStyle', 'none' );
    tgammal=tny;
end
set(plany4,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
plany5=patch(isosurface(X,Y,Z,Z,-2.5));
set(plany5,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
x=@(t) -sqrt(t^2+(2.5)^2-4);
y=@(t) t;
z=@(t) -2.5;
tgammal=-sqrt(8-(2.5)^2);
allat=-sqrt(8-(2.5)^2):h:sqrt(8-(2.5)^2);
for n=1:length(allat)
    tny=-sqrt(8-(2.5)^2)+n*h;
    cy5=plot3([0 x(tny)],[0 y(tny)],[-2.5 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(cy5, 'LineStyle', 'none' );
    tgammal=tny;
end
x=@(t) sqrt(t^2+(2.5)^2-4);
y=@(t) t;
z=@(t) -2.5;
tgammal=-sqrt(8-(2.5)^2);
allat=-sqrt(8-(2.5)^2):h:sqrt(8-(2.5)^2);
for n=1:length(allat)
    tny=-sqrt(8-(2.5)^2)+n*h;
    dy5=plot3([0 x(tny)],[0 y(tny)],[-2.5 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(dy5, 'LineStyle', 'none' );
    tgammal=tny;
end
set(plany5,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
plany6=patch(isosurface(X,Y,Z,Z,2));
set(plany6,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
x=@(t) t;
y=@(t) t;
z=@(t) 2;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    cy6=plot3([0 x(tny)],[0 y(tny)],[2 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(cy6, 'LineStyle', 'none' );
    tgammal=tny;
end
x=@(t) t;
y=@(t) -t;
z=@(t) 2;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    dy6=plot3([0 x(tny)],[0 y(tny)],[2 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(dy6, 'LineStyle', 'none' );
    tgammal=tny;
end
set(plany6,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
plany7=patch(isosurface(X,Y,Z,Z,-2));
set(plany7,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
x=@(t) t;
y=@(t) -t;
z=@(t) -2;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    cy7=plot3([0 x(tny)],[0 y(tny)],[-2 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(cy7, 'LineStyle', 'none' );
    tgammal=tny;
end
x=@(t) t;
y=@(t) t;
z=@(t) -2;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    dy7=plot3([0 x(tny)],[0 y(tny)],[-2 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(dy7, 'LineStyle', 'none' );
    tgammal=tny;
end
set(plany7,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
plany8=patch(isosurface(X,Y,Z,Z,1));
set(plany8,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
x=@(t) t ;
y=@(t) sqrt(3+t^2);
z=@(t) 1;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    cy8=plot3([0 x(tny)],[0 y(tny)],[1 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(cy8, 'LineStyle', 'none' );
    tgammal=tny;
end
x=@(t) t ;
y=@(t) -sqrt(3+t^2);
z=@(t) 1;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    dy8=plot3([0 x(tny)],[0 y(tny)],[1 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(dy8, 'LineStyle', 'none' );
    tgammal=tny;
end
set(plany8,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
plany9=patch(isosurface(X,Y,Z,Z,-1));
set(plany9,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
x=@(t) t ;
y=@(t) sqrt(3+t^2);
z=@(t) -1;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    cy9=plot3([0 x(tny)],[0 y(tny)],[-1 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(cy9, 'LineStyle', 'none' );
    tgammal=tny;
end
x=@(t) t ;
y=@(t) -sqrt(3+t^2);
z=@(t) -1;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    dy9=plot3([0 x(tny)],[0 y(tny)],[-1 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(dy9, 'LineStyle', 'none' );
    tgammal=tny;
end
set(plany9,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
plany10=patch(isosurface(X,Y,Z,Z,0));
set(plany10,'FaceColor','red','EdgeColor','none','FaceAlpha',0.5)
x=@(t) t ;
y=@(t) -sqrt(4+t^2);
z=@(t) 0;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    cy10=plot3([0 x(tny)],[0 y(tny)],[0 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(cy10, 'LineStyle', 'none' );
    tgammal=tny;
end
x=@(t) t ;
y=@(t) sqrt(4+t^2);
z=@(t) 0;
tgammal=-2;
allat=-2:h:2;
for n=1:length(allat)
    tny=-2+n*h;
    dy10=plot3([0 x(tny)],[0 y(tny)],[0 z(tny)],'yellow');
    plot3([x(tgammal) x(tny)],[y(tgammal) y(tny)],[z(tgammal) z(tny)])
    pause(0.1)
    set(dy10, 'LineStyle', 'none' );
    tgammal=tny;
end
set(plany10,'FaceColor','red','EdgeColor','none','FaceAlpha',0)
set(eh,'FaceColor','red','EdgeColor','none','FaceAlpha',0.3)
t=linspace(pi/4,pi/4+2*pi,200);
x=sqrt(2)*cos(t);y=-2*sqrt(2)*sin(t);
for n=1:200
    view([x(n) y(n) 1])
    pause(0.01)
end
rotate3d on,hold off