The following code is my attempt of employing Broyden's method for root finding on the function $f(x,y)=(e^{xy}-y^2-2,\cos(x+y)+\frac{1}{2})$. Where the first matrix is the Jacobian, then it gets updated using an estimate. So $k[l,k]$ computes the first 12 iterations. However, this program is extremely slow and I am not sure why. Could anyone point out why it is so slow and how I could improve it?
k[l_, k_] := Module[{m},
f[{x_, y_}] := {Exp[x y] - y^2 - 2, Cos[x + y] + 1/2};
h = {Exp[x y] - y^2 - 2, Cos[x + y] + 1/2};
b = {x, y};
m[0] := {{l}, {k}};
J[0] := D[h, {b}];
m[n_] :=
m[n] = N[m[n - 1]] -
Inverse[N[J[n - 1]]].N[f[m[n - 1]]] /. {x ->
Part[Part[N[m[n - 1]], 1], 1],
y -> Part[Part[N[m[n - 1]], 2], 1]};
J[n_] :=
J[n] = N[
J[n - 1]] + ((N[f[m[n]]] - N[f[m[n - 1]]] -
N[J[n - 1]].(N[m[n] - m[n - 1]]))/
Norm[N[m[n] - m[n - 1]]]^2).Transpose[N[m[n]] - N[m[n - 1]]];
Table[m[a], {a, 1, 12}]]
I would appreciate any input.
EDIT: I found some optimization to my code. I removed the redundant $N$ as per a suggestion in the comments. The optimization I found is to deal with numerical matrices form the get go. The original code did everything for arbitrary $x,y$
k[l_, k_] := Module[{m},
f[{x_, y_}] := {Exp[x y] - y^2 - 2, Cos[x + y] + 1/2};
h = {Exp[x y] - y^2 - 2, Cos[x + y] + 1/2};
b = {x, y};
m[0] := N[{{l}, {k}}];
J[0] :=
N[D[h, {b}]] /. {x -> Part[Part[m[0], 1], 1],
y -> Part[Part[m[0], 2], 1]};
m[n_] := m[n] = m[n - 1] - LinearSolve[J[n - 1], f[m[n - 1]]];
J[n_] :=
J[n] = J[
n - 1] + ((f[m[n]] - f[m[n - 1]] - J[n - 1].(m[n] - m[n - 1]))/
Norm[m[n] - m[n - 1]]^2).Transpose[m[n] - m[n - 1]];
Table[m[a], {a, 1, 15}]]
```
N
s, and in-body function definitions. An iterative algorithm might be better withNest
. I'm not familiar with this numerical method but I've rewritten it here: pastebin.com/Xhu4n2v8 . It's quite sensitive to the initial value and converges a bit slowly for this function, so I may not have got it right, but you get the gist of it. $\endgroup$LinearSolve
) and to use the Sherman-Morrison formula to deal with the rank-1-updates. At the moment OP's code looks as if the a linear solve were performed in each iteration. (The many linear solves are typically the reason why Newton's method takes so long. Not in dimension, 2 though.) $\endgroup$