Use fsolve for multiple variable problem without complex

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 日

0 投票

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 件のコメント

Peter Labuschagne
Peter Labuschagne 2018 年 5 月 21 日
Why do you use sum(objFun().^2))? Not sure about the sum or why you square the objective function.
Ameer Hamza
Ameer Hamza 2018 年 5 月 21 日
編集済み: Ameer Hamza 2018 年 5 月 21 日
Because fmincon minimize a scalar objective function. But your system has a vector output. One way to convert such system to scalar form is to minimize the L2 norm of the output vector. For example, consider this, you have 3 equations
Eq1(x,y,z) = 0;
Eq2(x,y,z) = 0;
Eq3(x,y,z) = 0;
fsolve() solves the above system of equation. But there is another approach to solve this system. From these three equations, I can make a scalar objective function as following
objFun = Eq1(x,y,z)^2 + Eq2(x,y,z)^2 + Eq3(x,y,z)^2
note that the value of this objective function will always be positive because of squares (L2 norm). The minimum of objFun can be zero. So when the objective function will minimize, the value of Eq1(x,y,z), Eq2(x,y,z) and Eq3(x,y,z) will get close to zero, which is what we require when we are trying to solve the equation. In other words, the optimization problem is equivalent to the equation solving the problem. Except that, now we are putting soft-constraints to make the equation equal to zero. Such a method is particularly useful when the given system of equations does not have a solution.
Peter Labuschagne
Peter Labuschagne 2018 年 5 月 21 日
Thank you, Ameer. This has been extremely helpful!
Ameer Hamza
Ameer Hamza 2018 年 5 月 21 日
You are welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeNonlinear Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by