Ode45 producing NaN values only
Show older comments
Hello,
I am working on a matlab program to plot the x,y positions of 5 particles based on a differential equation describing their motion and the influence that each particle has on eachother's motions. I attempted to make the code scalable to any N number of particles. To run the program you call the following:
clc
clear
[t,s] = ode45('NthSwarmDynamics', [0 50], [1 1 0, 2 2 0, 3 3 0, 4 4 0, 5 5 0, 1 1 0, 1 1 0, 1 1 0, 1 1 0, 1 1 0]);
%plot X1_x and X1_y
plot3(s(:,1),s(:,2),t,':b');
hold on;
%plot X2_x and X2_y
plot3(s(:,4),s(:,5),t,':r');
hold on;
%plot X3_x and X3_y
plot3(s(:,7),s(:,8),t,':g');
hold on;
%plot X4_x and X4_y
plot3(s(:,10),s(:,11),t,':m');
hold on;
%plot X5_x and X5_y
plot3(s(:,13),s(:,14),t,':c');
hold on;
The input to this code is t, and s. s is further broken down such that the first half of the vector is the x,y,z positions for each particle (3 coordinates X 5 particles = 15 values, seen here at 1,1,0 2,2,0, etc) and the second half of the vector is the x,y,z velocities for each particle.
The NthSwarmDynamics file follows:
%SwarmDynamics: Models the dynamics of a N-agent swarm in 3D
%S is the position variable
%V is the velocuty variable
function dSdt = NthSwarmDynamics(t, Z)
dimZ = length(Z);
S = Z(1:dimZ/2);
V = Z(dimZ/2+1:dimZ);
N = length(S)/3;
U_x = zeros(N,N);
U_y = zeros(N,N);
U_z = zeros(N,N);
alpha = 1;
beta = 1;
mass = 1;
C_a = 1;
l_a = 2;
C_r = 2;
l_r = 1;
for i = 1:N
for j = 1:N
if i == j
U_x(i,j) = 0;
U_y(i,j) = 0;
U_z(i,j) = 0;
else
r_x = S(i) -S(3*(j-1)+1);
r_y = S(i+1) -S(3*(j-1)+2);
r_z = S(i+2) -S(3*(j-1)+3);
M = sqrt((S(i)-S(3*(j-1)+1))^2+(S(i+1)-S(3*(j-1)+2))^2+(S(i+2)-S(3*(j-1)+3))^2);
U_x(i,j) = (C_a/l_a)*exp(-M/l_a)*(r_x/M)-(C_r/l_r)*exp(-M/l_r)*(r_x/M);
U_y(i,j) = (C_a/l_a)*exp(-M/l_a)*(r_y/M)-(C_r/l_r)*exp(-M/l_r)*(r_y/M);
U_z(i,j) = (C_a/l_a)*exp(-M/l_a)*(r_z/M)-(C_r/l_r)*exp(-M/l_r)*(r_z/M);
end
end
end
dSdt = zeros(length(Z),1);
j=1;
u=1;
for i=1:6:length(dSdt)
dSdt(i) = V(j);
dSdt(i+1) = V(j+1);
dSdt(i+2) = V(j+2);
dSdt(i+3) = ((alpha - beta * ((sqrt(V(j)^2 + V(j+1)^2 + V(j+2)^2))^2)*V(j))- sum(U_x(u,1:N)))/mass;
dSdt(i+4) = ((alpha - beta * ((sqrt(V(j)^2 + V(j+1)^2 + V(j+2)^2))^2)*V(j+1))- sum(U_y(u,1:N)))/mass;
dSdt(i+5) = ((alpha - beta * ((sqrt(V(j)^2 + V(j+1)^2 + V(j+2)^2))^2)*V(j+2))- sum(U_z(u,1:N)))/mass;
j = j+3;
u = u+1;
end
When I run the swarm plotter function, I got an empty plot and upon inspecting my s vector, there is nothing but NaN values. Does anyone know where my flaw is?
Thank you so much for your time, Jeremy
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices 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!