I am getting an error "Index in position 1 exceeds array bounds. Index must not exceed 1." Error in "v_x(n) = fmincon(fun_x(n), x0, A_x(n,:), b_x(n,:)); Can someone help"

1 次查看(过去 30 天)
Here is the code:
t_span = 30;
dt = 0.01;
t = 0:dt:t_span;
x(1) = 0; %Initial condition for state x
y(1) = 0; %Initial condition for state y
x0 = [0]; %initial condition for v_x in fmincon
y0 = [0]; %initial condition for v_y in fmincon
goal = [3; 5]; %final trajectory coordinate
obs_1_center = [1; 2]; %obstacle coordinate
D_obs = 0.5; %some positive value
R = 0.5; %radius of the obstacle
K = 1; %controller gain
alpha = 0.5; %constant
for n = 1:length(t)
obs_1_x(n) = obs_1_center(1) + R*cos(0.01*n);
obs_1_y(n) = obs_1_center(2) + R*sin(0.01*n);
v_des_x(n) = - K * (x(n) - goal(1));
v_des_y(n) = - K * (y(n) - goal(2));
A_x(n) = - (x(n) - obs_1_x(n)) / norm (x(n) - obs_1_x(n)) ;
A_y(n) = - (y(n) - obs_1_y(n)) / norm (y(n) - obs_1_y(n)) ;
b_x(n) = alpha * ( norm(x(n) - obs_1_x(n)) - D_obs );
b_y(n) = alpha * ( norm(y(n) - obs_1_y(n)) - D_obs );
fun_x{n} = @(v_x)((norm(v_x(n) - v_des_x(n)))^2);
fun_y{n} = @(v_y)((norm(v_y(n) - v_des_y(n)))^2);
v_x(n) = fmincon(fun_x(n), x0, A_x(n,:), b_x(n,:));
v_y(n) = fmincon(fun_y(n), y0, A_y(n,:), b_y(n,:));
x(n+1) = x(n) + dt * (v_x(n));
y(n+1) = y(n) + dt * (v_y(n));
end

采纳的回答

Jan
Jan 2022-12-6
Change:
fun_x{n} = @(v_x)((norm(v_x(n) - v_des_x(n)))^2);
fun_y{n} = @(v_y)((norm(v_y(n) - v_des_y(n)))^2);
v_x(n) = fmincon(fun_x(n), x0, A_x(n,:), b_x(n,:));
v_y(n) = fmincon(fun_y(n), y0, A_y(n,:), b_y(n,:));
to:
fun_x{n} = @(v_x)((norm(v_x - v_des_x(n)))^2);
fun_y{n} = @(v_y)((norm(v_y - v_des_y(n)))^2);
v_x(n) = fmincon(fun_x{n}, x0, A_x(n), b_x(n));
v_y(n) = fmincon(fun_y{n}, y0, A_y(n), b_y(n));
In this definition:
@(v_x)((norm(v_x(n) - v_des_x(n)))^2)
the inout argument v_x is a scalar. Then v_x(n) must fail for n > 1.
A_x(n) = ... creates a row vector, but A_x(n,:) requests the n.th row. A(n) uses the n.th element instead.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by