How can I make the Earth rotate around it's axis in matlab?

So I want to represent the Earth rotating for a number of seconds from a tspan knowing that a full rotation happens in 86160 seconds. It means that for 239.33 seconds , the Earth rotates with a degree. The problem is that I don t know how to use the rotate command in a right way. This is the code:
tspan=[0 :72000];
[X,Y,Z]=sphere(50);
R=6400000;
earth = imread('earth.jpg');
globe= surf(-X*R,Y*R,-Z*R);
image_file='earth.jpg';
cdata = imread(image_file);
set(globe, 'FaceColor', 'texturemap', 'CData', cdata, 'EdgeColor', 'none');
set(gcf,'Color','k')
set(gca, 'visible', 'off')
axis equal
view (90,0)
rotating=1; % 1 degree ever 239.33 seconds from tspan
rotate(earth, [0 1 0],1) % test to see if it's working

 Accepted Answer

Hi Alexandru,
MODIFIED answer
I used a jpg of my own and reproduced your code, eliminating some lines that were not in use. It works really well. I am not sure what you mean by 'doesn't stay on fixed axis, moves around it'. When I run this, the globe stays in place. I am using [0 0 1] instead of [0 1 0] so that the globe rotates about the north pole. With [0 1 0] the globe is rotating about an unusual axis, but it is still staying in place and rotating about its center of mass.
[X,Y,Z]=sphere(50);
R=6400000;
globe= surf(-X*R,Y*R,-Z*R);
cdata = imread('chilis.jpg');
set(globe, 'FaceColor', 'texturemap', 'CData', cdata, 'EdgeColor', 'none');
set(gcf,'Color','k')
set(gca, 'visible', 'off')
axis equal
view (90,0)
rotate(earth, [0 0 1],1) % test to see if it's working

12 Comments

Hi, David This is the image
The code doesn't seem to work properly. The Earth rotates , but doesn't stay fixed on axis , it moves around it.
I use it with a rk method that iterates for a number of seconds, so i put the rotate in the loop to draw it , but I still get that extra movement around axis.(the loop also displays an orbit ..using animatedline)
for i=1:(tspan(end)/h)
H=sqrt(y(1,i)^2+y(2,i)^2+y(3,i)^2);
k_1 = func(tspan(i), y(:,i), H, m, A, y(4:6, i));
k1=double(k_1);
k_2 = func(tspan(i)+0.5*h, y(:,i)+0.5*h*k_1, H, m, A, y(4:6, i));
k2=double(k_2);
k_3 = func((tspan(i)+0.5*h), (y(:,i)+0.5*h*k_2), H, m, A, y(4:6, i));
k3=double(k_3);
k_4 = func((tspan(i)+h),(y(:,i)+k_3*h), H, m, A, y(4:6, i));
k4=double(k_4);
y(:,i+1) = double(y(:,i) + (1/6)*(k1+2*k2+2*k3+k4)*h);
addpoints(orbit,y(1,i),y(2,i),y(3,i));
rotate(globe,[0 0 1],1)
drawnow update
end
With green,red,yellow are displayed the axis. That extra red from beyond is from the orbit.
The code is not complete since 'func' is not there. Could you provide it, and anything else needed to make the code work? Hard to comment otherwise.
function yprim=func (t,y,H,m,A,vit)
miu=398600.4418*10^9;
magn=(y(1)^2+y(2)^2+y(3)^2)^(3/2);
yprim=zeros(6,1);
yprim(1,1)=y(4);
yprim(2,1)=y(5);
yprim(3,1)=y(6);
yprim(4,1)=double(-miu*y(1)/magn);
yprim(5,1)=double(-miu*y(2)/magn);
yprim(6,1)=double(-miu*y(3)/magn);
end
still doesn't work because 'h' is not provided, also probably m,A,vit etc.
You are right:
y0(1,1)= 743322.3616 ;
y0(2,1)= -6346021.219 ;
y0(3,1)= -3394131.349 ;
y0(4,1)= 5142.38067;
y0(5,1)= 4487.44895 ;
y0(6,1)= -7264.00872;
%%%%Timpul
tspan=[0 :72000];
h=1
m=217;
A=1.2
y=zeros(6,tspan(end)/h);
y(:,1)=y0
for i=1:(tspan(end)/h)
H=sqrt(y(1,i)^2+y(2,i)^2+y(3,i)^2);
k_1 = func(tspan(i), y(:,i), H, m, A, y(4:6, i));
k1=double(k_1);
k_2 = func(tspan(i)+0.5*h, y(:,i)+0.5*h*k_1, H, m, A, y(4:6, i));
k2=double(k_2);
k_3 = func((tspan(i)+0.5*h), (y(:,i)+0.5*h*k_2), H, m, A, y(4:6, i));
k3=double(k_3);
k_4 = func((tspan(i)+h),(y(:,i)+k_3*h), H, m, A, y(4:6, i));
k4=double(k_4);
y(:,i+1) = double(y(:,i) + (1/6)*(k1+2*k2+2*k3+k4)*h);
addpoints(orbit,y(1,i),y(2,i),y(3,i));
rotate(globe,[0 0 1],1)
drawnow update
end
Now it doesn't know about addpoints or orbit. this does not seem to be working out too well.
orbit = animatedline('Color','r')
sorry about that
I figured it out , after all. The code it's working, but the problem is that the axis are rescaling after the orbit is being displayed as growing from values that are close to the Earth's radius, when it's at perigee, to bigger values at apogee.
just a matter of time before you solved it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!