Matlab plot3 not giving a 3D plot
显示 更早的评论
Hi all, Sorry I'm a beginner with Matlab and don't understand why my plot3 with 3 inputs is not giving me a 3D representation of 4 bugs travelling on a 3D plane?
Here is my code:
clc, clear, clear all
ti=0;
tf=1000;
tspan= linspace(ti,tf,1000000);
f0 = [0; 0; 0; 1; 2; 0; 1; 0; 3; 0; 2; 3];
[t, f] = ode45(@g, tspan, f0);
%Trajectories are done in 3 coordinates x,y,z
i_1=(f(:,1)); %Trajectory of bug 1
j_1=(f(:,2));
k_1=(f(:,3));
i_2=(f(:,4)); %Trajectory of bug 2
j_2=(f(:,5));
k_2=(f(:,6));
i_3=(f(:,7)); %Trajectory of bug 3
j_3=(f(:,8));
k_3=(f(:,9));
i_4=(f(:,10));
j_4=(f(:,11));
k_4=(f(:,12));
figure
hold on
plot3(i_1, j_1, k_1)
plot3(i_2, j_2, k_2)
plot3(i_3, j_3, k_3)
plot3(i_4, j_4, k_4)
title('test')
xlabel('x')
ylabel('y')
zlabel('z')
function dxdt = g(t,f)
a = sqrt((f(2)-f(1))^2 + (f(6)-f(5))^2 + (f(10)-f(9))^2);
b = sqrt((f(3)-f(2))^2 + (f(7)-f(6))^2 + (f(11)-f(10))^2);
c = sqrt((f(4)-f(3))^2 + (f(8)-f(7))^2 + (f(12)-f(11))^2);
d = sqrt((f(1)-f(4))^2 + (f(5)-f(8))^2 + (f(9) -f(12))^2);
dxdt = [ ...
(f(2) - f(1)) / a;
(f(6) - f(5)) / a;
(f(10) - f(9)) / a;
(f(3) - f(2)) / b;
(f(7) - f(6)) / b;
(f(11) - f(10)) / b;
(f(4) - f(3)) / c;
(f(8) - f(7)) / c;
(f(12) - f(11)) / c;
(f(1) - f(4)) / d;
(f(5) - f(8)) / d;
(f(9) - f(12)) / d];
end
Thank you
6 个评论
Torsten
2022-5-14
Did you see the plots generated by plot3 under
?
Do you have a different expectation about the plot ?
Jonas Freiheit
2022-5-14
Torsten
2022-5-15
figure
plot3(i_1, j_1, k_1)
hold on
plot3(i_2, j_2, k_2)
hold on
plot3(i_3, j_3, k_3)
hold on
plot3(i_4, j_4, k_4)
title('test')
xlabel('x')
ylabel('y')
zlabel('z')
instead of
figure
hold on
plot3(i_1, j_1, k_1)
plot3(i_2, j_2, k_2)
plot3(i_3, j_3, k_3)
plot3(i_4, j_4, k_4)
title('test')
xlabel('x')
ylabel('y')
zlabel('z')
Jonas Freiheit
2022-5-15
编辑:Jonas Freiheit
2022-5-15
Cris LaPierre
2022-5-15
I suggest one minor change to Torsten's code:
figure
plot3(i_1, j_1, k_1)
hold on
plot3(i_2, j_2, k_2)
plot3(i_3, j_3, k_3)
plot3(i_4, j_4, k_4)
hold off
title('test')
xlabel('x')
ylabel('y')
zlabel('z')
Use the Axes Toolbar to rotate the plot. See here for how to access it in a live script:
Jonas Freiheit
2022-5-15
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


