Out of memory error message when attempting to plot in 3D
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all,
From my following code as I'm trying to plot the trajectory of 3 bugs in three dimensions I am presented with the folllowing error message:
Out of memory. The likely cause is an infinite recursion within the program.
Error in bugs>g (line 37) dxdt=g(t,f)
From my following 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, tspan)
plot3(i_2, j_2, k_2, tspan)
plot3(i_3, j_3, k_3, tspan)
plot3(i_4, j_4, k_4, tspan)
title('test')
xlabel('x')
ylabel('y')
zlabel('z')
function dxdt = g(t,f)
dxdt=g(t,f)
dxdt=[(f(2)-f(1))/(sqrt((f(2)-f(1))^2+(f(6)-f(5))^2+(f(10)-f(9))^2));
(f(6)-f(5))/(sqrt((f(2)-f(1))^2+(f(6)-f(5))^2+(f(10)-f(9))^2));
(f(10)-f(9))/(sqrt((f(2)-f(1))^2+(f(6)-f(5))^2+(f(10)-f(9))^2));
(f(3)-f(2))/(sqrt((f(3)-f(2))^2+(f(7)-f(6))^2+(f(11)-f(10))^2));
(f(7)-f(6))/(sqrt((f(3)-f(2))^2+(f(7)-f(6))^2+(f(11)-f(10))^2));
(f(11)-f(10))/(sqrt((f(3)-f(2))^2+(f(7)-f(6))^2+(f(11)-f(10))^2));
(f(4)-f(3))/(sqrt((f(4)-f(3))^2+(f(8)-f(7))^2+((f12)-f(11))^2));
(f(8)-f(7))/(sqrt((f(4)-f(3))^2+(f(8)-f(7))^2+(f(12)-f(11))^2));
(f(12)-f(11))/(sqrt((f(4)-f(3))^2+(f(8)-f(7))^2+(f(12)-f(11))^2));
(f(1)-f(4))/(sqrt((f(1)-f(4))^2+(f(5)-f(8))^2+(f(9)-f(12))^2));
(f(5)-f(8))/(sqrt((f(5)-f(8))^2+(f(1)-f(4))^2+(f(9)-f(12))^2));
(f(9)-f(12))/(sqrt((f(1)-f(4))^2+(f(5)-f(8))^2+(f(9)-f(12))^2))];
end
Thanks.
0 Kommentare
Akzeptierte Antwort
Jan
am 13 Mai 2022
Bearbeitet: Jan
am 13 Mai 2022
function dxdt = g(t,f)
dxdt=g(t,f)
...
end
This function calls itself recursively. Of course this let Matlab crash. What is the purpose of "dxdt=g(t,f)"? Simply delete this line to solve this problem.
The code block does not look smart. Compare:
dxdt=[(f(2)-f(1))/(sqrt((f(2)-f(1))^2+(f(6)-f(5))^2+(f(10)-f(9))^2));
(f(6)-f(5))/(sqrt((f(2)-f(1))^2+(f(6)-f(5))^2+(f(10)-f(9))^2));
(f(10)-f(9))/(sqrt((f(2)-f(1))^2+(f(6)-f(5))^2+(f(10)-f(9))^2));
(f(3)-f(2))/(sqrt((f(3)-f(2))^2+(f(7)-f(6))^2+(f(11)-f(10))^2));
(f(7)-f(6))/(sqrt((f(3)-f(2))^2+(f(7)-f(6))^2+(f(11)-f(10))^2));
(f(11)-f(10))/(sqrt((f(3)-f(2))^2+(f(7)-f(6))^2+(f(11)-f(10))^2));
(f(4)-f(3))/(sqrt((f(4)-f(3))^2+(f(8)-f(7))^2+(f(12)-f(11))^2)); % BUG: f12->f(12)
(f(8)-f(7))/(sqrt((f(4)-f(3))^2+(f(8)-f(7))^2+(f(12)-f(11))^2));
(f(12)-f(11))/(sqrt((f(4)-f(3))^2+(f(8)-f(7))^2+(f(12)-f(11))^2));
(f(1)-f(4))/(sqrt((f(1)-f(4))^2+(f(5)-f(8))^2+(f(9)-f(12))^2));
(f(5)-f(8))/(sqrt((f(5)-f(8))^2+(f(1)-f(4))^2+(f(9)-f(12))^2));
(f(9)-f(12))/(sqrt((f(1)-f(4))^2+(f(5)-f(8))^2+(f(9)-f(12))^2))];
With:
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];
This is even faster.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Install Products finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!