# [✓] Get gradient of a function?

GROUPS:
 I have a function f(z,x),but it is a list of data. It is also in two variables z and x that are lists of data too.I need to find gradient f(z,x). In another word, how can I find a numerical differentiation of the function.you can say I have a table or a matrix and the first column is the values of variable z and the second column represents the values of variable x and the third column is the values of F which is a function in z and x. I need to find gradient FI have attached the file that contains my data. Thanks. Attachments:
11 months ago
9 Replies
11 months ago
 Thank you for your response. Please can you give me more details because I tried to use interpolatingFunction, but it doesn't work or I didn't know how to create a command using it.Thank you again!
11 months ago
 Neil Singer 2 Votes Ghady,You first need to restructure your lists in the right format for a two dimensional Interpolation. x={1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4}; y={1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4}; f={21,22,23,24,25,24,23,22,21,20,21,22,21,20,19,18}; data = MapThread[{{#1,#2},#3}&,{x,y,f}]; Next use Interpolation to construct the function ffun = Interpolation[data] There are many options to Interpolation that you should read --for example using splines or an interpolation order.DocumentationLastly, you can use derivative to get derivatives. Here I get derivatives at the point 2,2 in various directions: Derivative[1, 1][ffun][2, 2] Derivative[1, 0][ffun][2, 2] Derivative[0, 1][ffun][2, 2] You can also make a derivative function using the above syntax.Regards
11 months ago
11 months ago
 Dear Singer,Can you please check my file because I used the method you mentioned, but it doesn't work. Thanks. Attachments:
11 months ago
 Ghady,Your definition of f[z,x] = {list} makes no sense here. you are not constructing a function. No matter what you enter for z and x you will get the same list as a result.follow my example above -- the function is a plain list in which the first value of ffun corresponds to the first z and first x value. ffun={-3.9721816156419436+1.0114457938132355 I,-3.895303039505704+0.9366555722647851 I, ...} The next step is to put the data in the right form. data = MapThread[{{#1, #2}, #3} &, {z, x, ffun}] Next interpolate: q = Interpolation[data] Define your gradient function -- note that I used Evaluate so the grad is processed immediately. gradient[a_, b_] := Evaluate[Grad[q[a, b], {a, b}]] Use it as follows In[15]:= gradient[.34, .44] Out[15]= {141.589 - 72.8953 I, -237.291 + 55.3092 I} I attached your fixed file.Regards Attachments:
 Dear Mr.Singer,Please, I need your help if you can in this point because I posted my question previously, but I didn't get any help till now. Is there any way to vary interpolation or add a specific number or syntax to eliminate unwanted points. For example, in my case there are some arrows in the opposite direction which should be removed. In other words, what I should add in this: Interpolation[data]to improve my results.Thanks in advance! Attachments:
 Ghady,It appears that you have some bad data in the middle of your vector field. I am not sure of the origin of this. I would explore if the problem is numerical (your derivatives lose accuracy)or if the function is just changing quickly. You can find regions (ie look around {0.3435, 0.4465}`) where the vectors suddenly change direction. You need to look at your physics here and understand why. I don't think this is a Mathematica issue -- I think it is a physics issue or a numerical stability issue. If you trace the steps looking at the places where this happens it should become clear.I hope this helps.Regards