Re: Problem with user defined functions

• To: mathgroup at smc.vnet.net
• Subject: [mg37371] Re: [mg37340] Problem with user defined functions
• From: Daniel Lichtblau <danl at wolfram.com>
• Date: Fri, 25 Oct 2002 02:48:18 -0400 (EDT)
• References: <200210240655.CAA05087@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Farhat Habib wrote:
>
> Hello all,
>
> I am trying to evalute a function which looks like this. This is an
> analogous problem not the actual problem.
>
> test[x_] := p /. FindRoot[Sin[p] == x, {p, 1}][[1]]
> data[x_] := D[test[x], x]
> Plot[data[x], {x, 0, 1}]
>
> Now, when trying to evaluate the derivative mathematica replaces both the
> x's with the number at which it is trying to calculate the datapoint. This
> gives an error of 'xxxx is not a valid variable'. Replacing D[test[x]]
> with test'[x] doesn't help either though it results in a different set of
> errors. Is there any of way of getting around this problem?
>
>
> Farhat

In addition to the ways noted in sci.math.symbolic (differentiate
numerically or form an interpolating function) I can think of one method
that in essence reduces the plotting of the derivative to use the same
computation as for evaluating the function itself. Like the other
methods, this one will not make use of the fact that your particular
example has a readily available inverse function.

I'll show this using more customary notation {x,y}. We regard y as an
implicitly defined function of x.

expr[x_] = Sin[y]-x

We form the derivative with respect to x. We will use this to solve for
y'[x] in terms of x and y[x].

derivexpr = D[expr[x]/.y->y[x], x]

Now we give a function to evaluate y[x] numerically.

evaly[x_?NumberQ] := y /. FindRoot[expr[x]==0, {y,1}]

We use all this to find an expression that will evaluate the derivative
numerically. We solve for y'[x] and then replace y[x] in the solution by
evaly[x].

In[8]:= evaldydx = (First[y'[x] /. Solve[derivexpr==0, y'[x]]]) /.
y[x]->evaly[x]
Out[8]= Sec[evaly[x]]

This may now be plotted via

Plot[evaldydx, {x,0,1}]

Daniel Lichtblau
Wolfram Research

```

• Prev by Date: Re: Re: Poincare sections
• Next by Date: Re: Pascal's Triangle
• Previous by thread: Re: Problem with user defined functions
• Next by thread: Re: Problem with user defined functions