Re: Problem with replacement rules
- To: mathgroup at smc.vnet.net
- Subject: [mg91993] Re: Problem with replacement rules
- From: magma <maderri2 at gmail.com>
- Date: Tue, 16 Sep 2008 19:24:02 -0400 (EDT)
- References: <ga82r6$re7$1@smc.vnet.net> <gaar52$17m$1@smc.vnet.net>
On Sep 11, 12:16 pm, Jens-Peer Kuska <ku... at informatik.uni-leipzig.de>
wrote:
> Hi,
>
> makeFunction[expr_, x_] := Module[{var},
> var = Complement[Variables[expr], {x}];
> Function @@ {var, x /. Solve[expr == 0, x][[1]]}
> ]
>
> and
>
> f = makeFunction[2 + x - y^2, x];
>
> Plot[f[z], {z, -2, 2}]
>
> ??
>
> Regards
> Jens
This code proposed by Jens-Peer Kuska only works with polynomial
equations, because Variables only works correctly with polynomials.
For example,
makeFunction[Sin[x]-y,x]
gives as output:
Function[{y, Sin[x]}, ArcSin[y]]
which is incorrect and cannot be plotted.
A more general code is the following:
invFunction[expr_, x_, vars_List] :=
Function @@ {vars, x /. Solve[expr == 0, x][[1]]}
where the argument x is the dependent variable and vars_List is the
list of independent variables.
Now you have as before:
f = invFunction[2 + x - y^2, x, {y}]
Plot[f[z], {z, -2, 2}]
for your polynomial functions, but also
f = invFunction[Sin[x] - y, x, {y}]
Plot[f[z], {z, -2, 2}]
without a problem.
An example with multiple indep. variables:
f = invFunction[Sin[x] - y - z, x, {y, z}]
Plot3D[f[y, z], {y, -1, 1}, {z, -1, 1}]
hth