Trajectorys of 4 objects on 3D plane not symmetrical
Show older comments
Hi all,
Sorry i'm a beginner at Matlab and am having issues with getting 4 spots on a 3D plane with the intitial conditions (0,0,0), (1,0,3), (1,2,0), (0,2,3) to symmetrically meet at (0.5,1,1.5).
My 2D simulation appeared like this:

But from my 3D simulation they appeared like this:

Im unsure what line of code I need to change to fix this from my code.
My 3D code is given below:
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(:,5));
k_1=(f(:,9));
i_2=(f(:,2)); %Trajectory of bug 2
j_2=(f(:,6));
k_2=(f(:,10));
i_3=(f(:,3)); %Trajectory of bug 3
j_3=(f(:,7));
k_3=(f(:,11));
i_4=(f(:,4)); %Trajectory of bug 4
j_4=(f(:,8));
k_4=(f(:,12));
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')
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
%My 2D code is here:
tinitial = 0;
tfinal = 1000;
tspan = linspace(tinitial, tfinal, 1000000);
f0 = [0; 0; 1; 1; 0; 2; 2; 0];
[t, f] = ode45(@g, tspan, f0);
j = (f(:, 1));
k = (f(:, 5));
a = (f(:, 2));
b = (f(:, 6));
d = (f(:, 3));
e = (f(:, 7));
h = (f(:, 4));
i = (f(:, 8));
figure
hold on
plot3(j, k, tspan)
plot3(a, b, tspan)
plot3(d, e, tspan)
plot3(h, i, tspan)
title('test')
xlabel('x')
ylabel('y')
zlabel('t')
function dfdt = g(t,f)
dfdt = [(f(2) - f(1))/(sqrt((f(2)-f(1))^2 + (f(6)-f(5))^2));
(f(3)-f(2))/(sqrt((f(3)-f(2))^2 + (f(7)-f(6))^2));
(f(4) - f(3))/(sqrt((f(4)-f(3))^2 + (f(8)-f(7))^2));
(f(1) - f(4))/(sqrt((f(1)-f(4))^2 + (f(5)-f(8))^2));
(f(6) - f(5))/(sqrt((f(2)-f(1))^2 + (f(6)-f(5))^2));
(f(7) - f(6))/(sqrt((f(3)-f(2))^2 + (f(7)-f(6))^2));
(f(8) - f(7))/(sqrt((f(4)-f(3))^2 + (f(8)-f(7))^2));
(f(5) - f(8))/(sqrt((f(1)-f(4))^2 + (f(5)-f(8))^2))];
end
Cheers
2 Comments
Torsten
on 16 May 2022
I you change the plot command in the 2d-case as you did in the 3d-case, are the curves still as expected ?
figure
plot3(j,k,tspan)
hold on
plot3(a,b,tspan)
plot3(d,e,tspan)
plot3(h,i,tspan)
hold off
title('test')
xlabel('x')
ylabel('y')
zlabel('t')
Jonas Freiheit
on 17 May 2022
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!