# How can I solve a system of linear equations? [closed]

How can I solve this system of equations in Mathematica?

DCp1total = WA1*DCpA0 + WB1*DCpB0
DCp2total = WA2*DCpA0 + WB2*DCpB0
WA1 + WB1 = 1
WA2 + WB2 = 1

DCp1_total = 0.6
DCp2_total = 0.3
DCpA0 = 0.5
DCpB0 = 0.4


Unkowns: WA1, WA2, WB1 and WB2.

• Change DCp1_total to DCp1Total and DCp2_total to DCp2Total. _ is a reserved character. Change the = to == and then give your equations to Solve. I don't think your system of equations has a solution. But you can always check. – user13892 Jan 19 at 0:38

For the revised problem statement:

DCp1Total = 3/5;
DCp2Total = 3/10;
DCpA0 = 1/2;
DCpB0 = 2/5;

eq[1] = DCp1Total == WA1*DCpA0 + WB1*DCpB0 // Simplify;
eq[2] = DCp2Total == WA2*DCpA0 + WB2*DCpB0 // Simplify;
eq[3] = WA1 + WB1 == 1;
eq[4] = WA2 + WB2 == 1;

min = Minimize[Total[(Subtract @@@ (eq /@ Range[4]))^2], {WA1, WA2, WB1, WB2}]

(* {0, {WA1 -> 2, WA2 -> -1, WB1 -> -1, WB2 -> 2}} *)


Or,

sol = Solve[eq /@ Range[4], {WA1, WA2, WB1, WB2}]

(* {{WA1 -> 2, WA2 -> -1, WB1 -> -1, WB2 -> 2}} *)


EDIT: To restrict variables to positive values, add constraints.

min2 = Minimize[{Total[(Subtract @@@ (eq /@ Range[4]))^2],
Thread[{WA1, WA2, WB1, WB2} > 0]} // Flatten, {WA1, WA2, WB1, WB2}]

(* Minimize::wksol: Warning: there is no minimum in the region in which the
objective function is defined and the constraints are satisfied; returning a
result on the boundary. *)

(* {43/442, {WA1 -> 31/26, WA2 -> 0, WB1 -> 0, WB2 -> 13/17}} *)


Since the minimum is not zero, there is no solution and you can only minimize the objective.

• Bob thank so much this is great! One quick question: How can I restrict so that all unknowns can only be positive values? – John Jan 19 at 1:28
• Bob, another quick question: How can I get the results in the fraction rather than 13/17? Thanks again – John Jan 19 at 2:02
• If you want the fractions converted to decimal use N – Bob Hanlon Jan 19 at 2:04
• min is a list. The replacement rules are the second element in the outer list. Use WA1 /. min[[2]] – Bob Hanlon Jan 20 at 16:06

There is no solution. But you can use least squares approximation

ps. do not use _ in variable names in Mathematica as _ is meant be used for pattern and you will get in trouble if you do that.

(*from clean kernel*)

DCpA0 = 1/2;
DCpB0 = 4/10;
WA0 = 7/10;
WB0 = 3/10;
DCp1Total = 6/10;
DCp2Total = 3/10;

eq1 = DCp1Total == WA1*DCpA0 + WB1*DCpB0;
eq2 = DCp2Total == WA2*DCpA0 + WB2*DCpB0;
eq3 = WA1 + WA2 == WA0;
eq4 = WB1 + WB2 == WB0;

sol = Solve[{eq1, eq2, eq3, eq3}, {WA1, WA2, WB1 , WB2}]


This is because

{b,A}=CoefficientArrays[{eq1,eq2,eq3,eq4},{WA1,WA2,WB1 ,WB2}];
MatrixForm[A]


Det[A]
(*0   so the matrix is singular.   Try least squares *)

{WA1, WA2, WB1 , WB2} = LeastSquares[A, -b] // N

(* {0.577533, 0.211679, 0.332026, 0.0393432} *)

• Nasser thank you. When I put other parameters for example it gives the same results. I updated a bit one of the equations that perhaps makes it a little bit easier – John Jan 19 at 1:09
Solve[{DCp1total == WA1*DCpA0 + WB1*DCpB0,
DCp2total == WA2*DCpA0 + WB2*DCpB0, WA1 + WB1 == 1, WA2 + WB2 == 1,
DCp1total == 0.6, DCp2total == 0.3, DCpA0 == 0.5, DCpB0 == 0.4}]


{{WA1 -> 2., WA2 -> -1., WB1 -> -1., WB2 -> 2.}}