Call a function with inputs from different sources

1 次查看(过去 30 天)
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.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2015-2-14
编辑:Andrei Bobrov 2015-2-14
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by