3
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$ – user13892 Jan 19 at 0:38
5
$\begingroup$

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.

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

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}]

enter image description here enter image description here

This is because

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

enter image description here

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} *)
$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$ – John Jan 19 at 1:09
4
$\begingroup$
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.}}

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.