Re: Derivatives of numerical functions : how does mathematica work?

*To*: mathgroup at smc.vnet.net*Subject*: [mg61477] Re: Derivatives of numerical functions : how does mathematica work?*From*: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de>*Date*: Wed, 19 Oct 2005 23:07:27 -0400 (EDT)*Organization*: Uni Leipzig*References*: <dj4q04$ior$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Hi, << NumericalMath`NLimit` myfun[c_?NumericQ] := y /. FindRoot[(x + y) /. {x -> c}, {y, 0}] ND[myfun[c], c, 2.0] Regards Jens <amitgandhi at gmail.com> schrieb im Newsbeitrag news:dj4q04$ior$1 at smc.vnet.net... |I have stumbled upon a seemingly basic problem - but my inability to | understand why I cannot get mathematica to work in the way I think it | should makes me question how well I understand the foundtaions of | mathematica. | | Essentially I have a functions f[x] which takes x, and forms a system | of equations in the variables y1,...,yn where x appears as a parameter | to the system, and it uses FindRoot to solve for y1,...,yn and then | outputs the sum y1+...,yn. I know the solutions for y1,...,yn are | "smooth" in the parameter x, and thus I was interested in finding the | numerical derivative of y1+...+yn with respect to the parameter x. | However I cannot get mathematica to implement this - I can illustrate | the difficulty in a very basic setting (excuse the fact that I can do | this "toy" problem analytically, because my real problem requires | numerics). | | Suppose we have the equation | | equ=x+y | | For a particular value of x, I can solve the equation x+y=0 for y, | which for x=2.0 I do in mathematica as follows: | | y /. FindRoot[x + y /. {x -> 2.0}, {y, 0}] | | | which yields the obvious -2. | | Now I want to consider the above mathematica expression, which solves | for the value of y for a given x, as an expression that is variable in | x, and I wish to cconsider how the value of this expression varies with | x, i.e., the numerical derivative with respect to x. My inutuition for | mathematica suggests that I should write this as | | <<NumericalMath`NLimit` | ND[y /. FindRoot[(x + y) /. {x -> c}, {y, 0}], c, 2.0] | | However the output from entering this expression is a number of error | messages that read, the most important and telling one being that : | | "The function value {0. + c} is not a list of numbers with \ | dimensions {1} at {y} = {0.`}." | | Why is this happening - doesn't ND try to replace c with a trial value | in the expression in the first argument of ND before it tries | evaluating the expression? | | However after the error messages finish, the ND command manages to | produce the right answer, namely -1. | | ND is just an example of this phenomena - the same thing would have | happened had I tried to use NLimit, FindMinumum, FindRoot, or a host of | other mathematica functions with the expression I defined above. Can | anyone explain how to get mathematica to work "error free" for a | problem like this, and whether the numerical answer that mathematica | produces should be trusted after producing a long list of errors. | | | Thanks | Amit |