Use fsolve for multiple variable problem without complex

5 次查看(过去 30 天)
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

采纳的回答

Ameer Hamza
Ameer Hamza 2018-5-21
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
  4 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Nonlinear Optimization 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by