Message Boards Message Boards

0
|
6733 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:
GROUPS:

Assign from Solve to variable with the same name

Posted 12 years ago
Hi, I have a problem:
I use Solve to get the solutions of equations, but here comes the problem. I seem to be unable to find an easy way to assign all the outputs of Solve to variables with the same name at once.

This is how I use Solve
Input: sol := Solve[{eq1, eq2, eq3, eq4, eq5, eq6}, {var1, var2, var3, var4, var5, var6}]

Output: {var1 -> 14515., var2 -> 1242.27, var3 -> 13272.7, var4 -> 1058.78, var5 -> 12213.9, var6 -> 2281.53}
Now I have six outputs and I want to assign them to the variables with the same name. E. g. Solve gives me {var1 -> 14515} and I want to assign to variable called 'var1'. I know it can somehow be done by /.sol[[1]] etc., but I'd like to assign them all at once without having to list every variable and assign it extra.

Could you help me, please? Thank you
POSTED BY: Václav Pelc
4 Replies
Posted 12 years ago
I apologize. I made two mistakes. First I wrote the line claiming it would solve your problem. Second I did not test that before posting. I should be better than that.

Please try this correction
 (* evaluate your code prior to your solve and then this code *)
 sol = Solve[{r1, r2, r3, r4, r5, r6}, vars][[1]];
 
 MapThread[Set, {vars, vars /. sol}];
 
 (* and then look at the value assigned to your variables *)
 nF1
 
 (* and *)
nD1

(* and ... *)
I just checked this and I believe I have corrected my mistake.
Thank you for checking what I did and reporting my mistake.
POSTED BY: Bill Simpson
Posted 12 years ago
Thank you again, that works great!
POSTED BY: Václav Pelc
Posted 12 years ago
Is this acceptable
vars = {var1, var2, var3, var4, var5, var6};

vars = vars /. Solve[{eq1, eq2, eq3, eq4, eq5, eq6}, vars]
POSTED BY: Bill Simpson
Posted 12 years ago
Thank you for your advice, Bill Simpson, but it doesn't seem to work the way I'd like. I'll give you the whole code:
 (*Definiton of equations*)
 r1 = nF1 == nD1 + nW1;
 r2 = xAF1*nF1 == xAD1*nD1 + xAW1*nW1;
 r3 = nW1 == nD2 + nW2;
 r4 = xAW1*nW1 == xAD2*nD2 + xAW2*nW2;
 r5 = nW2 == nD3 + nW3;
 r6 = xAW2*nW2 == xAD3*nD3 + xAW3*nW3;
 
 (*Known parameters*)
xAF1 = 0.2308;
xAD1 = 0.9990;
xAW1 = 0.1589;
xAD2 = 0.9941;
xAW2 = 0.0865;
xAD3 = 0.3171;
xAW3 = 0.03353;
nW3 = 9932.42;

(*Solving of the unknown parameters*)
vars = {nF1, nD1, nW1, nD2, nW2, nD3};
vars = vars /. Solve[{r1, r2, r3, r4, r5, r6}, vars]

(*Here is the part for using solved variables*)
nAF1 = xAF1*nF1
nAD1 = xAD1*nD1
nAW1 = xAW1*nW1
nAD2 = xAD2*nD2
nAW2 = xAW2*nW2
nAD3 = xAD3*nD3
But my ouput still looks like this:
Out[1]= {{14515., 1242.27, 13272.7, 1058.78, 12213.9, 2281.53}}
Out[2]= 0.2308 nF1
Out[3]= 0.999 nD1
Out[4]= 0.1589 nW1
Out[5]= 0.9941 nD2
Out[6]= 0.0865 nW2
Out[7]= 0.3171 nD3
As you can see, it doesn't work since the variables which the equations are solved for aren't assigned. What am I doing wrong? Thank you

P. S. I know these are simple equations but Mathematica is new to me so I try to use it as much as possible to get used to it, and solution of this problem could be useful in the future.
POSTED BY: Václav Pelc
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract