Search code examples
matlabmatlab-figure

How to combine a 3D surf plot and a comet3 plot in MATLAB?


I'm writing a code to plot the circular orbit of a satellite (created using comet3() function) around a 3d model of the Earth (created using surf() and set() functions). The problem is that I can't seem to find a way to get them together in the same plot. I have tried using hold on and hold off but that doesn't seem to work either. I'm pasting the MATLAB code below for reference.

Edit: All the other functions like sv_from_coe(), odeset, etc. are working perfectly, the only place I'm facing issue is combining the plots from comet3() and set().

G = 6.67E-11;
Me = 5.976E24;

coe = [6776, 0.0005638, 2.0543, 0.9, 5.549, 0];
[r, v] = sv_from_coe(coe);
rv = [r v];

opt = odeset('RelTol', 1e-6, 'AbsTol', 1e-6);
[t,X] = ode45(@rate, [0 2*1.5*3600], rv, opt);

[x,y,z] = sphere;
r_earth = 6378*1000;

figure
hs1 = surf(x*r_earth,y*r_earth,-z*r_earth);
cdata = imread('1024px-Land_ocean_ice_2048.jpg');
alpha = 1;

hold on
axis equal
comet3(X(:,1), X(:,2), X(:,3))
set(hs1, 'FaceColor', 'texturemap', 'CData', cdata, 'FaceAlpha', alpha, 'EdgeColor', 'none')

Solution

  • You just have to reverse the order, first plot the earth and set the texture. Then use comet3 to animate the trajectory:

    % earth
    [x,y,z] = sphere;
    r_earth = 6378*1000;
    
    % some simple trajectory
    phi = 0:0.01:2*pi;
    r_orbit = r_earth + 408*1e3; % ISS orbit height
    xv = r_orbit * cos(phi);
    yv = r_orbit * sin(phi);
    zv = zeros(size(yv));
    
    % draw figure
    figure(1); clf;
    ax = axes;
    
    % first plot the earth and set texture
    hs1 = surf(x*r_earth,y*r_earth,-z*r_earth);
    alpha = 1;
    cdata = imread("Land_ocean_ice_2048.jpg");
    set(hs1, 'FaceColor', 'texturemap', 'CData', cdata, 'FaceAlpha', alpha, 'EdgeColor', 'none')
    hold on
    axis equal
    
    % finally, animate using comet3
    comet3(xv,yv,zv)