Re: Problem with replacement rules
- To: mathgroup at smc.vnet.net
- Subject: [mg92043] Re: Problem with replacement rules
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 18 Sep 2008 06:14:05 -0400 (EDT)
- References: <ga82r6$re7$1@smc.vnet.net> <gaar52$17m$1@smc.vnet.net> <gapf6a$o03$1@smc.vnet.net>
Hi, and what does Solve[] ? work with polynomial equations??? So there is no problem, because nobody would use Solve[] for an equation like Sin[x]-y Regards Jens magma wrote: > 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 >