Matlab plot3 not giving a 3D plot
168 次查看(过去 30 天)
显示 更早的评论
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 个评论
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:
采纳的回答
Steven Lord
2022-5-15
Since no axes existed when you called hold on, MATLAB created one using the default 2-D view and "locked" that axes property in place. Locking certain axes properties in place, so MATLAB doesn't automatically change them, is one of the main purposes of the hold function. So when you called plot3 it would have changed the view but effectively hold said "You can't do that."
The code @Cris LaPierre posted called plot3 before hold so the axes that was "locked" used the 3-D view. You could also call view to manually change the view. hold prevents MATLAB from automatically changing properties, but it doesn't prevent you the user from manually changing them.
figure
hold on
plot3(1:10, 1:10, 1:10) % Uses the 2-D view
view(3) % Manually change it to be in 3-D view
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Formatting and Annotation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!