MATLAB: Index exceeds the number of array elements (1)
1 view (last 30 days)
Show older comments
diego sanchez torralba
on 6 May 2021
Commented: diego sanchez torralba
on 6 May 2021
I dont see any problem but, the command window just respond me
####>> EJsteepest
Index exceeds the number of array elements (1).
Error in EJsteepest>@(x)x(1)^3+3*x(1)*x(2)^2-15*x(1)+12*x(2) (line 5)
f = @(x) x(1)^3+3*x(1)*x(2)^2-15*x(1)+12*x(2);
Error in steepest_descent (line 4)
f_hat = f(x_hat);
Error in EJsteepest (line 12)
#######
steepest_descent(f,a,b,x1,E, lambda, delta)
function x_star = steepest_descent(f,a,b,x1,E, lambda, delta)
m = 1;
x_hat =x1;
f_hat = f(x_hat);
I = eye(length(a));
x=zeros(1,length(a));%fila de ceros de 1 hasta j
d=zeros(1,length(a));
while lambda > E
if m > 0
for j = 1:length(a)
d(j) = (f(x_hat + delta*I(j,:))- f(x_hat - delta*I(j,:)))/2*delta;
end
d=-d/norm(d);
m=0;
end
for j=1:length(a)
x(j)=min(max(x_hat(j)+lambda*d(j),a(j)),b(j));
end
f_x = f(x);
if f_x < f_hat
while f_x< f_hat
x_hat = x;
f_hat = f_x;
for j=1:length(a)
x(j)=min(max(x_hat(j)+lambda*d(j),a(j)),b(j));
end
f_x = f(x);
end
m=1;
else
lambda = lambda/2;
end
end
x_star = x_hat;
end
##########NEXT SCRIP
f = @(x) x(1)^3+3*x(1)*x(2)^2-15*x(1)+12*x(2);
a=[3/2 -3/2];
b=[3 0];
E = 10^-1;
lambda=1/2;
x1=(1 -0.3);
delta=10^-2;
steepest_descent(f,a,b,x1,E, lambda, delta)
0 Comments
Accepted Answer
More Answers (1)
KSSV
on 6 May 2021
You are trying to extract more number of elements then present in the array.
Example:
A = rand(1,3) ;
A(1) % no error
A(3) % no error
A(4) % error, becuase there is no 4th element in A
In the same note check, the function
f = @(x) x(1)^3+3*x(1)*x(2)^2-15*x(1)+12*x(2);
It seems in your case x is a scalr and is not of size 1*2.
See Also
Categories
Find more on Matrix Indexing 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!