"Index exceeds the number of array elements (1)." error

I am trying to write two line search functions to help me write code later for implementing different ways to minimize functions using step size. This is what I have so far and I'm not sure why I'm getting this error. I get the same error for both of the algorithms that is
"Index exceeds the number of array elements (1).
Error in sym/subsref (line 900)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in linesearch2 (line 4)
grad = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)); 200*(x(2)-x(1)^2)];"
function [alpha] = linesearch2(nsteps)
syms x
f = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
grad = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)); 200*(x(2)-x(1)^2)];
alpha(0) = 0;
alpha(1) = 1;
c1 = 10^-4;
c2 = 0.9;
for i = 1:nsteps
if (f(alpha(i)) > f(0)+c1*alpha(i)*grad(0)||(f(alpha(i)) > f(alpha(i-1))))
alpha = lszoom(alpha(i-1), alpha(i));
return;
end
if abs(grad(alpha(i))) <= abs(c2*grad(0))
alpha = alpha(i);
return;
end
if grad(alpha(i)) >= 0
alpha = lszoom(alpha(i), alpha(i-1));
return;
end
alpha(i+1) = 2*alpha;
end
Error('step size alpha not found within 10 iterations')
end
And the lszoom function I'm trying to call is
function [alpha] = lszoom(alphalo, alphahigh)
syms x
f = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
grad = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)); 200*(x(2)-x(1)^2)];
alpha(0) = 0;
alpha(1) = 1;
c1 = 10^-4;
c2 = 0.9;
nsteps = 50;
for i = 1:nsteps
alpham = (alphalo + alphahigh) / 2;
if (f(alpham) > alpha(0)+c1*alpham*grad(0)||f(alpham)>f(alphalo))
alphahigh = alpham;
else
if abs(grad(alpham)) <= c2*abs(grad(0))
alpha = alpham;
return;
end
if grad(alpham)*(alphahigh-alphalo) >= 0
alphahigh = alphalo;
end
alphalo = alpham;
end
end
Error('step length alpha not found within 50 iterations')
end
I'm not great at MATLAB so any help/advice would be really wonderful!

Risposte (1)

You made f an anonymous function but your grad is a symbolic expression not an anonymous function. You are invoking grad later as if you expect it to be a function.

6 Commenti

Do you know how to fix this? I can't seem to get anything to work
I updated the code for the linesearch part but it now says "Symbolic function expected 2 input arguments but received 1."
function [a] = linesearch(f)
syms x y
f(x,y) = 100*(y - x^2)^2 + (1 - x)^2;
grad(x,y) = [-400*(y-x^2)*x-2*(1-x); 200*(y-x^2)];
a(1) = 0;
a(2) = 1;
c1 = 10^-4;
c2 = 0.9;
nsteps = 10;
for i = 2:nsteps
if (f(a(i)) > f(0)+c1*a(i)*grad(0)||(f(a(i)) > f(a(i-1))))
a = lszoom(a(i-1), a(i));
return;
end
if abs(grad(a(i))) <= abs(c2*grad(0))
a = a(i);
return;
end
if grad(a(i)) >= 0
a = lszoom(a(i), a(i-1));
return;
end
a(i+1) = 2*a;
end
Error('step size alpha not found within 10 iterations')
end
You are defining grad as requiring that x and y be passed to it. What are you expecting the value of grad(0) to be? Is that x=0 and y symbolic should be calculated? x symbolic and y=0? Should MATLAB be expected to understand it as the vector 0,0 decomposed into parts?
And then later you call grad passing in a(i). Same questions as before about 0, but I would add to that the question of whether you expect a(i) to be a vector that is to be automatically decomposed into x and y?
Well I am calling the function from this function:
function [min] = SteepestDescent(x0)
max = 20000;
tol = 10^-6;
[f, grad, gradTwo] = Rosen(x0);
x = x0;
while norm(grad) >= tol
for k = 1:max
p(k) = -grad(x);
%Choose appropriate stepsize alpha(k) from linesearch%
a(k) = linesearch(x, p(k), grad, f)
x(k+1) = x(k) + (a(k)*p(k));
end
min = x(k+1);
end
end
%Rosen(x) function:
function [f, grad, gradTwo] = Rosen(x)
g = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
f = g(x);
grad = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)); 200*(x(2)-x(1)^2)];
gradTwo = [800*x(1)+2, -400*x(1); -400*x(2), 200];
end
So my a(k) should be derived from the linesearch function.
The algorithm I am going off of is this:
where:
And then my current linesearch is:
function [astar] = linesearch(x0,p,grad,f)
x = x0;
a(1) = 0;
a(2) = 1;
c1 = 10^-4;
c2 = 0.9;
nsteps = 10;
for i = 2:nsteps
phi(a(i)) = f(x(k)+(a(k)*p(k)));
y = phi(a(i));
dfphi(a(i)) = grad(x(k));
z = dfphi(a(i));
if ((y(a(i)) > y(0)+c1*a(i)*z(0)) || (y(a(i)) > y(a(i-1))))
astar = lszoom(a(i-1), a(i));
return;
end
if abs(y(a(i))) <= c2*abs(z(0))
astar = a(i);
return;
end
if z(a(i)) >= 0
astar = lszoom(a(i), a(i-1));
return;
end
a(i+1) = 2*a(i);
end
Error('step size a (alpha) not found within 10 iterations')
end
Read the remarks at the beginning of the second image again: f(x) is described as a function of one variable. Your f and your grad are functions of three variables, and the algorithm does not apply (not unless you are willing to freeze two out of the three variables for the duration of the search.)
The second image talks explicitly about one-dimensional minimization; doing a three-dimension minimization instead violates the warrantee.

Accedi per commentare.

Richiesto:

il 20 Feb 2020

Commentato:

il 21 Feb 2020

Community Treasure Hunt

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

Start Hunting!

Translated by