Plotting the derivatives of an interpolation function of two variables

I'm trying to show how g1 and g2 changes with s1 below and want a surface plot but it the parametric3Dplot doesn't show me the result!

Clear[d1, d2, d3, d4, L, Δs, s1, s2, sp, sm, H, HT, IM, \
v, smin, smax, f, g1, g2]
d1 = 1; d2 = 2; d3 = 4; d4 = 3;
smin = -1; smax = 1; Δs = 0.1;
L = 5;
sp = {{0, 1}, {0, 0}};
sm = {{0, 0}, {1, 0}};
n = {{0, 0}, {0, 1}};
v = {{1, 0}, {0, 0}};
IM = IdentityMatrix[2];
H =
MatrixForm[
{{0, E^-s1 d1, E^s1 d3, 0}, {0, -d1 - d2 - s2/2, 0, 0},
{0, 0, -d3 - d4 - s2/2, 0}, {0, E^-s1 d2, E^s1 d4, -s2}}];
HT = KroneckerProduct[H, IM, IM, IM] +
KroneckerProduct[IM, H, IM, IM] +
KroneckerProduct[IM, IM, H, IM] +
KroneckerProduct[IM, IM, IM, H] +
(d1*E^-s1) KroneckerProduct[sp, IM, IM, IM,
v] + (d3*E^s1) KroneckerProduct[v, IM, IM, IM,
sp] + (d2*E^-s1) KroneckerProduct[n, IM, IM, IM,
sm] + (d4*E^s1) KroneckerProduct[sm, IM, IM, IM,
n] - (d1) KroneckerProduct[n, IM, IM, IM,
v] - (d3) KroneckerProduct[v, IM, IM, IM,
n] - (d2) KroneckerProduct[n, IM, IM, IM,
v] - (d4) KroneckerProduct[v, IM, IM, IM, n] - (s2/
2) KroneckerProduct[n, IM, IM, IM, v] - (s2/2) KroneckerProduct[
v, IM, IM, IM, n] - (s2) KroneckerProduct[n, IM, IM, IM, n];

Table[Max[Re[Eigenvalues[HT]]], {s1,smin,smax,Δs},
{s2,smin,smax,Δs}];
f = ListInterpolation[%, {{smin, smax}, {smin, smax}}];
g1[s1_] = -D[f[s1, s2], {s1, 1}] /. s2 -> 0;
g2[s1_] = -D[f[s1, s2], {s2, 1}] /. s2 -> 0;
ParametricPlot3D[{g1[s1], g2[s1]}, {s1, smin, smax}]

• 1. Get rid of the MatrixForm - it's meant for display purposes and should not be a part of your calculation. 2. Use ParametricPlot not ParametricPlot3D – Simon Woods Feb 25 '17 at 9:57
• @SimonWoods Now I know why it was taking time to run this code. Thx – zhk Feb 25 '17 at 10:08
• thanx for the MatrixForm point. but again it doesn't show the result. It's just the axes – sara kaviani Feb 25 '17 at 10:30

In this case I think it is easier to get results with Derivative rather than D.

Clear[g1, g2]
g1 = Derivative[1][-f[#1, #2] &]


-Derivative[1, 0][f][#1, #2]&

g2 = Derivative[0, 1][-f[#1, #2] &]


-Derivative[0, 1][f][#1, #2]&

Plot3D[{g1[s1, 0], g2[s1, 0]}, {s1, smin, smax}, {s2, smin, smax},
PlotPoints -> 50,
ClippingStyle -> None]


Update

This is an attempt to answer an issue raise by the OP in a comment.

I don't think what you are asking for in your comment is meaningful. At least, I can't understand what you are getting at. There really isn't a 2nd axis to plot g2[s1, 0] on.

Here is a way I think you might better visualize the two derivative slices.

Plot[{g1[s1, 0], g2[s1, 0]}, {s1, smin, smax}, PlotLegends -> "Expressions"]


On the other hand, perhaps you really want this:

Plot3D[{g1[s1, s2], g2[s1, s2]}, {s1, smin, smax}, {s2, smin, smax},
PlotPoints -> 100, ClippingStyle -> None]


I admit I am really confused about what you are trying to visualize.

• How I will know, when to use D and Derivative? – zhk Feb 25 '17 at 15:37
• @MMM. I would recommend reading the docs for both functions thoroughly and carefully. My personal heuristic is D for expressions and Derivative for functions. With D it is easier to get tangled up in variable scoping. – m_goldberg Feb 25 '17 at 15:41
• @m_goldberg thats alright but do you know how I can plot one of these surfaces in which one of the axes is g1 the other one is g2 and another is s1?! – sara kaviani Feb 25 '17 at 15:52
• @m_goldberg OP is asking for something like this ParametricPlot[{g1[s1, 0], g2[s1, 0]}, {s1, smin, smax}]. – zhk Feb 25 '17 at 16:20
• @MMM. But I can't imagine what surface that could be. I'm trying to get the OP to clarify her thoughts on the matter. – m_goldberg Feb 25 '17 at 16:36

Your code is taking too much time to run. So without running it, I suggest that, since g1[s1] and g2[s1] are only functions of s1, so you should use, ParametricPlot.

If s2 is not fixed then for g1[s1, s2] and g2[s1, s2], you can use ParametricPlot3D. For more details see this.

Alternatively you can do this

ParametricPlot3D[{g1[s1], g2[s1], s1}, {s1, smin, smax}]


Edit

Thanks to @SimonWoods, once MatrixForm is removed the code runs as it should be.

ParametricPlot[{g1[s1], g2[s1]}, {s1, smin, smax}]


ParametricPlot3D[{g1[s1], g2[s1], s1}, {s1, smin, smax}]


• it doesn't either shoe the result for ParametricPlot3D and ParametricPlot. don't know why? – sara kaviani Feb 25 '17 at 10:31
• @sarakaviani Quit your kernel and then run the code. From menu bar click Evaluation then Quit Kernel and then click on Local. – zhk Feb 25 '17 at 10:37
• thanx I fixed it. but the problem with 3D is that it doesn't give me a surface. I need a surface – sara kaviani Feb 25 '17 at 10:44