Just now I am attempting to apply some very simple matrix algebra to improve an approximation by adjusting a few numbers in an initial configuration to achieve desired results later in the simulation. Applying the pattern or frame by rote, or crude analogy, results in code that fails to achieve the desired results. I thought that I would record here the images that went thru my head in applying the old frame since it seems that I must recall some of the logic of the frame in order to find the bug. It is not so much logic as imagery that I must arrange in a logical order.
Early in learning the notion of derivative we are shown a picture such as this:
The notion of the slope of a line is extended to the slope of a curve, or a function, at a point on that curve, or some value of the argument of the function.
If you want to find r such that, f(r) = 0 we have a geometric clue from the picture.
We can guess rather directly that
r = x − f(x)/((f(x+Δx)−f(x))/Δx)
will usually be much closer to the r that we really want.
((f(x+Δx)−f(x))/Δx) serves as an approximation to the derivative here but to find our root there is no particular reason that Δx should be small.
I would not have started this page if the above were all that concerned me but in the place of real numbers implicitly assumed above I have a problem with a triple of reals in place of a real, in both the roles of argument and value of f. A closely related (parasitic) frame is that such a generalization is possible for the above frame. The images are missing however and I fall back on the shape of the formula which survives the transformation. My first attempt was to blindly code by analogy seeing f as a function of 3 real arguments and yielding 3 real values. The generalization of the derivative is the Jacobian and accordingly we replace (f(x+Δx)−f(x))/Δx by the matrix J. By f(x)/J we mean only the solution z to the equation Jz = f(x).
In the code function td(x, er) computes f(x) and deposits the three values in the array er. Just after the line of code {int u, v; for(u=0; u<3; ++u) for(v=0; v<3; ++v) m[u][v] -= e[v];}, er holds f(x+Δx)−f(x) for one of three possible Δx’s. In our analogy there are three reals in each value of f and three sets of these triples—one for each variation on Δx. Collectively this is captured in the 3 by 3 matrix J.
I found the bug and it is, of course, an anti-climax. I had to transpose J. It is the one part of the pattern that the analogy would not provide. Over and out for now.