Matlab plot3 not giving a 3D plot
Show older comments
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 Comments
Torsten
on 14 May 2022
Did you see the plots generated by plot3 under
?
Do you have a different expectation about the plot ?
Jonas Freiheit
on 14 May 2022
Torsten
on 15 May 2022
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
on 15 May 2022
Edited: Jonas Freiheit
on 15 May 2022
Cris LaPierre
on 15 May 2022
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
on 15 May 2022
Accepted Answer
More Answers (0)
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

