# Incoherence between exact and finite differences Jacobian

**URL:** <https://discourse.julialang.org/t/incoherence-between-exact-and-finite-differences-jacobian/95816>\
**Category:** New to Julia\
**Created:** [March 9, 2023, 6:16pm UTC](https://discourse.julialang.org/t/incoherence-between-exact-and-finite-differences-jacobian/95816 "2023-03-09T18:16:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GuyFromBosnia](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@GuyFromBosnia](https://discourse.julialang.org/u/GuyFromBosnia)\
**Post date:** [March 9, 2023, 6:16pm UTC](https://discourse.julialang.org/t/incoherence-between-exact-and-finite-differences-jacobian/95816/1 "2023-03-09T18:16:58Z")

</div>

I have this code here where I need to compute the exact jacobian matrix and the approximate jacobian matrix using the newton’s method I have the exact jacobian matrix giving me the correct results but the approximate is giving me the wrong results.I hope that you can help me  
function f(v)  
n = length(v) # stores the length of v  
h = 1/(n+1)  
f = zeros(n)  
f[1] = -2v[1] + v[2] + (1/4)\*exp(v[1])  
f[n] = -2v[n] + v[n-1] + (h^2)\*exp(v[n])  
for i in 2:n-1  
local h=1/(i+1)  
f[i] = ((v[i+1] - 2v[i] + v[i-1])/h^2) + exp(v[i])  
f[i] \*= h^2  
end  
return f  
end

epsilon=0.000001

function jacobian\_exact(f, x)  
n = length(x)  
J = zeros(n, n)  
for i = 1:n  
for j = 1:n  
if i == j  
h = 1 / (i + 1)  
J[i, j] = 2 / h^2 + exp(x[i])  
elseif i == j + 1 || i == j - 1  
h = 1 / (i + 1)  
J[i, j] = -1 / h^2  
end  
end  
end  
return J  
end

function find\_jacobian\_matrix(f, x, epsilon)  
n = length(x)  
J = zeros(n, n)  
for j in 1:n  
ei = zeros(n)  
ei[j] = 1  
J[:, j] = (f(x + epsilon\*ei) - f(x)) / epsilon  
end  
return J  
end

function newton\_method(f, x0, epsilon, max\_iter, method::Symbol=:exact)  
x = copy(x0)  
iter = 0  
while norm(f(x)) \> epsilon && iter \< max\_iter  
if method == :exact  
J = jacobian\_exact(f, x)  
elseif method == :approximate  
J = find\_jacobian\_matrix(f, x, epsilon)  
else  
error(“Invalid method specified. Must be :exact or :approximate.”)  
end  
y = -J \ f(x)  
x += y  
iter += 1  
end  
return x  
end

# initial guess

x0 = [0, 0, 0, 0]

# call the function

exact\_root = jacobian\_exact(f, x0) \ f(x0)  
approx\_root = find\_root(f, x0, epsilon, 100, :approximate)

# display the results

println("Exact Root: ", exact\_root)  
println("Approximate Root: ", approx\_root)

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [March 9, 2023, 7:11pm UTC](https://discourse.julialang.org/t/incoherence-between-exact-and-finite-differences-jacobian/95816/2 "2023-03-09T19:11:46Z")

</div>

Hey @GuyFromBosnia, welcome!

To make it easier for others to help you, it is a good idea to post a reproducible code example that is cleanly formatted (see [here](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) for more advice).

I took the liberty of adjusting formatting and making some simplifications, here’s what I arrive at:

```julia
using LinearAlgebra

function f(v)
    n = length(v)
    h = 1 / (n + 1)
    y = zeros(n)
    y[1] = -2v[1] + v[2] + (1 / 4) * exp(v[1])
    y[n] = -2v[n] + v[n-1] + (h^2) * exp(v[n])
    for i in 2:n-1
        h = 1 / (i + 1)
        y[i] = ((v[i+1] - 2v[i] + v[i-1]) / h^2) + exp(v[i])
        y[i] *= h^2
    end
    return y
end

function jacobian_exact(f, x)
    n = length(x)
    J = zeros(n, n)
    for i = 1:n
        for j = 1:n
            h = 1 / (i + 1)
            if i == j
                J[i, j] = 2 / h^2 + exp(x[i])
            elseif i == j + 1 || i == j - 1
                J[i, j] = -1 / h^2
            end
        end
    end
    return J
end

function jacobian_approx(f, x, epsilon)
    n = length(x)
    J = zeros(n, n)
    for j in 1:n
        ej = zeros(n)
        ej[j] = 1
        J[:, j] = (f(x + epsilon * ej) - f(x)) / epsilon
    end
    return J
end

function newton_method(f, x0, epsilon, max_iter, method::Symbol=:exact)
    x = copy(x0)
    iter = 0
    while norm(f(x)) > epsilon && iter < max_iter
        if method == :exact
            J = jacobian_exact(f, x)
        elseif method == :approximate
            J = jacobian_approx(f, x, epsilon)
        else
            error("Invalid method specified. Must be :exact or :approximate.")
        end
        x -= J \ f(x)
        iter += 1
    end
    return x
end

epsilon = 1e-6
xr = collect(1:4)
jacobian_exact(f, xr)
jacobian_approx(f, xr, epsilon)

x0 = zeros(4)
exact_root_newton = newton_method(f, x0, epsilon, 100, :exact)
approx_root_newton = newton_method(f, x0, epsilon, 100, :approximate)

```

From what I can tell, the `jacobian_approx` function seems to be correct, so I would say there is either an error in `jacobian_exact` or in the function `f` itself. What is the function you’re trying to implement?

If you’re new to Julia, you may not be aware of it, but there are plenty of packages that will compute exact derivatives for you much more reliably: see the [JuliaDiff](https://juliadiff.org/) webpage for a list

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [March 9, 2023, 7:13pm UTC](https://discourse.julialang.org/t/incoherence-between-exact-and-finite-differences-jacobian/95816/3 "2023-03-09T19:13:20Z")

</div>

Also, could you maybe modify the title of your question to something more specific, like “Incoherence between exact and finite differences Jacobian”? Basically everyone posting here needs help with a task 😉

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 10, 2023, 6:25am UTC](https://discourse.julialang.org/t/incoherence-between-exact-and-finite-differences-jacobian/95816/4 "2023-03-10T06:25:52Z")

</div>

The OPs problem is a homework problem, so he has to solve it without library support. That said a library can of course be helpful to check results.

OP make sure to let people know that you are looking for help with academic coursework so that people responding can tailor their replies appropriately and you’re not violating any academic integrity rules.

---

<div class="post-metadata">

**Author:** ![GuyFromBosnia](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@GuyFromBosnia](https://discourse.julialang.org/u/GuyFromBosnia)\
**Post date:** [March 10, 2023, 9:51am UTC](https://discourse.julialang.org/t/incoherence-between-exact-and-finite-differences-jacobian/95816/5 "2023-03-10T09:51:39Z")

</div>

![Screenshot_2023-03-10-10-50-25-321_com.microsoft.teams](https://global.discourse-cdn.com/julialang/original/3X/c/6/c6bfbb17d4b09031103764d8d0bfbf178e59e0f6.jpeg)  
The second function is the one I am trying to implement i have fixed the exact jacobian function so I am getting almoast the same ones as approximated but I am not sure if the results are correct
