4
$\begingroup$

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.

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

2 Answers 2

5
$\begingroup$

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}]

enter image description here

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.
$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – user001
    Mar 14, 2012 at 4:12
  • $\begingroup$ @user001 I added a bit, let me know if you have more questions. $\endgroup$ Mar 14, 2012 at 4:32
  • $\begingroup$ 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. $\endgroup$
    – user001
    Mar 14, 2012 at 5:23
5
$\begingroup$

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]

Mathematica graphics

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}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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