Mathematica How Do You Substitute Variable Values in Mathematica?

  • Thread starter Thread starter Sosi
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
The discussion revolves around using Mathematica to assign a variable to an output and substitute it in an expression. The user initially attempts to substitute a solved variable, X1, into another expression, Vp, but encounters issues with the output remaining unchanged. The solution involves using "ReplaceAll" instead of "Replace," as it applies a more effective algorithm for substitution. Additionally, it is highlighted that the output from the Solve function is a list containing a transformation rather than a direct value. By applying "ReplaceAll" with the first component of the list, the user successfully achieves the desired substitution in the expression.
Sosi
Messages
13
Reaction score
1
Hi!
I'm doing my first steps in Mathematica and I've wanted to do something simple like assigning a variable to the output and then use it. Something like this:

Code:
In:Vp = vprx == kprx2*X1;
In:dX1 = kp X0 + X1 (-cat kcatl - kgpx gpx - kprx2 prx2);
In:rX1 = Solve[dX1 == 0, X1];
Out:{{X1 -> (kp X0)/(catalase kcatl + gpx kgpx + kprx2 prx2)}}

then i want to Sbustitute X1 in the expression Vp, with the value of the output of rX1.I'm using the function Replace but this doesn't do what I want. The output is:
Code:
In:Replace[Vp, X1 -> rX1]
Out:vprx == kprx2 X1

when it should be
Code:
Out:vprx==(kprx2kp X0)/(catalase kcatl + gpx kgpx + kprx2 prx2)

Thanks a lot for all the help!
 
Physics news on Phys.org
There are two problems, one is simple, one is more complicated.

First, use "ReplaceAll" instead of "Replace", it uses a deeper algorithm that will properly do what you want it to do.

Second, look at the result of "rX1". It is NOT just a number that can be plugged in. It is a list that has one component with a variable, "X1", being transformed to a larger expression.

Try using:

Code:
ReplaceAll[Vp, rX1[[1]] ]

This takes the first component of the rX1 list and uses it in the ReplaceAll function.
 
jpreed said:
Try using:
Code:
ReplaceAll[Vp, rX1[[1]] ]
This takes the first component of the rX1 list and uses it in the ReplaceAll function.

Awsome! It works just as I wanted! Thanks a lot!
 

Similar threads

Replies
5
Views
3K
Replies
1
Views
2K
Replies
0
Views
496
Replies
2
Views
3K
Replies
6
Views
8K
Replies
8
Views
5K
Replies
1
Views
2K
Back
Top