I tried plot a 3d animated but it doesn't work.

1 view (last 30 days)
ti = 0;
tf = 1E-2;
tspan=[ti tf];
y0 = [(10E-6).*rand(6,1);((-3.14).*rand(2,1) + (3.14).*rand(2,1))]; % intial conditions
o = sort(10e3*rand(1,2),'ascend'); %detuning frequency
tc = 10E-6;
[T,Y]= ode45(@(t,y) rate_eq(t,y,o),tspan,y0);
figure(1)
subplot 221
plot(Y(:,4),Y(:,5));
xlabel("A(2)")
ylabel("A(1)")
grid on
subplot 222
plot(Y(:,4),Y(:,6));
xlabel("A(3)")
ylabel("A(1)")
grid on
subplot 223
plot(Y(:,5),Y(:,6));
xlabel("A(3)")
ylabel("A(2)")
grid on
subplot 224
plot(T./tc,Y(:,4));
hold on
plot(T./tc,Y(:,5));
plot(T./tc,Y(:,6));
hold off
xlabel("time in ms")
ylabel("Amplitude")
grid on
figure(2)
plot(T./tc,Y(:,7));
hold on
plot(T./tc,Y(:,8));
hold off
xlabel("time in ms")
ylabel("Phase diagram")
grid on
figure(3)
z = Y(:,4);
y = Y(:,5);
x = Y(:,6);
curve = animatedline('LineWidth',2);
hold on;
for i=1:length(z)
addpoints(curve,x(i),y(i),z(i));
head = plot3(x(i),y(i),z(i),'filled','MarkerFaceColor','r');
drawnow
pause(0.001);
delete(head);
end
Error using plot3
Invalid color, marker, or line style.
function dy = rate_eq(t,y,o)
dy = zeros(8,1);
P = 0.2;
a = 0.1;
tf = 230E-6;
tc = 30E-9;
k = 1E-5;
dy(1) = (P - y(1).*((abs(y(4)))^2 +1))./tf;
dy(2) = (P - y(2).*((abs(y(5)))^2 +1))./tf;
dy(3) = (P - y(3).*((abs(y(6)))^2 +1))./tf;
dy(4)= (y(1)-a).*((y(4))./tc) + (k./tc).*(y(5)).*cos(y(7));
dy(5)= (y(2)-a).*((y(5))./tc) + (k./tc).*(y(4)).*cos(y(7)) + (k./tc).*(y(6))*cos(y(8));
dy(6)= (y(3)-a).*((y(6))./tc) + (k./tc).*(y(5)).*cos(y(8));
dy(7) = o(1,1) - (k./tc).*((y(4)./y(5)) + (y(5)./y(4))).*sin(y(7)) + (k./tc).*(y(6)/y(5)).*sin(y(8));
dy(8) = o(1,2) - (k./tc).*((y(5)./y(6)) + (y(6)./y(5))).*sin(y(8)) + (k./tc).*(y(4)/y(5)).*sin(y(7));
end

Accepted Answer

Karim
Karim on 4 Oct 2022
Edited: Karim on 4 Oct 2022
I think you want to use scatter3 and not plot3. See below for a demonstration, i commented some line to run it in the browser (which doesn't support animations).
But essentially, the only change is plot3 into scatter3.
ti = 0;
tf = 1E-2;
tspan=[ti tf];
y0 = [(10E-6).*rand(6,1);((-3.14).*rand(2,1) + (3.14).*rand(2,1))]; % intial conditions
o = sort(10e3*rand(1,2),'ascend'); %detuning frequency
tc = 10E-6;
[T,Y]= ode45(@(t,y) rate_eq(t,y,o),tspan,y0);
figure(1)
subplot 221
plot(Y(:,4),Y(:,5));
xlabel("A(2)")
ylabel("A(1)")
grid on
subplot 222
plot(Y(:,4),Y(:,6));
xlabel("A(3)")
ylabel("A(1)")
grid on
subplot 223
plot(Y(:,5),Y(:,6));
xlabel("A(3)")
ylabel("A(2)")
grid on
subplot 224
plot(T./tc,Y(:,4));
hold on
plot(T./tc,Y(:,5));
plot(T./tc,Y(:,6));
hold off
xlabel("time in ms")
ylabel("Amplitude")
grid on
figure(2)
plot(T./tc,Y(:,7));
hold on
plot(T./tc,Y(:,8));
hold off
xlabel("time in ms")
ylabel("Phase diagram")
grid on
figure(3)
z = Y(:,4);
y = Y(:,5);
x = Y(:,6);
curve = animatedline('LineWidth',2);
hold on;
for i=1:50%length(z)
addpoints(curve,x(i),y(i),z(i));
head = scatter3(x(i),y(i),z(i),'r','filled');
drawnow
% pause(0.001);
% delete(head);
end
view(3); xlabel('X'); ylabel('Y'); zlabel('Z'); grid on
function dy = rate_eq(t,y,o)
dy = zeros(8,1);
P = 0.2;
a = 0.1;
tf = 230E-6;
tc = 30E-9;
k = 1E-5;
dy(1) = (P - y(1).*((abs(y(4)))^2 +1))./tf;
dy(2) = (P - y(2).*((abs(y(5)))^2 +1))./tf;
dy(3) = (P - y(3).*((abs(y(6)))^2 +1))./tf;
dy(4)= (y(1)-a).*((y(4))./tc) + (k./tc).*(y(5)).*cos(y(7));
dy(5)= (y(2)-a).*((y(5))./tc) + (k./tc).*(y(4)).*cos(y(7)) + (k./tc).*(y(6))*cos(y(8));
dy(6)= (y(3)-a).*((y(6))./tc) + (k./tc).*(y(5)).*cos(y(8));
dy(7) = o(1,1) - (k./tc).*((y(4)./y(5)) + (y(5)./y(4))).*sin(y(7)) + (k./tc).*(y(6)/y(5)).*sin(y(8));
dy(8) = o(1,2) - (k./tc).*((y(5)./y(6)) + (y(6)./y(5))).*sin(y(8)) + (k./tc).*(y(4)/y(5)).*sin(y(7));
end

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!