What I want to do:
I am trying to write a user-defined function in Excel to calculate the partial derivative of a function,
f(x, y,...n), with respect to, x. My initial approach was to have the function change the value of x by ±0.01 %, and record the response in f. Then the slope is simply the partial derivative.
My function should work like this:
=PARTIAL(A2, A1)
Where cell A1 is some function that relies on the cells A2, A3, A(n). My function would return the partial derivative of A1 with respect to A2.
The full non-working code is shown below:
Function PARTIAL(x As Range, f As Range) As Double
Dim e As Double
e = 0.0001 * x
Dim y2 As Double
Dim y1 As Double
x = x + e
y2 = f
x = x - (2 * e)
y1 = f
x = x + e
PARTIAL = (y2 - y1) / (2 * e)
End Function
Why I can't do it:
The problem is that my function relies on the ability to modify A2, and record the response in A1. Unfortunetly, Excel is very strict about not allowing functions to modify the worksheet they are operating in, and calls like that below will throw an error:
Function myFunction(x as Range) as Double
x.Cells(1, 1).Value = 5
End Function
What I would like to know:
Is there a workaround for this problem? Does someone have a better way to conveniently calculate a partial derivative in Excel. I am trying to build a bigger function that eventually propagates the error through a series of analytical calculations, and this propagation relies on the partial derivative.
Thank you,
Michael