2
$\begingroup$

I want to solve equation with different variable and list answers in a table how can Solve it in a loop (Without repeating the equation)

a[1] = 14; b[1] = 16; c[1] = 3;
a[2] = 10; b[2] = 10; c[2] = 13;
a[3] = 19; b[3] = 15; c[3] = 7;
a[4] = 17; b[4] = 11; c[4] = 6;
Solve[{ E[1]*0.01 + F[1]*0.04 + a[1] == b[1], E[1]* 0.04 +  F[1]*0.02 + a[1] == c[1]}, {E[1], F[1]}]
Solve[{ E[2]*0.01 + F[2]*0.04 + a[2] == b[2], E[2]* 0.04 +  F[2]*0.02 + a[2] == c[2]}, {E[2], F[2]}]
Solve[{ E[3]*0.01 + F[3]*0.04 + a[3] == b[3], E[3]* 0.04 +  F[3]*0.02 + a[3] == c[3]}, {E[3], F[3]}]
olve[{ E[4]*0.01 + F[4]*0.04 + a[4] == b[4], E[4]* 0.04 +  F[4]*0.02 + a[4] == c[4]}, {E[4], F[4]}]

how can I do it?

$\endgroup$
6
$\begingroup$

First off, don't use capital letters, you'll run into trouble like you did there (E is defined as the natural log unit $e$). You can do what you are going for with a Table,

a = {14, 10, 19, 17};
b = {16, 10, 15, 11};
c = {3, 13, 7, 6};

Table[
 {e, f} /. 
  First@Solve[{e*0.01 + f*0.04 + a[[n]] == b[[n]], 
     e*0.04 + f*0.02 + a[[n]] == c[[n]]}, {e, f}]
 , {n, 4}]

(* {{-342.857, 
  135.714}, {85.7143, -21.4286}, {-285.714, -28.5714}, {-228.571, \
-92.8571}} *)

Just to explain some of that notation, when I type a[[n]] it is accessing the $n^{th}$ element of the list a. For the first case,

Solve[{e*0.01 + f*0.04 + a[[1]] == b[[1]], 
  e*0.04 + f*0.02 + a[[1]] == c[[1]]}, {e, f}]
(* {{e -> -342.857, f -> 135.714}} *)

Notice that Solve returns a list of replacement rules, so we use the /. notation

{e, f} /. {{e -> -342.857, f -> 135.714}}
(* {{-342.857, 135.714}} *)

But then we notice that it gives the answer with an extra pair of curly brackets around it (why? I don't know). We can do away with those by using the First command, which is equivalent to typing [[1]] afterwards,

{e, f} /. First@{{e -> -342.857, f -> 135.714}}
(* {-342.857, 135.714} *)

So you just wrap that up in a Table and run it from 1 to 4.

If you want to learn about pure functions, and Apply, you could write it like this as well, and get the same result.

({e, f} /. 
    First@Solve[{e*0.01 + f*0.04 + #1 == #2, 
       e*0.04 + f*0.02 + #1 == #3}, {e, f}]) & @@@ Transpose@{a, b, c}
$\endgroup$
1
  • $\begingroup$ thank you for taking the time to reply to my problem. $\endgroup$ – asal Dec 18 '15 at 13:25
7
$\begingroup$
a = {14, 10, 19, 17};
b = {16, 10, 15, 11};
c = {3, 13, 7, 6};
eq[a_,b_,c_] := Solve[{e*0.01+f*0.04 +a ==b,e*0.04 +f*0.02 + a == c},{e, f}];
MapThread[eq, {a, b, c}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ thank you for taking the time to reply to my problem. $\endgroup$ – asal Dec 18 '15 at 13:25
4
$\begingroup$

If the equation is linear (as in your example) a good built-in function to try is LinearSolve.

a = {14, 10, 19, 17};
b = {16, 10, 15, 11};
c = {3, 13, 7, 6};

Your equations can be re-written as:

e*0.01 + f*0.04 == b - a
e*0.04 + f*0.02 == c - a

So the matrix input to LinearSolve will be:

{{0.01, 0.04}, {0.04, 0.02}}

Mathematica graphics

LinearSolve allows a matrix to be the second argument in order to get multiple solutions.

For this case that matrix will be:

b2=b-a;
c2=c-a;

{b2,c2};

The output needs to be transposed:

Transpose@LinearSolve[{{0.01, 0.04}, {0.04, 0.02}}, {b2, c2}]
(* {{-342.857, 135.714}, {85.7143, -21.4286}, 
    {-285.714, -28.5714},{-228.571,-92.8571}} *)
$\endgroup$
2
$\begingroup$
a = {14, 10, 19, 17};
b = {16, 10, 15, 11};
c = {3, 13, 7, 6};
arr = Array[m, {2, 4}];
sol=Solve[{{0.01, 0.04}.arr + a == b, {0.04, 0.02}.arr + a == c}, 
 Flatten[arr]]

yields:

{{m[1, 1] -> -342.857, m[1, 2] -> 85.7143, m[1, 3] -> -285.714, 
  m[1, 4] -> -228.571, m[2, 1] -> 135.714, m[2, 2] -> -21.4286, 
  m[2, 3] -> -28.5714, m[2, 4] -> -92.8571}}

If you want to extract e and f:

{e, f} = arr /. sol[[1]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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