# UDF to solve or invert formula

#### Kelvin Stott

##### Active Member
Hello,

I have a simple formula, p = (a+b)^n - b^n, but need to find n in terms of a, b and p. Problem is there isn't an exact algebraic solution (at least not according to WolframAlpha), so I need to create a fully automatic user defined function (UDF) that effectively acts a bit like goal seeker or solver, but without using a macro (Sub) as that has disadvantages like changing cells and blocking undo, etc.

Is there a generic UDF to solve or "invert" any given formula?

This would be such an incredibly useful function, so I can't imagine it hasn't been done, but all I can find are goal seek macros, which is not what I want.

Thanks,
Kelvin

Last edited:

### Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
PS., I guess the syntax of the UDF should be something like:

SOLVE(Input formula f(x), Target value for f(x), [Initial estimate for x (optional)], [Min x (optional)], [Max x (optional)]) and the output value of the function would be x.

Would that be possible?

Last edited:
So I created the SOLVE function (code below), and it seems to work great in some cases:

SOLVE("x^2",2) = 1.4142135623731

But not in other cases, for example:

SOLVE("x^2",2,1,0,5) = Error: 1.50993850552705E-03
SOLVE("LN(x)",2) = #VALUE!

Any idea why it's not working reliably?

Code:
``````Function SOLVE(Fx, Target, Optional x0 = 0, Optional xMin = -10 ^ 16, Optional xMax = 10 ^ 16, Optional yTol = 10 ^ -12, Optional iMax = 200)

For i = 1 To iMax
x1 = x0 + (xMax - x0) * 0.5 ^ i
x2 = x0 - (x0 - xMin) * 0.5 ^ i
y0 = Abs(Evaluate(Replace(Fx, "x", x0)) - Target)
y1 = Abs(Evaluate(Replace(Fx, "x", x1)) - Target)
y2 = Abs(Evaluate(Replace(Fx, "x", x2)) - Target)
If y1 < y0 And y1 <= y2 Then
x0 = x1
y0 = y1
ElseIf y2 < y0 And y2 <= y1 Then
x0 = x2
y0 = y2
End If
If y0 = 0 Then Exit For
Next i

If y0 < yTol Then
SOLVE = x0
Else
SOLVE = "Error: " & y0
End If

End Function``````

Without delving too deeply into your code, it appears that you are just doing a binary search within the range. Which is reasonable enough with most functions. But if the function is discontinuous, like LN, it appears that you're evaluating it at 0, which would cause a problem.

You really should test the yTol within the loop too.

And if you want to learn a bit more, and find more efficient algorithms, try searching for Newton's Method.

Good luck!

Thanks Eric.

Does anyone have any ready made code for the Newton method so that I can integrate and compare, if it's really more efficient?

Thanks!

So I managed to improve with Newton's method, and now it's working perfectly.

This short user defined function can solve pretty much any formula, exactly what I wanted. It usually converges within just 4 to 6 cycles (though I allow up to 20 just in case).

Code:
``````Function SOLVE(Fx, Target, Optional x0 = 0.5, Optional yTol = 10 ^ -12, Optional iMax = 20)

dx = 10 ^ -6

For i = 1 To iMax
x1 = x0 + dx
y0 = Evaluate(Replace(Fx, "x", x0))
y1 = Evaluate(Replace(Fx, "x", x1))
x0 = x0 - (y0 - Target) * dx / (y1 - y0)
y0 = Evaluate(Replace(Fx, "x", x0))
dy = Abs(y0 - Target)
If dy < yTol * Target Then Exit For
Next i

If dy < yTol * Target Then
SOLVE = x0
Else
SOLVE = "Error: " & dy
End If

End Function``````

Thanks for all the help. :-/

Last edited:
Just a couple of pointers, it's good to see that you figured out a nice function.

Thanks again.

I always prefer to calculate exact roots with algebra where possible as it's more efficient, precise and reliable, but so many times I haven't been able to so I'm really pleased to have this as back-up.

Yes, it is nice when you can solve an equation exactly. But in the real world, there are SO many situations where you can't. If you get into differential equations or numerical analysis, you'll see lots of examples of that. In such a case, it's nice to have some way to solve the equation. It's a matter of using the right tool for the right job.

Replies
2
Views
733
Replies
6
Views
1K
Replies
1
Views
1K
Replies
0
Views
435
Replies
7
Views
273

1,221,525
Messages
6,160,329
Members
451,637
Latest member
hvp2262

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back