MATLAB: How can one make this program work? (fmin question)

MATLAB

In Part 1, I have learned that
x_optimal = fminbnd(@fun,0,2)
function out=fun(x)
if x<=1 out=x; else out=sqrt(x); end
out=-out; %because we're maximizing

end
Now I want to parameterize this example. I want to define a parameterized function
function out=fun(x,a)
if x<=1 out=x; else out=a*sqrt(x); end
out=-out; %because we're maximizing
end
And want to create an output vector
x=zeros(10,1);
for a=1:1:10
x(a,1)=fminbnd(@fun,0,2) % minimizes fun(x,a)
end
But this code does not work. How can one make the code work?

Best Answer

x=zeros(10,1);
for a=1:1:10
x(a,1)=fminbnd(@(x) fun(x,a), 0, 2) % minimizes fun(x,a)
end