Message Boards Message Boards

0
|
2732 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Assigning variables from rules

Posted 3 years ago

I'm new to Mathematica. I have to say, it's making me crazy. I want to solve a system of four simultaneous equations in four variables and then have the numerical solutions applied to those variables from the rules so that, in this case,

m=127, b=7,f=14.5588, t=148.559:

solutions = 
Solve[m == 127 && b == 7 && f == 0.098*t && m + b + f == t, {m, b, f,
t}]

Then I want to set m, b, t, and f to the values in the rule assignment. I've tried every permutation I can think of of Part and ReplaceAll, but I either get an error or I get m=m, b=b, etc. It really shouldn't be that difficult, it seems like something users would want to do all the time. I see Stack Exchange with obscure code using @@@, and other stuff. Is there not just a simple way to solve a set of simultaneous equations and assign the results to the variables in the equations?

POSTED BY: Robert Ryan
2 Replies

Hi 'Robert,

it is by no means clear what you want to do. Something like this?

m = 127;
b = 7;
solutions = Solve[{f == 0.098*t, m + b + f == t}, {t, f}]
{t, m} /. Flatten[solutions]
POSTED BY: Hans Dolhaine

Robert,

As Hans points out, its not clear exactly what you want, However, if you are asking how to change rules into assignments, you could do:

solutions /. Rule -> Set;

In Hans' corrected example, this will make assignments for t and m. HOWEVER, I believe that changing the rules to assignments is bad coding practice in Mathematica. I generally like to make notebooks that can be rerun without clearing the global variables or quitting the kernel. Once t and m are defined, the earlier code that creates your equations will no longer work without a Clear["Global`*"] or killing the kernel.

It is preferable to use the results of solving as Hans showed by doing

newexpression /. solutions

to make the variable assignments locally.

regards,

Neil

POSTED BY: Neil Singer
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