Call a function with inputs from different sources

1 Ansicht (letzte 30 Tage)
Samuel Mustapha
Samuel Mustapha am 14 Feb. 2015
Kommentiert: Samuel Mustapha am 15 Feb. 2015
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.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 14 Feb. 2015
Bearbeitet: Andrei Bobrov am 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

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by