Partial derivative of an interpolated function (with Mathematica)

In summary, the conversation discusses a problem faced when trying to plot a partial derivative of a function obtained by interpolation in Mathematica. The error occurs due to differentiating with respect to a constant value. A solution is provided by evaluating the derivative in advance. The conversation also mentions the issue of finding a point where both first and second derivatives of the function are zero, and suggests using the FindRoot function. Finally, options for improving the quality of interpolation and the limitations of taking a second derivative with limited support points are also discussed.
  • #1
evgenx
14
0
Hi,

I faced a problem (in Mathematica) when trying to plot a partial
derivative of a functiona (of two variables) obatined by "Interpolation".
More precisely, here is my input:

surf=Interpolation[{
{{160.0, 160.0}, 2.852688},
{{160.0, 170.0}, 2.827547},
{{160.0, 180.0}, 2.818931},
{{160.0, 190.0}, 2.826640},
{{160.0, 200.0}, 2.851483},

{{170.0, 160.0}, 2.861634},
{{170.0, 170.0}, 2.832750},
{{170.0, 180.0}, 2.822240},
{{170.0, 190.0}, 2.830275},
{{170.0, 200.0}, 2.858395},

{{180.0, 160.0}, 2.862344},
{{180.0, 170.0}, 2.831671},
{{180.0, 180.0}, 2.820786},
{{180.0, 190.0}, 2.831775},
{{180.0, 200.0}, 2.862605},

{{190.0, 160.0}, 2.857940},
{{190.0, 170.0}, 2.830020},
{{190.0, 180.0}, 2.822137},
{{190.0, 190.0}, 2.832657},
{{190.0, 200.0}, 2.861574},

{{200.0, 160.0}, 2.850865},
{{200.0, 170.0}, 2.826201},
{{200.0, 180.0}, 2.818547},
{{200.0, 190.0}, 2.827127},
{{200.0, 200.0}, 2.852228}
}]

s1=Function[{a,b},surf[a,b]]

da=Function[{a,b},D[s1[a,b],a]]

Plot3D[da[a, b], {a, 175.0, 185.0}, {b, 175.0, 185.0}]

When running this input Mathematica gives the error:
General::ivar: 175.00071499999999` is not a valid variable

Any help will be greatly appreciated !
Many thanks!


Evgeniy
 
Physics news on Phys.org
  • #2
The problem is here:

da=Function[{a,b},D[s1[a,b],a]]

When you evaluate da[170,175] it translates this to D[s1[170,175],170]. Obviously, you cannot differentiate wrt 170, so it throws the error. What you want is for it to evaluate the partial derivative in advance and construct a new interpolating function object and simply evaluate that function as needed. You can do that as follows:

da=Function[{a,b},Evaluate[D[s1[a,b],a]]]
 
  • #3
Hi,

Many thanks for your reply. It works now fine.

Well, there is an additional related question.
Namely, I'd like to find a point on the surf[a,b]
(i.e. values of a and b) where both dsurf/da=0
and d²surf/da²=0. Just simply solving:

s1=Function[{a,b},surf[a,b]]

da=Function[{a,b},Evaluate[D[s1[a,b],a]]]
dada=Function[{a,b},Evaluate[D[s1[a,b],{a,2}]]]

sol=FindRoot[{da==0,dada==0},{a, 180.0},{b, 165.0}]


gives the error:

The function value {Function[{a, b},
InterpolatingFunction[{{160., 200.}, {160., 200.}}, <>][a, b]],
Function[{a, b}, InterpolatingFunction[{{160., 200.}, {160., 200.}}, <>][
a, b]]} is not a list of numbers with dimensions {2} at {a, b} =
{180., 165.}.


What can I do wrong?
Thanks!


Evgeniy
 
  • #4
sol=FindRoot[{da==0,dada==0},{a, 180.0},{b, 165.0}]

should be

sol=FindRoot[{da[a,b]==0,dada[a,b]==0},{a, 180.0},{b, 165.0}]

Since da and dada are pure functions you need to supply the arguments.
 
  • #5
Great, everything works! Thanks a lot!

A-a, just the very last question ... When I tried to plot
da and dada (with Plot3D) they looked like the function (surf) has
discontinuities at a=180.0. I guess it has to do with the quality of
interpolation. After I added Method->"Spline" (as an option for Interpolation)
da became better but dada still has a crease at a=180.0. Don't you happen
to know how to improve it.

Best wishes,
Evgeniy
 
  • #6
You may want to increase the order of the interpolation using the InterpolationOrder option. However, you should realize that what you are doing, taking the second derivative of a function with only 5 support points, is inherently unlikely to give very good results.
 
  • #7
Ok, I see.
Thanks again for your help!
 

Related to Partial derivative of an interpolated function (with Mathematica)

1. What is a partial derivative?

A partial derivative is a mathematical concept that measures the rate of change of a function with respect to one of its variables while holding all other variables constant. It is denoted by ∂ (pronounced "partial") and is commonly used in multivariable calculus and mathematical analysis.

2. How is a partial derivative calculated?

To calculate a partial derivative of a function, you first need to identify the variable with respect to which you want to take the derivative. Then, you differentiate the function with respect to that variable while treating all other variables as constants. This process is similar to taking a regular derivative, but with the additional step of holding other variables constant.

3. What is an interpolated function?

An interpolated function is a function that is constructed from a set of known data points. It is used to estimate values between the given data points and is often used in data analysis and modeling. In Mathematica, the Interpolation function is used to create interpolated functions.

4. How do you take the partial derivative of an interpolated function in Mathematica?

To take the partial derivative of an interpolated function in Mathematica, you can use the D function and specify the variable with respect to which you want to take the derivative. For example, D[f[x,y],x] would give you the partial derivative of the interpolated function f with respect to x.

5. Can Mathematica handle higher-order partial derivatives of interpolated functions?

Yes, Mathematica is capable of handling higher-order partial derivatives of interpolated functions. You can use the D function multiple times to obtain second, third, and higher-order partial derivatives. You can also specify the order of the derivative using the syntax D[f[x,y],{x,n},{y,m}], where n and m represent the order of the derivatives with respect to x and y, respectively.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Differential Geometry
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
2K
Replies
36
Views
3K
  • Quantum Physics
Replies
1
Views
986
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
4
Views
10K
Back
Top