MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Problem with user defined functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37363] RE: [mg37340] Problem with user defined functions
  • From: "DrBob" <drbob at bigfoot.com>
  • Date: Fri, 25 Oct 2002 02:47:00 -0400 (EDT)
  • Reply-to: <drbob at bigfoot.com>
  • Sender: owner-wri-mathgroup at wolfram.com

Try this:

Off[Solve::"ifun"]
test[x_] := p /. First@Solve[Sin[p] == x, p]
data[x_] := D[test[y], y] /. y -> x
Plot[data[x], {x, 0, 1}]

In order to differentiate test[x] at x, it's necessary for x to be a
symbol, but when data[x] is evaluated x has a value, so D[test[x],x]
can't work.  Hence we take the derivative at y and then replace y with
x.  Since y has no value, FindRoot can't do the job, since it has to
have numerics to work with (other than p).  If you have a problem Solve
can't handle, then you can't differentiate it symbolically; you'll have
to choose a small delta and do everything numerically, something like
this:

delta = 0.00001; 
start = 0.; 
test[x_] := start = p /. FindRoot[Sin[p] == x, {p, start}, AccuracyGoal
-> 8]
data[x_] := (test[x + delta] - test[x - delta])/(2*delta)
Plot[data[x], {x, 0, 1 - delta}]

For x very close to 1, the AccuracyGoal has to be increased; the Plot
stabilizes when the goal is increased to 10 or more.

For your REAL problem, of course, things may be different.

Returning to the example, it's possible to compute the derivative of p
without solving for p:

Sin[p] == x
(Dt[#1, x] & ) /@ %
% /. Cos -> cos
First[Solve[%, Dt[p, x]]]
% /. cos[a_] -> Sqrt[1 - sin[p]^2]
% /. sin[p] -> x

Bobby

-----Original Message-----
From: Farhat Habib [mailto:farhat at pacific.mps.ohio-state.edu] 
To: mathgroup at smc.vnet.net
Subject: [mg37363] [mg37340] Problem with user defined functions


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?

Thanks in advance,

Farhat






  • Prev by Date: Re: Pascal's Triangle
  • Next by Date: RE: help on bootstrap sample
  • Previous by thread: Re: Problem with user defined functions
  • Next by thread: Pascal's Triangle