MATLAB: I have an optimization problem but i can’t impose the constraints i want, please help. The constraint i want is psi between -pi and pi

fminsearchoptimazation

function f = objectivefcn1(a)
psi = 0;
L=1;
ds=L/3999;
s=0:ds:L;
for n = 1:100
psi = psi + a(n)*cos(2*pi*n*s);
end
f=-(trapz(s,cos(psi))*(1-0.5).*(1-trapz(s,cos(psi).^2)))/(1-0.5*trapz(s,cos(psi).^2));
end
a0=zeros(1,100);
a0(1)=1;
options = optimset('MaxFunEvals', 10000*80,'MaxIter ',10000*80);
a = fminsearch(@objectivefcn1,a0,options);

Best Answer

According to the documentation, fminsearch will "Find minimum of unconstrained multivariable function using derivative-free method" (emphasis added.) So no, you cannot add constraints to a call to fminsearch. There is a function for performing bounded minimization in MATLAB, fminbnd, but that will "Find minimum of single-variable function on fixed interval" (emphasis added.)
Since you have a multivariate function and you want to constrain the solution, you're going to need something like fmincon from Optimization Toolbox or one of the optimizers from Global Optimization Toolbox.