index exceeds array bound meaning?
Afficher commentaires plus anciens
what is the meaning of index exceeds array bound
3 commentaires
suman sourabh
le 27 Mar 2022
clc
clear
%Step 1: Define decision variables
x =optimvar('x',2,'LowerBound',[0 0],'UpperBound',[5 5]);
%Step 2: Define objective function
obj = x(1)^2 + x(2)^2 + x(3)^2;
%Step 3: Define constraints (if any)
A = 1 -x(2)^(-1)* x(3) >= 0;
B = x(1) - x(3) >=0;
C = x(1) - x(2)^2+x(2)*x(3)-4 ==0;
%Step 4: Define objective problem
prob = optimproblem('Objective',obj,'ObjectiveSense','maximize');
prob.Constraints.con_1 = A;
prob.Constraints.con_2 = B;
prob.Constraints.con_2 = C;
%Step 5: Solve the problem
x0.x = [0 0];
%Defining initial starting point
[sol, fval, exitflag, output] = solve(prob,x0);
suman sourabh
le 27 Mar 2022
kindly resolve this
Walter Roberson
le 29 Mar 2022
You have
x =optimvar('x',2,'LowerBound',[0 0],'UpperBound',[5 5]);
so you say that x is a vector of 2 optimization variables. Your upper bound and lower bound are both vectors of length 2, which is consistent. x is not ambiguous: it really is length 2.
obj = x(1)^2 + x(2)^2 + x(3)^2;
but there you try to use x(3)^2 which would require that x be of length 3 or more.
Your constaints A, B, C all require that x has at least 3 elements.
x0.x = [0 0];
but that says that the initial value is a vector of length 2.
You need to decide whether your function involves 2 variables or 3 variables.
Réponse acceptée
Plus de réponses (1)
CECILIA ABASCAL FALAGAN
le 16 Août 2020
0 votes
Good morning,
I would like to obtain the real time of my computer to later carry out operations and obtain the elapsed time. I have used the "datetime" command but I can't separate the time from the date and use it for this purpose. Any suggestion? I would be very grateful
1 commentaire
Walter Roberson
le 16 Août 2020
Most of the time, you would use tic and toc to obtain elapsed time.
You can also use
tin = clock;
do your code
tout = clock;
how_long = etime(tout, tin) %seconds
You can also
tin = datetime();
do your code
tout = datetime();
how_long = seconds(tout - tin) %seconds
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!