MATLAB: Use fsolve for multiple variable problem without complex

complexfsolveoptimization

I need to optimize four variables of a function, which is used to model a machine. This is so that, when I use the function, the calculated value dc is the same as the measured value dm. My objective function is the cosine rule with some Pythagoras. I'm trying to use fsolve to optimize Variables X(1) through X(4) so that dc = dm. The fsolve function returns that it can't run more than 800 function evaluations, and I don't know how to change that. Also, the function value eps goes complex if I try replace it with (dzm – sqrt(dzc)).^2. I would really appreciate some help on how to solve this problem without getting it to run into complex as well as to run more function evaluations.
[la, lb, lc] = textread('zt4m.csv','%f, %f, %f');
x = [la, lb, lc] ;
s = size(x);
% la,lb,lc are arm lengths used in dc to get to dm
dm = [0, 5.46, 18.3, 9.45, 13.49, 19.89]'; % the measured value
sd = size(dm);
li = la;
lin = lc;
vec = ones(size(li));
% options = optimset('MaxIter', 10e4);
[X,Fval] = fsolve(@(x) objFun(li,lin,x(1),x(2),x(3),x(4),dm),[400 400 480 73])
dc = (li + vec*X(1)).^2 + (lin + vec*X(2)).^2 - 2.*(li.*lin + li.*X(2) + lin.*X(1) + vec.*X(1).*X(2)).*cosd(X(3));
dzc = dc - (X(4) - 0.0001)^2;
dc = sqrt(dzc)
function eps = objFun(li,lin,Di,Din,beta,b,dzm)
vec = ones(size(li));
dc = (li + vec*Di).^2 + (lin + vec*Din).^2 - 2.*(li.*lin + li.*Din + lin.*Di + vec.*Di.*Din).*cosd(beta);
dzc = dc - b^2;
eps = (dzm.^2 - dzc); % i would like to replace this with (dzm - sqrt(dzc)).^2
end

Best Answer

You are not able to change the maximum function evaluation because you are not passing the option to fsolve(). Being said that, fsolve() is just the wrong tool for minimizing the objective function. Choose an optimizer such as fmincon() to minimize the objective function. Here is how you can do this
[la, lb, lc] = textread('zt4m.csv','%f, %f, %f');
x = [la, lb, lc] ;
s = size(x);
% la,lb,lc are arm lengths used in dc to get to dm
dm = [0, 5.46, 18.3, 9.45, 13.49, 19.89]'; % the measured value
sd = size(dm);
li = la;
lin = lc;
vec = ones(size(li));
options = optimoptions('fmincon', 'MaxFunctionEvaluations', 10e4, 'MaxIter', 10e4);
[X,Fval] = fmincon(@(x) sum(objFun(li,lin,x(1),x(2),x(3),x(4),dm).^2),[400 400 480 73], [], [], [], [], [], [], [], options)
dc = (li + vec*X(1)).^2 + (lin + vec*X(2)).^2 - 2.*(li.*lin + li.*X(2) + lin.*X(1) + vec.*X(1).*X(2)).*cosd(X(3));
dzc = dc - (X(4) - 0.0001)^2;
dc = sqrt(dzc)
function eps = objFun(li,lin,Di,Din,beta,b,dzm)
vec = ones(size(li));
dc = (li + vec*Di).^2 + (lin + vec*Din).^2 - 2.*(li.*lin + li.*Din + lin.*Di + vec.*Di.*Din).*cosd(beta);
dzc = dc - b^2;
eps = (dzm.^2 - dzc); % i would like to replace this with (dzm - sqrt(dzc)).^2
end