3
$\begingroup$

I am trying to do a simple operation of manipulating an equation of multiple variables (say, $x,y,z$), so that I can get one of the these variables on the LHS and everything else on the RHS. It should be a simple operation, but I cannot find a way to do it in Mathematica (I tried exploring Collect, Reduce and Simplify but they don't seem to serve this purpose). And other posts (e.g. here) are too long/complicated to understand for my simple purposes.

Obviously the equation cannot be solved (1 equation in several variables). I just want it to be simply rearranged to be in terms of one variable on one side. For example to rearrange

x y+y^2-1+z y x-24*z+Tan[x]==x^4 

so that all $z$ terms are on LHS and everything else on RHS, like:

z==(x^4-x y-y^2+1-Tan[x])/(y x-24)

I think it should be a simple operation...

UPDATE: Also, how to make this work even if the LHS is to be an expression in one variable ($z$) and not just $z$ itself. For example, if we slightly modify the above example to start with: xy+y^2-1+(z+Cos[z]) y x-24*(z+Cos[z])+Tan[x]==x^4 and we now want it to get the result as z+Cos[z]==(x^4-x y-y^2+1-Tan[x])/(y x-24) ? Of course, by this I imply that Mathematica will need to automatically treat z+Cos[z] (instead of only z) as a new variable, witout me having to tell it to do so.

$\endgroup$
  • 1
    $\begingroup$ Equal @@ Solve[x y + y^2 - 1 + z y x - 24*z + Tan[x] == x^4, z][[1, 1]] $\endgroup$ – Akku14 Jan 9 at 9:14
  • $\begingroup$ Is zyx a variable or a product of 3 variables? $\endgroup$ – yarchik Jan 9 at 18:58
  • $\begingroup$ Is something like ApplySides what you are looking for? $\endgroup$ – Suba Thomas Jan 9 at 21:01
  • $\begingroup$ @yarchik sorry, just updated the post; x y z are separate. But I also mean generally how to isolate one variable from the rest of eqn (regardless of eqn form). $\endgroup$ – user135626 Jan 10 at 20:16
2
$\begingroup$

This function brings everything to one side (f), takes a derivative with respect of variable of interest var and integrates back to get an expression dependent on this variable. This is then the left hand side lhs, the right hand side rhs is formed by the rest of the expression:

Clear[e,iso]
iso[eq_,var_]:=Module[{f,lhs,rhs,g},
f=eq/.Equal[a_,b_]->a-b;
lhs=Integrate[D[f/.{var->g[var]},var],var]//Simplify;
lhs=lhs/.{g[var]->var};
rhs=Simplify[lhs-f];
Equal[lhs,rhs]]

The idea, thus, is to use the reciprocity of derivative and antiderivative operations. Let us test it on 3 examples

e[1]=x y+y^2-1+z y x-24z+Tan[x]==x^4;
e[2]=x y+y^2-1+z y x-24Exp[z^2]+Tan[x]==x^4;
e[3]=x y+y^2-1+z y x-24Cos[x+z]+Tan[x]==x^4;

iso[#,z]&/@{e[1],e[2],e[3]}

$$z (x y-24)=x^4-x y-\tan (x)-y^2+1,$$ $$x y z-24 e^{z^2}=x^4-x y-\tan (x)-y^2+1,$$ $$x y z-24 \cos (x+z)=x^4-x y-\tan (x)-y^2+1$$

Notice, the function works even when it is impossible to Solve for a given variable.

| improve this answer | |
$\endgroup$
  • $\begingroup$ This is a nice way to do it - thanks! This works well when one cannot separate the varaibles completely. However, notice that it sometimes fails to properly simplify the easy and separable cases (e.g. the first example e[1] you mention in your answer didn't work, even though here one would easily isolate z and divide both sides by its coefficient to get z alone on LHS). Any suggestions to refine this? $\endgroup$ – user135626 Jan 11 at 3:55
  • $\begingroup$ And can you please explain the purpose of using {var -> g[var]} and its inverse later? I am a beginner and trying to understand what you did. Thanks! $\endgroup$ – user135626 Jan 11 at 4:15
  • $\begingroup$ @user135626 If var stands explicitly there, Mathematica will try hard to integrate. Sometimes the results of this integration can be very different from the original form. In order to avoid this, the variable is replaced by some indefinite function g[var]. $\endgroup$ – yarchik Jan 11 at 9:01
  • $\begingroup$ I will add simplify to the rhs. This should solve the problem with e[1] that you pointed out in the first question. $\endgroup$ – yarchik Jan 11 at 9:02
  • $\begingroup$ As you mention in the post, there could be complicated cases, where z cannot be isolated. That is why the function does not try to isolate the variable but rather find A and B such that A[z,x,y]==B[x,y]. In simpler cases, you may want to use a simpler solution proposed by @LucaFerroglio. $\endgroup$ – yarchik Jan 11 at 9:12
1
$\begingroup$

With Reduce you can actually solve this problem; you need, though, to specify the variable you want to use to solve it for:

Reduce[xy + y^2 - 1 + zyx - 24*z + Tan[x] == x^4, z]

which yields as a solution

z == 1/24 (-1 - x^4 + xy + y^2 + zyx + Tan[x])

This will obviously work only if your variable can be expressed explicitly.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for this. Will this work in another equation, say, if the LHS was an expression of $z$ and not just $z$? For example, if the LHS should become $z+z^2$ or $z+\cos[z]$, will 'Reduce' be able to isolate all expressions related to $z$ to the LHS? Note that I don't want it to solve the equation. I just want it to re-organise it in a way that isolates $z$ on one side against all others on the other side. $\endgroup$ – user135626 Jan 10 at 20:20
  • $\begingroup$ @user135626 What do you want if the equation contains z + x Cos[z] and the x cannot be separated to get terms depending only on z? $\endgroup$ – Michael E2 Jan 10 at 23:13
  • $\begingroup$ @MichaelE2 Let's say it CAN be separated, but as an expression of z, not only z. So, instead of starting with the expression xy + y^2 - 1 + z y x - 24 z + Tan[x] == x^4, let's assume we started with: xy + y^2 - 1 + z y x - 24 (z + Cos[z]) + Tan[x] == x^4 (only difference is now I added Cos[z] to z). How can Mathematica isolated (z + Cos[z]) simply to one side, and all else to other side? $\endgroup$ – user135626 Jan 10 at 23:21
  • $\begingroup$ @MichaelE2 in the above comment there is an error. I meant to say if we start with: xy + y^2 - 1 + (z+Cos[z]) y x - 24 (z + Cos[z]) + Tan[x] == x^4... See update in main post. $\endgroup$ – user135626 Jan 10 at 23:33
0
$\begingroup$

You can always brute force it.

eq = x y + y^2 - 1 + (z + Cos[z]) y x - 24*(z + Cos[z]) + Tan[x] == x^4

(Solve[eq /. {z + Cos[z] -> f[z]}, f[z]][[1, 1]] /. Rule -> Equal) /. f[z] -> z + Cos[z]
(*z + Cos[z] == (x^4 - Tan[x] - x y - y^2 + 1)/(x y - 24)*)

This, of course, requires that all expressions containing z be the same form.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.