How to identify, if the variable is an array or not in a given MATLAB code?
Show older comments
I have the following MATLAB code:
How can I identify which one is array and which one is not an array?
clc
clear all
close all
t_span = 500;
dt = 0.1;
t = 0:dt:t_span;
%%% Initial positions and angles of drones
x1(1) = 800;
y1(1) = 1200;
psi1(1) = 0;
x2(1) = 200;
y2(1) = 400;
psi2(1) = pi/2;
%%% Constants
v0 = 25;
rd = 300;
alpha1(1) = 1;
alpha2(1) = 1;
theta_d = pi;
k = 0.01;
K = 1; % Turning rate gain value
center = 0;
x_t(1) = 0;
y_t(1) = 0;
x_t_dot(1) = 0;
y_t_dot(1) = 0;
% Communication parameters
communication_interval = 1; % Periodic communication interval in seconds
communication_counter = round(communication_interval / dt); % Counter to track communication timing
communication1 = false(size(t));
communication2 = false(size(t));
alpha_filter = 0.1;
for n = 1:length(t)
disp(['Current time = ', num2str(n*dt)])
x_r1(n) = x1(n) - x_t(n);
y_r1(n) = y1(n) - y_t(n);
x_r2(n) = x2(n) - x_t(n);
y_r2(n) = y2(n) - y_t(n);
r1(1, n) = sqrt( (x_r1(n))^2 + (y_r1(n))^2 );
r2(1, n) = sqrt( (x_r2(n))^2 + (y_r2(n))^2 );
xd_dot1(n) = -alpha1 * (v0 / r1(n)) * ( x_r1(n)*( (r1(n)^2 - rd.^2) / (r1(n)^2 + rd.^2) ) + y_r1(n) * ( 2 * r1(n)*rd / (r1(n)^2 + rd.^2) ));
yd_dot1(n) = -alpha1 * (v0 / r1(n)) * ( y_r1(n)*( (r1(n)^2 - rd.^2) / (r1(n)^2 + rd.^2) ) - x_r1(n) * ( 2 * r1(n)*rd / (r1(n)^2 + rd.^2) ));
xd_dot2(n) = -alpha2 * (v0 / r2(n)) * ( x_r2(n)*( (r2(n)^2 - rd.^2) / (r2(n)^2 + rd.^2) ) + y_r2(n) * ( 2 * r2(n)*rd / (r2(n)^2 + rd.^2) ));
yd_dot2(n) = -alpha2 * (v0 / r2(n)) * ( y_r2(n)*( (r2(n)^2 - rd.^2) / (r2(n)^2 + rd.^2) ) - x_r2(n) * ( 2 * r2(n)*rd / (r2(n)^2 + rd.^2) ));
theta1(n) = atan2(y_r1(n), x_r1(n));
theta2(n) = atan2(y_r2(n), x_r2(n));
del_theta(n) = wrap(theta2(n) - theta1(n));
psi_d1(n) = atan2(yd_dot1(n), xd_dot1(n));
psi_d2(n) = atan2(yd_dot2(n), xd_dot2(n));
psid_dot1(n) = (4 * alpha1 * v0 * rd * r1(n)^2) / (r1(n)^2 + rd^2)^2;
psid_dot2(n) = (4 * alpha2 * v0 * rd * r2(n)^2) / (r2(n)^2 + rd^2)^2;
e_psi1(n) = wrap(psi1(n) - psi_d1(n));
e_psi2(n) = wrap(psi2(n) - psi_d2(n));
u12(n) = -K * e_psi1(n) + psid_dot1(n);
if u12(n) > 0.2
u12(n) = 0.2;
elseif u12(n) < -0.2
u12(n) = -0.2;
end
u22(n) = -K * e_psi2(n) + psid_dot2(n);
if u22(n) > 0.2
u22(n) = 0.2;
elseif u22(n) < -0.2
u22(n) = -0.2;
end
if mod(n, communication_counter) == 0
theta1_hat(n) = theta1(n);
theta2_hat(n) = theta2(n);
communication1(n) = true;
communication2(n) = true;
else
theta1_hat(n+1) = theta1(n) + dt * (v0 / rd);
theta2_hat(n+1) = theta2(n) + dt * (v0 / rd);
end
u11(n) = k * (rd / r1(n))^2 * wrap(theta2_hat(n) - theta1(n) - theta_d)*rd + (v0);
u21(n) = k * (rd / r2(n))^2 * wrap(theta2(n) - theta1_hat(n) - theta_d)*rd + (v0);
if u11(n) < (v0 - 5)
u11(n) = v0-5;
elseif u11(n) > (v0 + 5)
u11(n) = v0 + 5;
end
if u21(n) < (v0 - 5)
u21(n) = v0-5;
elseif u21(n) > (v0 + 5)
u21(n) = v0 + 5;
end
%%% Dynamics
x1(n+1) = x1(n) + dt * (u11(n) * cos(psi1(n)));
y1(n+1) = y1(n) + dt * (u11(n) * sin(psi1(n)));
psi1(n+1) = psi1(n) + dt * (u12(n));
x2(n+1) = x2(n) + dt * (u21(n) * cos(psi2(n)));
y2(n+1) = y2(n) + dt * (u21(n) * sin(psi2(n)));
psi2(n+1) = psi2(n) + dt * (u22(n));
%%% Target Dynamics
x_t(n+1) = x_t(n) + dt * (0);
y_t(n+1) = y_t(n) + dt * (0);
end
6 Comments
Pallov Anand
on 19 Jul 2023
Stephen23
on 19 Jul 2023
"How can I identify which one is array and which one is not an array?"
All MATLAB variables are matrices/arrays, as the documentation explains:
What exactly are you trying to find out?
All variables in MATLAB are arrays. Some of them are also matrices as detected by the ismatrix function.
Are you perhaps trying to detect whether those variables are matrices, vectors, and/or scalars? If so in addition to ismatrix take a look at isvector and isscalar. If you also care about the orientation of the vectors the isrow and iscolumn functions will also be of use.
scalarX = classifyVariable(1)
rowVectorX = classifyVariable(1:10)
columnVectorX = classifyVariable((1:10).')
matrixX = classifyVariable(magic(4))
ndArrayX = classifyVariable(ones(2, 3, 4))
function results = classifyVariable(x)
results = table(isscalar(x), isvector(x), ismatrix(x), isrow(x), iscolumn(x), ...
'VariableNames', ["scalar", "vector", "matrix", "row", "column"]);
end
Pallov Anand
on 19 Jul 2023
Steven Lord
on 19 Jul 2023
In the above code, my for is loop is running for n = 1:length(t) i.e., n will take values 1, 1.1, 1.2, .......
Incorrect. In the expression 1:length(t) the step between consecutive elements is 1 not 0.1. So that loop will run for n = 1, n = 2, n = 3, ... n = length(t).
dpb
on 19 Jul 2023
@Pallov Anand, you did create a time vector, t, however, and I'd recommend using it then in the loop...you instead recomputed it every time step with
x_t(n+1) = x_t(n) + dt * (0);
y_t(n+1) = y_t(n) + dt * (0);
which, of course, is same as
x_t(n+1)=x_t(n);
but, unless you didn't copy over something, x_t, y_t are never set after being initialized to zero so they'll remain there.
But, you would be better off using
t(n)
when you need the actual time, although it doesn't appear to ever be actually used anywhere.
Accepted Answer
More Answers (0)
Categories
Find more on Resizing and Reshaping 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!