2
$\begingroup$

I have a function sol defined as a solution to an equation (computed numerically). Specifically

sol[p_, a_] := NSolve[g[p, x, a] == 0, x, Reals][[1, 1, 2]]

Trying it out

sol[.2, .51]
sol[.2, .52]
sol[.2, .53]

gives .51, .52, .53 -- on this range it behaves like identity. Now I try

ND[sol[.2, aa], aa, .51 ]

and instead of giving me 1 as I would expect, it gives me very long expression involving variable x. I am not using the variable aa anywhere else in the code. And yes, I have done Needs["NumericalCalculus`"].

How is this possible? It seems obvious to me that it has to return a number! How can it return anything else?


Edit: here is the whole code (I have simplified it a little bit, so the values are different than above, but it should still give numbers, not expressions)

Needs["NumericalCalculus`"]
g[  x_, a_] = D[RealAbs[1 - x]^1.2 + RealAbs[a - x]^1.2, x];
sol[a_] := NSolve[g[ x, a] == 0, x, Reals][[1, 1, 2]];
ND[sol[aa], aa, .55]
$\endgroup$
2
  • $\begingroup$ Without knowing g, NDSolve can not evaluate. And Part [[1,1,2]] simply picks out x from the unevaluated NDSolve. Therefore, as long as` x` has no value you get: x $\endgroup$ Feb 13 at 13:25
  • $\begingroup$ I have updated the code. Sorry, I did not include g because I did not think it is relevant. I think the code should now give a number but it gives a very long expression. $\endgroup$ Feb 13 at 13:52
1
$\begingroup$

After typing your code into a fresh notebook and evaluating it. I get the following results:

I suggest Quitting your Kernel and evaluating everything again. You could also try clearing some variables using Clear[sol, sol, aa, g, p, x, a]. And running your code again once you have cleared them.

enter image description here

$\endgroup$
5
  • $\begingroup$ Sorry, I did not include g. I have now updated the question to include the definition of g. $\endgroup$ Feb 13 at 13:52
  • $\begingroup$ @user2316602 It looks like you are passing an expression in terms of $x$ and $aa$ to ND. Try evaluating sol[aa]. And let me know if you understand? $\endgroup$ Feb 13 at 15:22
  • 1
    $\begingroup$ Oh, I see where it went wrong! Thank you $\endgroup$ Feb 13 at 15:31
  • $\begingroup$ By the way, how did you create the cool animation? $\endgroup$ Feb 13 at 15:49
  • $\begingroup$ The animation is a screen recording converted into a GIF file :) Search "GIF screen recorder" if you are interested in doing this your self. I'm on Ubuntu, so I'm using this one $\endgroup$ Feb 13 at 16:03
1
$\begingroup$

The problem was that ND expects an expression. When I gave it sol[aa], it looked at

NSolve[-((1.2` (aa - xa))/RealAbs[aa - xa]^0.8`) - (1.2` (1 - xa))/  RealAbs[1 - xa]^0.8` == 0, xa, Reals]

then it took the 1,1,2'th element of this which was

-((1.2 (aa-xa))/RealAbs[aa-xa]^0.8)

which clearly cannot be numerically differentiated.

I have resolved this by making my own little function for numerical differentiation.

$\endgroup$
0
$\begingroup$

Here is a plot of your function:

Plot3D[g[x, y], {x, 0, 2}, {y, .5, 0.6}]

enter image description here

Why do you think the partial derivatives should be one?

$\endgroup$
2
  • $\begingroup$ Yes, sorry, it's not one. But it's a number for sure! $\endgroup$ Feb 13 at 15:29
  • $\begingroup$ g has 2 variables, you only specify one, therefore, what you get is a function of x $\endgroup$ Feb 13 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.