# Replacing a expression in another expression with a Variable [duplicate]

I want to set a symbol (or variable) for an expression. Here is an example:

A x^2 + C x + D


I want to reduce this expression to

x^2 + G x + D/A


but Mathematica can't do this way

A x^2 + C x + D /. C/A -> G


The above was just an simple example, but my problem is in complex expression like:

(c Kϕ Sin[(i π z)/l])/hw + (c EIw i^4 π^4 Sin[(i π z)/l])/(hw l^4) -
(c i^2 π^2 (-GJ + r0^2 P[z]) Sin[(i π z)/l])/(hw l^2) +
d Dw (6/hw^2 + β μ) Sin[(π z)/λ] + (c i π r0^2 Cos[(i π z)/l] Derivative[1][P][z])/(hw l)


and I want to replace some part like this:

and first expression convert to:

How can I do that?

## marked as duplicate by bbgodfrey, corey979, Feyre, Mr.Wizard♦Jan 7 '17 at 20:42

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Jan 4 '17 at 17:34
• I am marking this as a duplicate of mathematica.stackexchange.com/q/3822/121 -- unfortunately there is no robust built-in solution for this kind of thing but it has been brought up many times before and there are many partial solutions. See my answer to that question for links to many more related questions. Please try the solutions therein, and if they do not work edit this question to describe what you tried and what the result was, including a link to the source post. – Mr.Wizard Jan 7 '17 at 20:41

A x^2 + C x + D /. {A -> 1, C -> G, D -> D/A}


gives what you expect

• tnx but i said that very simple example to find a way for that complex expression. – Amir Jan 4 '17 at 18:15

The question is ambiguous, in that what variables (if any) are to be eliminated when introducing H1 and F is not specified. One course of action is to name the original expression ex and then perform,

Solve[H1 == (EIy β^2 λ)/2, EIy][[1, 1]]
Solve[F == (3 Dw λ)/hw^3 - Dw*(2 - μ)/(2 hw) λ β, Dw][[1, 1]]
ex /. {%, %%}
(* -((2 c F hw^3 (-(6/hw^3) + (β (2 - μ))/hw) Sin[(π z)/λ])/(λ (6 - 2 hw^2 β +
hw^2 β μ))) + d Kx Sin[(i π z)/λ] + (2 d H1 i^4 π^4 Sin[(i π z)/λ])/(β^2 λ^5) -
(d i^2 π^2 P[z] Sin[(i π z)/λ])/λ^2 + (d i π Cos[(i π z)/λ] Derivative[1][P][z])/λ *)

• I edited my question. please look again. tnx – Amir Jan 4 '17 at 20:02
• Please give your definitions of B1, etc in Mathematica format. – bbgodfrey Jan 4 '17 at 20:24
• Also, F and Q are not in your final expression. Is that intentional? – bbgodfrey Jan 4 '17 at 20:32
• B1 = (GJ [Pi]^2)/(2 [Lambda] hw^2); N1 = (Af r02 [Pi]^2)/(2 [Lambda] hw^2); S1 = (Af [Pi]^2)/(2 [Lambda]); H1 = (EIy [Beta]^2 [Lambda])/2; F = (3 Dw [Lambda])/hw^3 - Dw*(2 - [Mu])/(2 hw) [Lambda] [Beta]; Q = (3 Dw [Lambda])/hw^3 + Dw*[Mu]/(2 hw) [Lambda] [Beta]; – Amir Jan 4 '17 at 20:35
• Because I have 2 expression and i just posted one expression. Is it necessary to send exact formula? – Amir Jan 4 '17 at 20:37