Message Boards Message Boards

[✓] 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 F

I have attached the file that contains my data. Thanks.

Attachments:
POSTED BY: Ghady Almufleh
Answer
2 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!

POSTED BY: Ghady Almufleh
Answer
2 months ago

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.Documentation

Lastly, 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

POSTED BY: Neil Singer
Answer
2 months ago

Mr.Singer, Thank you for your answer!! I will try your method.

Thanks a lot.

POSTED BY: Ghady Almufleh
Answer
2 months ago

Dear Singer,

Can you please check my file because I used the method you mentioned, but it doesn't work.

Thanks.

POSTED BY: Ghady Almufleh
Answer
2 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

POSTED BY: Neil Singer
Answer
2 months ago

Dear Singer,

Now it works. I really appreciate your effort and time!!

Thanks and Thanks a lot!

POSTED BY: Ghady Almufleh
Answer
2 months ago

Group Abstract Group Abstract