# plot function against composite variable

How can one plot $f(x,y)$ as a pseudo-univariate function of $x/y$ (or some other composition of these variables) over the domain $a<\frac{x}{y}<b$. Plot[f,{x/y,a,b}] produces the following error

Plot::write: Tag Times in x/y is Protected. >>


The alternative is to redefine the function in terms of a single variable (i.e., replace all occurrences of x/y with a new variable z); however, I wish to avoid tedious manual redefinition if possible.

Edit: As a simple example, consider the function

$$f(x,y) = \frac{y^2}{x^2}\times\exp(x/y)+5\frac{x}{y}.$$

This function could be redefined as

$$g(z)=\frac{\exp(z)}{z^2}+5z$$

where $z\equiv x/y$.

One could redefine their function $f(x,y)$ as $g(z)$, but it would be more convenient if one could simply plot $f(x,y)$ against $x/y$ as a pseudo-univariate function.

-
Can you give a concrete example? Do f[1,1] and f[2,2] give the same answer? –  Brett Champion Mar 14 '12 at 3:48
Hi @BrettChampion. Yes, f[1,1] and f[2,2] give the same answer. I have edited my post to include a concrete example. –  user001 Mar 14 '12 at 3:50
This is an ill-posed problem. There are arbitrarily many ways to write a real number as a fraction of two others. –  Jens Mar 14 '12 at 6:10

Here's an approach that uses Rationalize to turn a real number into a rational number, and extracts the numerator and denominator to be used when evaluating f[x,y].

f[x_, y_] := y^2/x^2 Exp[x/y] + 5 x/y

g[xy_?NumericQ] :=
With[{r = Rationalize[xy, 0]},
f[Numerator[r], Denominator[r]]
]

Plot[g[xy], {xy, 0, 2}]


A few notes:

• the definition of g doesn't need to know what f is.
• I used Rationalize[x, 0] to make sure the result is rational, avoiding cases like Rationalize[0.123423789] returning 0.123423789.
• I used _?NumericQ to make sure g is only defined for numbers, since Rationalize[x,0] will return x, which I want to avoid.
-
That's impressive. I understand your approach as you described it, but I don't follow all the syntax (I am still new to Mathematica). Could you briefly explain it? –  user001 Mar 14 '12 at 4:12
@user001 I added a bit, let me know if you have more questions. –  Brett Champion Mar 14 '12 at 4:32
Thanks. That is very clever. Is there a way to generalize it? Suppose, one wanted to use as an argument $x^2 + y$, $xy$, etc. –  user001 Mar 14 '12 at 5:23

I agree with Jens's comment that the problem is ill posed. But you can still brute force it numerically by abusing ParametricPlot:

ParametricPlot[{x/y, (5 x)/y + (E^(x/y) y^2)/x^2}, {x, 1, 2}, {y, 1, 2},
Mesh -> False, AspectRatio -> 1/GoldenRatio, Axes -> False]


This is ugly but convenient. When the function can't be expressed in terms of the composite variable you chose, this will give a surface instead of a line (thus it gives you instant verification as well).

Alternatively you can try to do the variable replacement symbolically:

Solve[Eliminate[{f == (5 x)/y + (E^(x/y) y^2)/x^2, z == x/y}, {x, y}], f]

(* ==> {{f -> (E^z + 5 z^3)/z^2}} *)

-