MATLAB: Hello, I am trying to do optimization and i keep getting the error! “Maximum number of function evaluations has been exceeded increase MaxFunEvals option” I have crosschecked the function it looks okay. I will appreciate some help. Below is the cod

AA=1019067.465;
BB=1.866977063;
CC = 543553.1777;
DD = 221576.9168;
format long g
x1min = 0; x1max = 50;
x2min = 0; x2max = 50;
x3min = 0; x3max = 50;
fun1=@(x)(((AA-sum(A.*real(200*exp(-((E-x(1)).^2/x(2).^2)).*(1+erf(x(3)*(E-x(1))/(sqrt(2)*x(2)))))))^2)+((BB-sum(B.*real(200*exp(-((E-x(1)).^2/x(2).^2)).*(1+erf(x(3)*(E-x(1))/(sqrt(2)*x(2)))))))^2)+(CC-sum(C.*real(200*exp(-((E-x(1)).^2/x(2).^2)).*(1+erf(x(3)*(E-x(1))/(sqrt(2)*x(2)))))))^2+(DD-sum(D.*real(200*exp(-((E-x(1)).^2/x(2).^2)).*(1+erf(x(3)*(E-x(1))/(sqrt(2)*x(2)))))))^2)+ 10^100 * any(x < 0);
[X1, X2, X3] = ndgrid( linspace(x1min, x1max, 50), linspace(x2min, x2max, 50),linspace(x3min, x3max, 50));
[xvals, fvals,exitflag] = arrayfun(@(x1,x2,x3) fminsearch(fun1,[x1,x2,x3], optimset('TolX', 1e-6, 'MaxFunEvals', 200000, 'MaxIter', 200000)), X1, X2, X3, 'Uniform', 0);
[bestfval, minidx] = min(cell2mat(fvals(:)));
bestx1x2x3 = xvals{minidx};

Best Answer

In your first group of code, the A.* section, the real() that follows extends only to the 200*exp(-((E-x(1)).^2/x(2).^2)) and not to the "(1+erf" etc. In all the other code groups, the real() extends over the whole 200*exp(-((E-x(1)).^2/x(2).^2)) together with the "(1+erf" . Was that deliberate?