2
$\begingroup$

I had a problem where I had to solve a series of equations where some of the variables can take multiple value such as shown below

c[0]={0.0871817, 0.0850908, 0.0826321}

c[3]={0.0874968, 0.0857884, 0.0838024}

d[0]={2.23049*10^-6, 6.46928*10^-6, 0.0000128172}

d[1]={2.04172*10^-6, 5.90291*10^-6, 0.0000116484}

d[2]={1.86443*10^-6, 5.37164*10^-6, 0.0000105544}

And I'd like to solve for conc[1] and conc[2]

d[0]*(c[0] - conc[1]) = d[1]*(conc[1] - conc[2])
d[1]*(conc[1] - conc[2]) = d[2]*(conc[2] - c[3])

So I used Solve in such way

Solve[Thread[d[0]*(c[0] - conc[1]) == d[1]*(conc[1] - conc[2])] &&
Thread[d[1]*(conc[1] - conc[2]) == d[2]*(conc[2] - c[3])], {conc[1],
conc[2]}]

which didn't gave me the right answer. I wonder if this is a legit approach or there's any other way to do it? I had a similar question yesterday that prompt me to use Thread for this kind of problems https://mathematica.stackexchange.com/questions/97054/solve-equation-with-a-variable-of-multiple-value I'm not sure if this method fit for my current problem.

Thanks for reading and let me know if you have any questions or suggestions!

$\endgroup$
2
  • 1
    $\begingroup$ What kind of beasts are your conc[ ] supposed to be?Lists? Reals? $\endgroup$ Oct 15, 2015 at 20:49
  • $\begingroup$ Yes, it's supposed to be a List with three values just like the other variables defined. Maybe I have to define those two to be Thread as well? $\endgroup$
    – Fang
    Oct 15, 2015 at 20:53

2 Answers 2

3
$\begingroup$
conc[1] = Array[x, 3];
conc[2] = Array[y, 3];

Solve[d[0]*(c[0]    - conc[1]) == d[1]*(conc[1] - conc[2]) &&
      d[1]*(conc[1] - conc[2]) == d[2]*(conc[2] - c[3]), 
      Flatten[{conc[1], conc[2]}]]

(*
 {{x[1] -> 0.0872775, x[2] -> 0.0853022, x[3] -> 0.0829852, 
   y[1] -> 0.0873822, y[2] -> 0.0855338, y[3] -> 0.0833736}}
*)
$\endgroup$
1
  • $\begingroup$ How could I get the value from $x[1]$,..$y[3]$? these just return themselves expression. $\endgroup$
    – eason
    Mar 9, 2022 at 14:07
1
$\begingroup$

Because d[1] (conc[1] - conc[2]) makes up one side of both equations, it can be eliminated, and the system is shown to be under-determined. Therefore, it has infinitely many solution sets. Belisarius's answer gives one of them, but I know no reason why it should be preferred.

Here is the general solution.

uu = Array[u, 3]; vv = Array[v, 3];
Solve[d[0] (c[0] - uu) == d[2] (vv - c[3]), Flatten @ {uu, vv}]

Solve::svars: Equations may not give solutions for all "solve" variables. >>

{v[1] -> 0.191796 - 1.19634 u[1], 
 v[2] -> 0.188267 - 1.20434 u[2], 
 v[3] -> 0.18415 - 1.21439 u[3]}

We can generate any specific solution with

sol[usol_] := Join[usol, gensol /. usol]

For example,

sol[{u[1] -> 0., u[2] -> 0., u[3] -> 0.}]
{u[1] -> 0., u[2] -> 0., u[3] -> 0., 
 v[1] -> 0.191796,  v[2] -> 0.188267, v[3] -> 0.18415}

or

sol[{u[1] -> 0.08727751016208445, 
     u[2] -> 0.08530217552915369, 
     u[3] -> 0.08298516000872024}]
{u[1] -> 0.0872775, u[2] -> 0.0853022, u[3] -> 0.0829852, 
 v[1] -> 0.0873822, v[2] -> 0.0855338, v[3] -> 0.0833736}

which is belisarius' solution.

$\endgroup$
1
  • $\begingroup$ The reason that this equation is under-determined is because I slightly changed my original code where the second equation is d[nodes - 2]*(conc[nodes - 2] - conc[nodes - 1]) == d[nodes - 1]*(conc[nodes - 1] - c[nodes]) plus an array function And @@ Array[ d[#]*(conc[#] - conc[# + 1]) == d[# + 1]*(conc[# + 1] - conc[# + 2]) &, nodes - 3, 1] I guess the answer I'm looking for is more like how to define a multi-value variable which belisarius suggested using the Array function. I might've overlooked the actual solution to my question. Thanks for pointing that out. $\endgroup$
    – Fang
    Oct 15, 2015 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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