Index exceeds the number of array elements (1)
Show older comments
function [tg] = TO(x0, eps)
obj = @(x) 7*x(1)^5 + 20*x(2)^5 + x(1)^3 + 12*x(2)^3;
gradient = @(x)[35*x(1)^4 + 3*x(1)^2; 100*x(2)^4 + 36*x(2)^2];
hessiana = @(x)[140*x(1)^3 + 6*x(1) 0;0 400*x(2)^3 + 72*x(2)];
x = x0;
tg = x0;
while(norm(gradient(x))>eps)
grad = gradient(x);
obj_alfa = @(alfa)obj(x - alfa*grad);
alfa_star = fminbnd(obj_alfa, 0, 1);
x = x - alfa_star*grad;
tg = [tg x];
end
x = x0;
tn = x0;
while(norm(gradient(x))>eps)
grad = gradient(x);
hess = hessiana(x);
d_newton = inv(hess)*grad;
obj_alfa = @(alfa) obj(x - alfa*d_newton);
alfa_star = fminbnd(obj_alfa, 0, 1);
x = x - alfa_star*d_newton;
tn = [tn x];
end
It gives the error in the subject line every time I try to call the function and I don't see why.
2 Comments
Image Analyst
on 12 Jan 2019
Edited: Image Analyst
on 12 Jan 2019
eps is a built-in function. Don't call your variable that. Call it eps1 or something. Now, how did you call it? For example did you do
x0 = rand(1, 500);
eps1 = .003;
tg = TO(x0, eps1)
or what??? You didn't just click the green run triangle without specifying x0 and eps1 did you?
Alexa Dumi
on 12 Jan 2019
Edited: Alexa Dumi
on 12 Jan 2019
Accepted Answer
More Answers (0)
Categories
Find more on Linear Algebra 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!