# How to define variables in terms of other variables in a mathematica program for simplifying an expression?

I'm new to Mathematica and trying to learn it on my own from various internet resources. I have the following question. How do I simplify the expression $$X=\frac{-b\pm\sqrt{b^2-4ac}}{2a}\tag{1}$$ where $a,b,c$ are real, known expressions in terms of other parameters (say, $x,y,z$). Again $x,y,z$ are in turn real, known functions of yet another set of real parameters (say, $p,q,r$).

How do I simplify the algebraic expression $X=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$ to obtain $X$ in terms of $p,q,r$. Thank you.

An example For an example, consider $a=-x+y+z, b=x-y+z, c=x+y-z$. Then $x=p^2+q^2$, $y=q^2+r^2$ and $z=p^2+r^2$.

• You can try the Simplify or the FullSimplify commands? It is better to make a MWE to illustrate the issue. – Nasser Sep 5 '17 at 11:16
• @Nasser Thanks. But after writing (1) how does one give the inputs i.e., $a=a(x,y,z)$, $b(x,y,z)$, $c(x,y,z)$, and in the next step $x(p,q,r), y(p,q,r), z(p,q,r)$, in order for the mathematica to evaluate $x$ as a function of $p,q,r$ i.e., $x(p,q,r)$? – SRS Sep 5 '17 at 11:20
• Something like Simplify[expr /. {a -> a[x, y, z], b -> b[x, y, z], c -> c[x, y, z]}] (and analogously for p, q, r) ought to work. We can't give more helpful feedback unless you show your actual expressions. – J. M.'s discontentment Sep 5 '17 at 11:27
• @J.M. I have given an example of what I have in mind. Does that help? – SRS Sep 5 '17 at 11:30
• Then, have you tried the operation I suggested? – J. M.'s discontentment Sep 5 '17 at 11:31

Your question is answered by the Applying Transformation Rules tutorial and the ReplaceAll documentation page.

For example,

a + b /. {a -> x + y, b -> x + x^2}
(* 2 x + x^2 + y *)

a + b /. {a -> x + y, b -> x + x^2} /. {x -> p - q, y -> p + q}
(* p + 2 (p - q) + (p - q)^2 + q *)

Expand[%]
(* 3 p + p^2 - q - 2 p q + q^2 *)

• This is really helpful. If it works out I'll accept the answer. @Szabolcs – SRS Sep 5 '17 at 11:43

a[x_, y_, z_]:=−x+y+z
b[x_, y_, z_]:=x−y+z
c[x_, y_, z_]:=x+y−z


then

x[p_, q_, r_]:=p^2+q^2
y[p_, q_, r_]:=q^2+r^2
z[p_, q_, r_]:=p^2+r^2


and finally substitute in your original expresson like so:

-b[x[p,q,r],y[p,q,r],z[p,q,r]]...

• Sorry I didn't quite understand the last line. I'm trying to compute $X$ using the expression (1). @user42582 – SRS Sep 5 '17 at 11:39
• after having evaluated the definitions for a[x_,y_,z_], b[x_,y_,z_], c[x_,y_,z_],x[p_,q_,r_],y[p_,q_,r_],z[p_,q_,r_] you should use these newly defined functions in place of $a,b,c$ in your relation (1) eg replace the value of $b$ with b[x[p,q,r],y[p,q,r],z[p,q,r]] etc. (Perhaps you should also look at the solution by @Szabolcs above-it depends on a central theme in Mathematica :rules and replacing parts of expressions.) – user42582 Sep 5 '17 at 11:47