Call a function with inputs from different sources

1 view (last 30 days)
I am trying to input an anonymous function into a full function so that the full function can evaluate the anonymous function. The problem seems to be that the anonymous function is in a loop along with the full function and two of the variables in the anonymous function are meant to update at every interation but it doesn't seem to update.
for i = 1:5
d = -df(x)
%x = x+a*d;
ff = @(x, d) ((x(1)+a*d(1)) + 4*(x(2)+a*d(2)) - 3)^2 +...
(2*(x(1)+a*d(1)) + 5*(x(2)+a*d(2)) - 15)^2;
fff = @(a) ff(x, d);
if norm(d) == 0 || abs(norm(d)) < 0.1
break
end
alpha = Golden_section(fff, 0)
x = x +(alpha*d)
end
just wondering if there's any way for me to do this. As you can see above, I tried inputting the first two variables before calling the function in the golden section function but it just says that a does not exists.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 14 Feb 2015
Edited: Andrei Bobrov on 14 Feb 2015
try is it:
ff = @(x, d,a) ((x(1)+a*d(1)) + 4*(x(2)+a*d(2)) - 3)^2 +...
(2*(x(1)+a*d(1)) + 5*(x(2)+a*d(2)) - 15)^2;
x = ...; % input initial x
for i = 1:5
d = -df(x);
if norm(d) == 0 || abs(norm(d)) < 0.1
break
end
alpha = Golden_section(@(a)ff(x,d,a), 0);
x = x +(alpha*d);
end

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!