Your main function calls broyden with a seven element vector as its first input and a handle to the F function as its second input.
The broyden function calls feval to evaluate the F function with one input, the seven element vector. The f on this line does NOT refer to your f function but to the function handle stored in the variable named f.
You may have expected that MATLAB would automatically expand that vector to pass each element into F as a separate input w1 through w7.
function y = F(w1,w2,w3,w4,w5,w6,w7)
It doesn't. In this call w1 is a seven element vector and w2 through w7 do not exist. [You can add a call to the whos function as the first line inside your F function to check this; that whos output will show only w1 in the workspace.] Thus when you try to use w2 on this line inside F:
MATLAB realizes you called F with not enough input arguments and so throws an error.
You could modify F so it accepts one input argument and refers to elements of that input argument. The first two lines would then be the following.
function y = F(w)
y1 = f(1/2,w(1),w(2));
end
Actually, I'd recommend preallocating y and filling it in as you go. Then you don't need to assemble the pieces together at the end.
function y = F(w)
y = zeros(7, 1);
y(1) = f(1/2,w(1),w(2));
end
Alternately you could modify your broyden function to pass each element of the vector into F individually. But I'd prefer the former, as among other things it's less typing.
fr = feval(f, xv(1), xv(2), xv(3), xv(4), xv(5), xv(6), xv(7));
Best Answer