Clear Filters
Clear Filters

How to solve the below equations for unknowns with equal and inequality constraints?

1 view (last 30 days)
equation are:
226*lm1 + 204*lm2 + 242*lm3 + 252*lm4 + 228*lm5 + 176*lm6 + 300*lm7 - 130*lm8 - 170*lm9 - 182*lm10 - 120*lm11 - 148*lm12 - 58*lm13 - 102*lm14 == 0
204*lm1 + 232*lm2 + 212*lm3 + 256*lm4 + 192*lm5 + 192*lm6 + 280*lm7 - 156*lm8 - 124*lm9 - 156*lm10 - 112*lm11 - 152*lm12 - 68*lm13 - 116*lm14 == 0
242*lm1 + 212*lm2 + 260*lm3 + 266*lm4 + 246*lm5 + 184*lm6 + 320*lm7 - 134*lm8 - 186*lm9 - 196*lm10 - 128*lm11 - 156*lm12 - 60*lm13 - 106*lm14 == 0
252*lm1 + 256*lm2 + 266*lm3 + 298*lm4 + 246*lm5 + 216*lm6 + 340*lm7 - 168*lm8 - 172*lm9 - 198*lm10 - 136*lm11 - 176*lm12 - 74*lm13 - 128*lm14 == 0
228*lm1 + 192*lm2 + 246*lm3 + 246*lm4 + 234*lm5 + 168*lm6 + 300*lm7 - 120*lm8 - 180*lm9 - 186*lm10 - 120*lm11 - 144*lm12 - 54*lm13 - 96*lm14 == 0
176*lm1 + 192*lm2 + 184*lm3 + 216*lm4 + 168*lm5 + 160*lm6 + 240*lm7 - 128*lm8 - 112*lm9 - 136*lm10 - 96*lm11 - 128*lm12 - 56*lm13 - 96*lm14 == 0
300*lm1 + 280*lm2 + 320*lm3 + 340*lm4 + 300*lm5 + 240*lm6 + 400*lm7 - 180*lm8 - 220*lm9 - 240*lm10 - 160*lm11 - 200*lm12 - 80*lm13 - 140*lm14 == 0
106*lm8 - 156*lm2 - 134*lm3 - 168*lm4 - 120*lm5 - 128*lm6 - 180*lm7 - 130*lm1 + 74*lm9 + 98*lm10 + 72*lm11 + 100*lm12 + 46*lm13 + 78*lm14 == 0
74*lm8 - 124*lm2 - 186*lm3 - 172*lm4 - 180*lm5 - 112*lm6 - 220*lm7 - 170*lm1 + 146*lm9 + 142*lm10 + 88*lm11 + 100*lm12 + 34*lm13 + 62*lm14 == 0
98*lm8 - 156*lm2 - 196*lm3 - 198*lm4 - 186*lm5 - 136*lm6 - 240*lm7 - 182*lm1 + 142*lm9 + 148*lm10 + 96*lm11 + 116*lm12 + 44*lm13 + 78*lm14 == 0
72*lm8 - 112*lm2 - 128*lm3 - 136*lm4 - 120*lm5 - 96*lm6 - 160*lm7 - 120*lm1 + 88*lm9 + 96*lm10 + 64*lm11 + 80*lm12 + 32*lm13 + 56*lm14 == 0
100*lm8 - 152*lm2 - 156*lm3 - 176*lm4 - 144*lm5 - 128*lm6 - 200*lm7 - 148*lm1 + 100*lm9 + 116*lm10 + 80*lm11 + 104*lm12 + 44*lm13 + 76*lm14 == 0
46*lm8 - 68*lm2 - 60*lm3 - 74*lm4 - 54*lm5 - 56*lm6 - 80*lm7 - 58*lm1 + 34*lm9 + 44*lm10 + 32*lm11 + 44*lm12 + 20*lm13 + 34*lm14 == 0
78*lm8 - 116*lm2 - 106*lm3 - 128*lm4 - 96*lm5 - 96*lm6 - 140*lm7 - 102*lm1 + 62*lm9 + 78*lm10 + 56*lm11 + 76*lm12 + 34*lm13 + 58*lm14 == 0
and equal and inequality constraints are:
%% euality contraints is
lm1 + lm2 + lm3 + lm4 + lm5 + lm6 + lm7 - lm8 - lm9 - lm10 - lm11 - lm12 - lm13 - lm14 == 0;
%% and inequality contraints is
lm1>=0; lm2>=0; lm3>=0; lm4>=0; lm5>=0; lm6>=0;
lm8>=0; lm9>=0; lm7>=0; lm10>=0; lm11>=0; lm12>=0; lm13>=0; lm14>=0;
please help with this equal and inquality constrains to solve the unknows.

Answers (2)

Matt J
Matt J on 10 Dec 2020
Edited: Matt J on 10 Dec 2020
Since your equations and constraints are linear, lsqlin is applicable,

John D'Errico
John D'Errico on 10 Dec 2020
Edited: John D'Errico on 10 Dec 2020
First, LEARN TO USE MATRICES!!!!!!!!!!!!!!!!!!!!!!
LEARN TO USE VECTORS!!!!!!!!!!!!
MATLAB is a matrix language. Don't define your unknowns as separate scalar variables. This will quickly lead you into the land of programming hell, from which it will be difficult to escape.
As for the problem you have posed, it is possible there is no exact non-trivial solution, at least based on what I see at first glance. Let me explore what you have posed.
  1. You have 14 unknown variables.
  2. You have a homogeneous system of 14 LINEAR equations, all of which are expected to hold true.
  3. But then also an equality. Wait, we just said you have a system of 14 equations. Is thios not also an equation that must be true? So you have 15 equations. Or is the latter a linear constraint tht MUST hold true, while the others are merely equations that can be solved, allowing for errors? So there are effectively 15 equations in 14 unknowns. That suggests there may be no solution besides the trivial all-zero solution.
  4. Bound constraints on 10 of the variables, all of which are expected to be non-negative. The other 4 unknowns can apparently be anything at all.
syms lm1 lm2 lm3 lm4 lm5 lm6 lm7 lm8 lm9 lm10 lm11 lm12 lm13 lm14
E(1) = 226*lm1 + 204*lm2 + 242*lm3 + 252*lm4 + 228*lm5 + 176*lm6 + 300*lm7 - 130*lm8 - 170*lm9 - 182*lm10 - 120*lm11 - 148*lm12 - 58*lm13 - 102*lm14 == 0;
E(2) = 204*lm1 + 232*lm2 + 212*lm3 + 256*lm4 + 192*lm5 + 192*lm6 + 280*lm7 - 156*lm8 - 124*lm9 - 156*lm10 - 112*lm11 - 152*lm12 - 68*lm13 - 116*lm14 == 0;
E(3) = 242*lm1 + 212*lm2 + 260*lm3 + 266*lm4 + 246*lm5 + 184*lm6 + 320*lm7 - 134*lm8 - 186*lm9 - 196*lm10 - 128*lm11 - 156*lm12 - 60*lm13 - 106*lm14 == 0;
E(4) = 252*lm1 + 256*lm2 + 266*lm3 + 298*lm4 + 246*lm5 + 216*lm6 + 340*lm7 - 168*lm8 - 172*lm9 - 198*lm10 - 136*lm11 - 176*lm12 - 74*lm13 - 128*lm14 == 0;
E(5) = 228*lm1 + 192*lm2 + 246*lm3 + 246*lm4 + 234*lm5 + 168*lm6 + 300*lm7 - 120*lm8 - 180*lm9 - 186*lm10 - 120*lm11 - 144*lm12 - 54*lm13 - 96*lm14 == 0;
E(6) = 176*lm1 + 192*lm2 + 184*lm3 + 216*lm4 + 168*lm5 + 160*lm6 + 240*lm7 - 128*lm8 - 112*lm9 - 136*lm10 - 96*lm11 - 128*lm12 - 56*lm13 - 96*lm14 == 0;
E(7) = 300*lm1 + 280*lm2 + 320*lm3 + 340*lm4 + 300*lm5 + 240*lm6 + 400*lm7 - 180*lm8 - 220*lm9 - 240*lm10 - 160*lm11 - 200*lm12 - 80*lm13 - 140*lm14 == 0;
E(8) = 106*lm8 - 156*lm2 - 134*lm3 - 168*lm4 - 120*lm5 - 128*lm6 - 180*lm7 - 130*lm1 + 74*lm9 + 98*lm10 + 72*lm11 + 100*lm12 + 46*lm13 + 78*lm14 == 0;
E(9) = 74*lm8 - 124*lm2 - 186*lm3 - 172*lm4 - 180*lm5 - 112*lm6 - 220*lm7 - 170*lm1 + 146*lm9 + 142*lm10 + 88*lm11 + 100*lm12 + 34*lm13 + 62*lm14 == 0;
E(10) = 98*lm8 - 156*lm2 - 196*lm3 - 198*lm4 - 186*lm5 - 136*lm6 - 240*lm7 - 182*lm1 + 142*lm9 + 148*lm10 + 96*lm11 + 116*lm12 + 44*lm13 + 78*lm14 == 0;
E(11) = 72*lm8 - 112*lm2 - 128*lm3 - 136*lm4 - 120*lm5 - 96*lm6 - 160*lm7 - 120*lm1 + 88*lm9 + 96*lm10 + 64*lm11 + 80*lm12 + 32*lm13 + 56*lm14 == 0;
E(12) = 100*lm8 - 152*lm2 - 156*lm3 - 176*lm4 - 144*lm5 - 128*lm6 - 200*lm7 - 148*lm1 + 100*lm9 + 116*lm10 + 80*lm11 + 104*lm12 + 44*lm13 + 76*lm14 == 0;
E(13) = 46*lm8 - 68*lm2 - 60*lm3 - 74*lm4 - 54*lm5 - 56*lm6 - 80*lm7 - 58*lm1 + 34*lm9 + 44*lm10 + 32*lm11 + 44*lm12 + 20*lm13 + 34*lm14 == 0;
E(14) = 78*lm8 - 116*lm2 - 106*lm3 - 128*lm4 - 96*lm5 - 96*lm6 - 140*lm7 - 102*lm1 + 62*lm9 + 78*lm10 + 56*lm11 + 76*lm12 + 34*lm13 + 58*lm14 == 0;
[A,B ] = equationsToMatrix(E)
A = 
B = 
So there is your basic linear system. That is, a HOMOGENEOUS linear system. A homogeneous system is special, because it means your right hand side is all zero. As such, a trivial solution to the problem is all zeros for the unknowns. Do you disagree that would be a perfectly valid solution?
DA = double(A);
DB = double(B);
rank(DA)
ans = 2
So effectively, you have only TWO pieces of information there. Not 14 equations. Just TWO. And then you repeated the same two pieces of information 12 additional times, saying exactly the same story. (A lot of effort wasted typing out all those equations for no good reason.)
How about the extra equation? We can write that as
Aeq = [ones(1,7),-ones(1,7)];
Beq = 0;
Does that yield any additional information to the problem?
rank([DA;Aeq])
ans = 3
So effectively you now have 3 equations. And 14 unknowns. Plus some lower bound constraints, on SOME of the unknowns.
All of this means my initial guess was wrong, since you actually have a rank deficient linear system of equation. In fact, there will be infinitely many solutions. You cannot "solve" this though, at least not in any meaningful way that you would want to write down. You can very arbitrarily find SOME solution.
If you pick at least one of the unknowns to be non-zero, then you can solve for some solution, out of the infinitely many possible solutions. Simplest is to just use lsqlin.
LB = [zeros(10,1);-inf(4,1)]; % 10 unknonws are bounded from below by 0, the others have no bounds
LB(1) = 1;
So I'll force the first unknown to be greater than or equal to 1. Essentially this converts the homogeneous problem into a problem that is no longer homogeneous.
X = lsqlin(DA,DB,[],[],Aeq,Beq,LB)
Minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
X = 14×1
1.9990 1.0008 0.9997 0.9992 1.0002 1.0002 0.9994 1.0011 0.9992 0.9988
This is a valid solution to your problem. One of infinitely many. And while we could list all infinitely many solutions, that will take some time. I'll start it running now, and let you know when it is done...
Edit: I see that you want to solve the problem without using lsqlin, writing your own code?
Can I say what a bad idea this would be? Never write your own code to do poorly what is already done well by existing professionally written code.
If you insist however, you can find some basic algorithms for this in pseudocode. I would look for algorithm LSEI, which effectively does the same thing as LSQLIN. You should probably find code for it in Lawson & Hanson, "Solving Least Squares Problems". They might even show MATLAB code. Is it a bad idea? It still is. If you don't know enough to have been able to do what I already did here, then you are going to have a hard time re-writing an LSEI code yourself. But good luck.

Tags

Community Treasure Hunt

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

Start Hunting!