Solving non linear system
2 views (last 30 days)
Show older comments
Ahmed Elsayed
on 14 Feb 2021
Commented: Alex Sha
on 17 Feb 2021
I'm trying to solve a non-linear min-max systems using the below code. and i'm geeting this error
Error using symengine
Input arguments must be convertible to floating-point numbers.
Error in sym/privBinaryOp (line 1030)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in sym/min (line 78)
C = privBinaryOp(X, Y, 'symobj::zipWithImplicitExpansion', 'symobj::minComplex');
Any suggestions to fix it?
syms x111 x114 x121 x124 x211 x214 x221 x224
eq1 = min((min(3*x111,8*x111)+min(2*x211,11*x211)),(6*min(3*x111,8*x111)+6*min(2*x211,11*x211)))+min((7*min(3*x121,8*x121)+7*min(2*x221,11*x221)),(-3*max(3*x124,8*x124)-3*max(2*x224,11*x224)))+min((2*min(x111,7*x111)+2*min(3*x211,7*x211)),(-5*max(x114,7*x114)-5*max(3*x214,7*x214)))+min((min(x121,7*x121)+min(3*x221,7*x221)),(5*min(x121,7*x121)+5*min(2*x221,7*x221)))==-964;
eq2 = min ((6* min (3* x111, 8 *x111)+6* min (2* x211, 11 *x211)),(-max (3*x114, 8* x114) -max (2* x214, 11* x214)))+min ((min (3 *x121, 8* x121)+min (2* x221, 11* x221)),(7* min (3* x121, 8* x121)+7 *min (2 *x221, 11 *x221)))+ min ((3 *min (x111, 7* x111)+3 *min (3* x211, 7* x211)), (6 *min (x111, 7* x111)+6* min (3 *x211, 7* x211)))+min ((4 *min (x121, 7 *x121) +4 *min (3* x221, 7* x221)),(-3 *max (x124, 7 *x124) -3* max (3* x224, 7 *x224)))== -793;
eq3 = min ((min (-8 *x114, -x114) +min (-4 *x214, 6* x211)), (6 *min (-8* x114, -x114) +6 *min (-4* x214, 6* x211)))+min ((7* min (-8* x124, -x124) +7* min (-4* x224, 6* x221)), (-3* max (-x121, -8* x121) -3* max (6* x224, -4* x221)))+min ((2* min (-5* x114, -x114) +2* min (-6 *x214, 2* x211)), (-5* max (-x111, -5* x111) -5* max (2* x214, -6* x211)))+min ((min (-5* x124, -x124) +min (-6* x224, 2* x221)), (5* min (-5* x124, -x124) +5 *min (-6* x224, 2* x221))) == -1331;
eq4 = min ((6* min (-8* x114, -x114) +6* min (-4 *x214, 6* x211)), (-max (-x111, -8* x111) -max (6* x214, -4* x211)))+min ((min (-8* x124, -x124) +min (-4 *x224, 6* x221)), (7* min (-8* x124, -x124) +7 *min (-4* x224, 6* x221)))+min ((3* min (-5* x114, -x114) +3* min (-6* x214, 2* x211)), (6* min (-5* x114, -x114) +6* min (-6* x214, 2* x211)))+min ((4* min (-5 *x124, -x124) +4* min (-6* x224, 2* x221)), (-3* max (-x121, -5* x121) -3* max (2* x224, -6* x221))) == -1476;
eq5 = max ((max (3* x114, 8* x114) +max (2* x214, 11* x214)), (6* max (3* x114, 8* x114) +6 *max (2 *x214, 11 *x214)))+max ((-3 *min (3 *x121, 8* x121) -3* min (2 *x221, 11* x221)), (7 *max (3* x124, 8* x124) +7* max (2* x224, 11* x224)))+max ((-5* min (x111, 7* x111) -5 *min (3* x211, 7* x211)), (2 *max (x114, 7 *x114) + 2* max (3* x214, 7* x214)))+max ((max (x124, 7 *x124) +max (3 *x224, 7* x224)), (5 *max (x124, 7* x124) +5 *max (3 *x224, 7* x224))) == 1816;
eq6 = max ((-min (3* x111, 8* x111) -min (2* x211, 11* x211)), (6* max (3* x114, 8* x114) +6 *max (2* x214, 11* x214)))+max ((max (3* x124, 8* x124) +max (2* x224, 11* x224)), (7 *max (3 *x124, 8* x124) +7* max (2* x224, 11* x224)))+max ((3* max (x114, 7* x114) +3 *max (3 *x214, 7* x214)), (6* max (x114, 7* x114) +6* max (3 *x214, 7* x214)))+max ((-3* min (x121, 7* x121) -3* min (3 *x221, 7* x221)), (4* max (x124, 7* x124) +4* max (3 *x224, 7 *x224))) == 2019;
eq7 = max ((max (-x111, -8* x111) +max (6 *x214, -4 *x211)), (6* max (-x111, -8* x111) +6* max (6 *x214, -4 *x211)))+max ((-3* min (-8* x124, -x124) -3 *min (-4* x224, 6 *x221)), (7 *max (-x121, -8* x121) +7 *max (6 *x224, -4* x221)))+max ((-5* min (-5* x114, -x114) -5 *min (-6 *x214, 2* x211)), (2* max (-x111, -5* x111) +2 *max (2* x214, -6* x211)))+max ((max (-x121, -5 *x121) +max (2* x224, -6 *x221)), (5* max (-x121, -5 *x121) +5 *max (2 *x224, -6* x221))) == 968;
eq8 = max ((-min (-8 *x114, -x114) -min (-4 *x214, 6 *x211)), (6* max (-x111, -8* x111) +6* max (6* x214, -4 *x211)))+max ((max (-x121, -8 *x121) +max (6* x224, -4* x221)), (7 *max (-x121, -8 *x121) +7 *max (6* x224, -4* x221)))+max ((3 *max (-x111, -5 *x111) +3 *max (2* x214, -6 *x211)), (6 *max (-x111, -5* x111) +6* max (2 *x214, -6 *x211)))+max ((-3* min (-5* x124, -x124) -3 *min (-6* x224, 2* x221)), (4* max (-x121, -5 *x121) +4 *max (2 *x224, -6 *x221))) == 843;
sol = fsolve([eq1 eq2 eq3 eq4 eq5 eq6 eq7 eq8], [x111 x114 x121 x124 x211 x214 x221 x224]);
x1Sol = sol.x111;
x2Sol = sol.x114;
x3Sol = sol.x121;
x4Sol = sol.x124;
x5Sol = sol.x211;
x6Sol = sol.x214;
x7Sol = sol.x221;
x8Sol = sol.x224;
6 Comments
Alex Sha
on 16 Feb 2021
Hi, some error or wrong input:
1: "6x112" should be "6*x112";
2: "==-206&&" should be "==-206;";
3: "-3X223,5X222" should be "-3*X223,5*X222";
4: "X*213" should be "X213";
5: "2Min(" should be "2*Min(".
After doing above modifications, the result will be:
x112: -6.7405060869832
x212: 2.36971338564915
x122: 9.82757565025085
x222: 2.73979655042066
x123: 2.66935775949842
x223: 3.48702836315802
x113: 8.53397905414663
x213: -0.859553149820282
Alex Sha
on 17 Feb 2021
One more solution:
x112: -9.12751728247536
x212: -4.85242981963661
x122: 40.4685284423888
x222: 18.2309312718438
x123: -30.3270190436273
x223: 17.4965264223753
x113: 13.8173196565403
x213: -4.695181312283
Accepted Answer
Walter Roberson
on 14 Feb 2021
syms x111 x114 x121 x124 x211 x214 x221 x224
min = @(A,B) piecewise(A<=B, A, B)
max = @(A,B) piecewise(A>=B, A, B)
eq1 = min((min(3*x111,8*x111)+min(2*x211,11*x211)),(6*min(3*x111,8*x111)+6*min(2*x211,11*x211)))+min((7*min(3*x121,8*x121)+7*min(2*x221,11*x221)),(-3*max(3*x124,8*x124)-3*max(2*x224,11*x224)))+min((2*min(x111,7*x111)+2*min(3*x211,7*x211)),(-5*max(x114,7*x114)-5*max(3*x214,7*x214)))+min((min(x121,7*x121)+min(3*x221,7*x221)),(5*min(x121,7*x121)+5*min(2*x221,7*x221)))==-964;
eq2 = min ((6* min (3* x111, 8 *x111)+6* min (2* x211, 11 *x211)),(-max (3*x114, 8* x114) -max (2* x214, 11* x214)))+min ((min (3 *x121, 8* x121)+min (2* x221, 11* x221)),(7* min (3* x121, 8* x121)+7 *min (2 *x221, 11 *x221)))+ min ((3 *min (x111, 7* x111)+3 *min (3* x211, 7* x211)), (6 *min (x111, 7* x111)+6* min (3 *x211, 7* x211)))+min ((4 *min (x121, 7 *x121) +4 *min (3* x221, 7* x221)),(-3 *max (x124, 7 *x124) -3* max (3* x224, 7 *x224)))== -793;
eq3 = min ((min (-8 *x114, -x114) +min (-4 *x214, 6* x211)), (6 *min (-8* x114, -x114) +6 *min (-4* x214, 6* x211)))+min ((7* min (-8* x124, -x124) +7* min (-4* x224, 6* x221)), (-3* max (-x121, -8* x121) -3* max (6* x224, -4* x221)))+min ((2* min (-5* x114, -x114) +2* min (-6 *x214, 2* x211)), (-5* max (-x111, -5* x111) -5* max (2* x214, -6* x211)))+min ((min (-5* x124, -x124) +min (-6* x224, 2* x221)), (5* min (-5* x124, -x124) +5 *min (-6* x224, 2* x221))) == -1331;
eq4 = min ((6* min (-8* x114, -x114) +6* min (-4 *x214, 6* x211)), (-max (-x111, -8* x111) -max (6* x214, -4* x211)))+min ((min (-8* x124, -x124) +min (-4 *x224, 6* x221)), (7* min (-8* x124, -x124) +7 *min (-4* x224, 6* x221)))+min ((3* min (-5* x114, -x114) +3* min (-6* x214, 2* x211)), (6* min (-5* x114, -x114) +6* min (-6* x214, 2* x211)))+min ((4* min (-5 *x124, -x124) +4* min (-6* x224, 2* x221)), (-3* max (-x121, -5* x121) -3* max (2* x224, -6* x221))) == -1476;
eq5 = max ((max (3* x114, 8* x114) +max (2* x214, 11* x214)), (6* max (3* x114, 8* x114) +6 *max (2 *x214, 11 *x214)))+max ((-3 *min (3 *x121, 8* x121) -3* min (2 *x221, 11* x221)), (7 *max (3* x124, 8* x124) +7* max (2* x224, 11* x224)))+max ((-5* min (x111, 7* x111) -5 *min (3* x211, 7* x211)), (2 *max (x114, 7 *x114) + 2* max (3* x214, 7* x214)))+max ((max (x124, 7 *x124) +max (3 *x224, 7* x224)), (5 *max (x124, 7* x124) +5 *max (3 *x224, 7* x224))) == 1816;
eq6 = max ((-min (3* x111, 8* x111) -min (2* x211, 11* x211)), (6* max (3* x114, 8* x114) +6 *max (2* x214, 11* x214)))+max ((max (3* x124, 8* x124) +max (2* x224, 11* x224)), (7 *max (3 *x124, 8* x124) +7* max (2* x224, 11* x224)))+max ((3* max (x114, 7* x114) +3 *max (3 *x214, 7* x214)), (6* max (x114, 7* x114) +6* max (3 *x214, 7* x214)))+max ((-3* min (x121, 7* x121) -3* min (3 *x221, 7* x221)), (4* max (x124, 7* x124) +4* max (3 *x224, 7 *x224))) == 2019;
eq7 = max ((max (-x111, -8* x111) +max (6 *x214, -4 *x211)), (6* max (-x111, -8* x111) +6* max (6 *x214, -4 *x211)))+max ((-3* min (-8* x124, -x124) -3 *min (-4* x224, 6 *x221)), (7 *max (-x121, -8* x121) +7 *max (6 *x224, -4* x221)))+max ((-5* min (-5* x114, -x114) -5 *min (-6 *x214, 2* x211)), (2* max (-x111, -5* x111) +2 *max (2* x214, -6* x211)))+max ((max (-x121, -5 *x121) +max (2* x224, -6 *x221)), (5* max (-x121, -5 *x121) +5 *max (2 *x224, -6* x221))) == 968;
eq8 = max ((-min (-8 *x114, -x114) -min (-4 *x214, 6 *x211)), (6* max (-x111, -8* x111) +6* max (6* x214, -4 *x211)))+max ((max (-x121, -8 *x121) +max (6* x224, -4* x221)), (7 *max (-x121, -8 *x121) +7 *max (6* x224, -4* x221)))+max ((3 *max (-x111, -5 *x111) +3 *max (2* x214, -6 *x211)), (6 *max (-x111, -5* x111) +6* max (2 *x214, -6 *x211)))+max ((-3* min (-5* x124, -x124) -3 *min (-6* x224, 2* x221)), (4* max (-x121, -5 *x121) +4 *max (2 *x224, -6 *x221))) == 843;
eqns = [eq1 eq2 eq3 eq4 eq5 eq6 eq7 eq8]
EQFUN = matlabFunction(eqns, 'vars', {[x111 x114 x121 x124 x211 x214 x221 x224]}, 'file', 'q744767_eqns.m', 'optimize', false)
!cat q744767_eqns.m
sol = fsolve(EQFUN, [x111 x114 x121 x124 x211 x214 x221 x224]);
x1Sol = sol.x111;
x2Sol = sol.x114;
x3Sol = sol.x121;
x4Sol = sol.x124;
x5Sol = sol.x211;
x6Sol = sol.x214;
x7Sol = sol.x221;
x8Sol = sol.x224;
Execution might take a while... A long while. Just to build the function properly :(
2 Comments
Walter Roberson
on 14 Feb 2021
Note: when you use piecewise() with matlabFunction then you must send the output to a file, where it will be turned into if/else . matlabFunction() cannot deal with piecewise() in a pure anonymous function.
Walter Roberson
on 14 Feb 2021
Interesting, but I can't say that I am astonished.
piecewise() resolves out most (or all) of the branches, so it has a fairly high fan-out.
Your system is not suitable for fsolve(), by the way. fsolve() expects equations with continuous derivatives, but min() and max() do not have continuous derivatives.
Perhaps fminmax would help? Though you might need to rewrite some of the max() in terms of the negative of the min() of the negative of the expressions; https://www.mathworks.com/help/optim/ug/fminimax.html
More Answers (0)
See Also
Categories
Find more on Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!