Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.3K Views
|
9 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Find second derivative using D?

Posted 7 years ago
POSTED BY: mohamed alzenad
9 Replies
Anonymous User
Anonymous User
Posted 7 years ago

Thanks to all of you for giving a lot of knowledge and clear the confusion

POSTED BY: Anonymous User

Mohamed,

I am guessing that your issue is related to numerical precision and your use of spline interpolation. If you want to take multiple derivatives (which is a highly noisy process) you will need to set your interpolation order high enough to be able to take derivatives (which successively get worse.) Spline interpolation makes for a smooth function that is forced to go through all of the data points. Numerically the splining process can result in strange artifacts (like the function looping around to go through a point). Therefore, the spline interpolation is unlikely to give good derivatives. Try using a higher order (but not spline) interpolation. But understand your function and what you are doing is running into numerical precision/instability issues.

Let me know if the interpolation change helps.

Regards,

Neil

POSTED BY: Neil Singer

Thank you Neil, I tried your suggestions above and worked well for up to the 3rd derivative. For the 4th derivative, the value of "ExactCondCovegeProbLAB22" is a negative large number which is not correct because it is a probability (it should be between 0 and 1). I plotted "funTable2D" and "ifun2D" to see how good the interpolation function "ifun2D" is as follows

ListPlot3D[funTable2Dc]

enter image description here

Plot3D[ifun2D[s, r], {s, LlimitxS, UlimitxS}, {r, LlimitxR, UlimitxR}]

enter image description here

As we can see, the two functions does not look similar

LlimitxR = height; UlimitxR = Sqrt[RmaxAB^2 + height^2];
LlimitxS = parmtrLAB[LlimitxR]; UlimitxS = parmtrLAB[UlimitxR];

Print[111];
funTable2D = 
  Table[{s, r, Exp[-noisepow*s]*laplaceLAB[s, r]}, {s, LlimitxS, 
    UlimitxS, 10000}, {r, LlimitxR, UlimitxR, 500}];
Print[222];
funTable2Dc = Flatten[Chop[funTable2D], 1];
Print[333];
ifun2D = Interpolation[funTable2Dc, Method -> "Spline"];
Print[444];
funDervtveLAB22[s_, r_, kk_] := (-s)^kk/kk!*D[ifun2D[s, r], {s, kk}];
xxSumS22[s_, r_] := 
  Sum[funDervtveLAB22[s, r, kk], {kk, 0, mLParm - 1}];
xxSumR22[r_] := xxSumS22[s, r] /. s -> (mLParm*sinr/zetaLAB*r^alfaLAB);
xxPdF22[r_] := xxSumR22[r]*CondPdfLAB[r];
ExactCondCovegeProbLAB22[[hIdx, mIdx]] = 
  NIntegrate[xxPdF22[r], {r, height, Sqrt[RmaxAB^2 + height^2]}];
POSTED BY: mohamed alzenad

Mohamed,

to do the 2D interpolation I recommend the following:

  1. Use fewer points in x because the function is fairly flat in that direction (see the 3D Plot below) to save time
  2. Chop off small imaginary parts (some solutions with x or y 0 have infinitesimal imaginary parts)
  3. Reformat the data into the correct format for interpolation and plotting by Flattening the table.

    funTable2D = Table[{x, y, xxSumS1[x, y]}, {x, 0, 500000, 10000}, {y, 0, 500000, 1000}];
    funTable2Dc = Flatten[Chop[funTable2D], 1];
    

View the result:

ListPlot3D[funTable2Dc]

enter image description here

Now you can do the interpolation:

ifun2D = Interpolation[funTable2Dc]

To Plot

Plot[ifun2D[x, 200], {x, 0, 500000}]

Derivatives

fun2 = D[ifun2D[x, y], x]
fun3 = D[ifun2D[x, y], {x, 2}]

To Plot:

Plot[fun2 /. y -> 200, {x, 0, 500000}]
Plot[fun3 /. y -> 200, {x, 0, 500000}]

I hope this help.

Regards,

Neil

POSTED BY: Neil Singer
POSTED BY: mohamed alzenad
POSTED BY: mohamed alzenad

Mohamed,

The problem you are having is that you are doing a numerical calculation and running out of precision. Numerical derivatives are noisy operations -- you must maintain extreme precision to take multiple derivatives. Your calculation involves a Numerical integrate and then you take the derivative of it. I recommend taking the derivative analytically -- it will also speed things up. Another way to do this is to keep precision with Interpolation functions:

fun = Table[{x, xxSumS1[x, 200]}, {x, 0, 500000, 1000}];

(This is a bit slow but you can lower the resolution. Also, You will get warnings which you can fix by specifying AccuracyGoal in the NIntegrate)

ifun = Interpolation[fun]
Plot[ifun[x], {x, 0, 500000}]
Plot[ifun'[x], {x, 0, 500000}]
Plot[ifun''[x], {x, 0, 500000}]

To get

enter image description here

POSTED BY: Neil Singer

Thank you John for your reply. The numerical integration is with respect to the variable z and it gives a function in s and r, for example; InteglaplaceLABNABs[s,r] is a function in s and r.

InteglaplaceLABNABs[s_?NumericQ, r_?NumericQ] := NIntegrate[funLABNABs[z, s], {z, lowerlimitLABNABs[r], RmaxLAB}];

I used D on NIntegrate for the 1st derivative and it worked well and gave good result.

POSTED BY: mohamed alzenad

Your calculations use NIntegrate, a numerical method. Symbolic differentiation cannot handle a numerical function.

POSTED BY: John Doty
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard