How to identify, if the variable is an array or not in a given MATLAB code?
24 次查看(过去 30 天)
显示 更早的评论
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 个评论
Steven Lord
2023-7-19
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
2023-7-19
@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.
采纳的回答
dpb
2023-7-19
编辑:dpb
2023-7-19
_"In the above code, is theta1(n), theta2(n), theta1_hat(n), theta2_hat(n) are arrays or not?"_
As others have noted, in MATLAB, everything is an array of one type or another, so the four variables (and all the rest as well) are arrays, the size of them is what is the question; your code above does not preallocate your variables before computing/addressing them in the loop; hence they are "grown" dynamically each pass through the loop, adding a new element every time n increments. If you'll look, the editor will show you warnings about doing that.
NOTA BENE:, though, that while theta1 are vectors of (eventually) n elements, theta1(n) is simply a single element of that array--it is still an array, but is of size 1 or would show up as True for isscalar(theta1(n))
You should, instead, preallocate all your arrays before beginning the loop with something like
...
t = [0:dt:t_span].'; % I prefer column, not row, but your choice
theta1=zeros(size(t)); % preallocate variables to match t...
theta2=theta1; % etc., ...
...
Also note that
N=numel(t);
theta1(N)=0;
...
has the same effect and may be somewhat faster, besides.
"So,.computing theta1_hat(n+1) is fine..."
No. You've defined your t vector to N elements so all the computed variables should also only exist over the range 1:N, but when you run the loop index n to N, then n+1 is past the array size and, depending upon exact context and usage, either adds an unwanted element to the end of the array (on assignment) or results in an addressing error (if referencing an array of only n elements).
You should only run the loop indices from
for n=1:N-1
...
in order to not run off the end or add the extra, element.
Your prof is correct, you have a logic error in the code...MATLAB, with its dynamic (and silent) memory reallocation, masks such mistakes; languages with fixed memory models such as Fortran, will error and not let you do such things.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!