# Gradient vector of a matrix of geographical extent

**URL:** https://discourse.julialang.org/t/gradient-vector-of-a-matrix-of-geographical-extent/87318
**Category:** General Usage
**Tags:** differentiation, calculus, gradient
**Created:** [September 15, 2022, 3:39pm UTC](https://discourse.julialang.org/t/gradient-vector-of-a-matrix-of-geographical-extent/87318 "2022-09-15T15:39:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ManuelFossa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manuelfossa/32/36305_2.png) [@ManuelFossa](https://discourse.julialang.org/u/ManuelFossa)
#### Post date: [September 15, 2022, 3:39pm UTC](https://discourse.julialang.org/t/gradient-vector-of-a-matrix-of-geographical-extent/87318/1 "2022-09-15T15:39:26Z")

</div>

I have a geographical matrix `M` of dimensions `nLat=181` and `nLon=221` , with `lats = 30:0.25:90` and `lons=-30:0.25:25`.  
I want to compute the gradient vector of `M` w.r.t to ` lats , lons`.  
I have tried `ForwardDiff , Iterations , ScatteredIterations , Flux ` and my problem is that they all accept continuous functions as input.  
I have tried the method [interpolating](https://discourse.julialang.org/t/differentiation-without-explicit-function-np-gradient/57784/2) , or [this one](https://www.anycodings.com/1questions/3341429/problem-with-combining-forwarddiff-and-pyplot-in-julia-105), but to no avail.

Here is my current code

```julia
x = lats
y = lons
points = (x,y)
samples = M
itp = Interpolations.interpolate(points, samples, Gridded(Linear()))
itpGradient = Interpolations.gradient.(itp,x,y)

```

I get the following error

```julia
DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 181 and 221")

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [September 15, 2022, 7:30pm UTC](https://discourse.julialang.org/t/gradient-vector-of-a-matrix-of-geographical-extent/87318/2 "2022-09-15T19:30:33Z")

</div>

You could adapt the 1D solution in the [post](https://discourse.julialang.org/t/differentiation-without-explicit-function-np-gradient/57784/2) linked to 2D as follows:

```julia
using Interpolations

f(x,y) = x^2 + y^2 # function used just to generate data

x = y = -2:0.02:2
z = f.(x, y') # z is the numeric data at coordinates (x, y)

itp = interpolate((x,y), z, Gridded(Linear()));
grad = gradient.(Ref(itp), x, y')

```

If the data matrix `z` is noisy, we could fit smoothing 2D splines before estimating the gradients. An example using Dierckx:

```julia
using Interpolations, Dierckx

f(x,y) = x^2 + y^2
x, y = -2:0.02:2, -3:0.02:3
nx, ny = length(x), length(y)
z = f.(x, y') + rand(nx, ny) # noisy data matrix over grid defined by (x,y)

s0 = 0.01*nx*ny*hypot(extrema(z)...) # test different smoothing factors
spl = Spline2D(x, y, z; kx=3, ky=3, s=s0)
zsmooth = evalgrid(spl, x, y)
itp = interpolate((x,y), zsmooth, Gridded(Linear()));
grad = gradient.(Ref(itp), x, y')

```

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [September 17, 2022, 12:02am UTC](https://discourse.julialang.org/t/gradient-vector-of-a-matrix-of-geographical-extent/87318/3 "2022-09-17T00:02:39Z")

</div>

[grdgradient](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/modules/grdgradient/index.html#grdgradient)?

---

<div class="post-metadata">

### Author: ![ManuelFossa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manuelfossa/32/36305_2.png) [@ManuelFossa](https://discourse.julialang.org/u/ManuelFossa)
#### Post date: [September 19, 2022, 12:48pm UTC](https://discourse.julialang.org/t/gradient-vector-of-a-matrix-of-geographical-extent/87318/4 "2022-09-19T12:48:24Z")

</div>

> [@rafael.guerra](#):
>
> `Dierckx`

Thank you very much, that works 🙂
