So I would like to write a code which iteratively finds the double root (a root which is also a maximum or a minumum) of functions. I would like to apply this to more complex functions Mathematica struggle with handling, but I thought I would start with a simple one:
f[x_] = (-1)*x*(x - 4)^2*(x + 6)
Now this function obviously has a double root in x = 4. I wanted to write a loop which would test different values of x until it finds a specific x for which f[x]==0 and f'[x]==0 (the conditions of a double root). I wrote the following:
solution = 0.1
While[f[solution] < 0, solution += 0.1;
If[f[solution] == 0 && f'[solution]==0, Return[solution]]
]
As I see it, this loop should start testing x = 0.1, if f[0.1] < 0 (which I have made sure it is in the whole interval 0.1<x<4) it should increase the tested value with 0.1 and try again until f[x] no longer is negative. Also, when we reach the point where the conditions for the double root are met, the loop should return solution. However, it doesn't seem to work at all. I have let the loop run for half an hour without any result. Could somebody please tell me what's wrong?
The only problem I can think about is if for some specific solution we have f[solution] < 0, f[solution+0.1] > 0 meaning that the condition f[solution] == 0 will never be met. This could of course be the case with more complex function, but with the current f[x] this shouldn't be a problem as 4 = 0.1 + 39*0.1.
EDIT: I do know that there are several commands which can find the root to this function. However, all I would like to know is simply why this loop does not work.
0.1to1/10. – corey979 Sep 08 '16 at 10:19x^2 - Sqrt[2] == 0and you simply test allxvalues in the interval $[0,2]$ with a certain step sizedx. Do you expect to ever find a value for which the equality truly holds? – Szabolcs Sep 08 '16 at 10:23f[x] == 0tof[x] >= 0and something similar with f'[x]. – Jhonny Sep 08 '16 at 11:04Rootsand related topics. – corey979 Sep 08 '16 at 11:15Rootscommand doesn't work on the more complicated function I would like to apply a similar loop on later. Right now I would just like to find the fault in the loop described above. – Jhonny Sep 08 '16 at 11:20FindRootsworks on any functions. If this is really the complete code, it is good to note thatReturnis not appropriate here. It should be used in a function and it's almost never needed in Mathematica. This code is not part of a function. – Szabolcs Sep 08 '16 at 11:50f[x] == 0andf'[x]==0simultaneously? – Jhonny Sep 08 '16 at 11:56forf'? Normally you'd have an extra parameter which you are also solving for (makes for a second variable). You might want to review the involved mathematics as well as some basic numerical methods for equation solving before starting this project. In the least take the time to understand the bisection method, Newton's method, secant method. – Szabolcs Sep 08 '16 at 12:23forf'. If I for instance was only solving forfin the example abow, I could get three roots: -6, 0 and 4. Even if I solve forf', there is more than one point where that condition is met (which is very clear if you plot the graph). All this said I could review the math, but this discussion is going off-topic because all I asked for was an explanation why my loop doesn't do what I want it to do, and not alternative methods. – Jhonny Sep 08 '16 at 12:39f[x]==0to find a set of solutions, then computef'[x]on these solutions and check which derivatives are equal to zero. – corey979 Sep 08 '16 at 12:41f[x]forx==4. Thus the test in theWhilenever fails. You are incrementing in steps of0.1, you you may have expected that eventuallysolutionwill be equal to4. But0.1is an inexact number (floating point). Computers represent floating point numbers in binary.0.1is not exactly representable in binary ... – Szabolcs Sep 08 '16 at 12:420.1many times will never give an exact4because0.1is not exactly1/10. That's the explanation of why this loop doesn't stop. The fundamental issue however is that such an algorithm is not capable of finding roots. The failure you see here is just one manifestation of that. – Szabolcs Sep 08 '16 at 12:43