Thursday, April 29, 2010

Multivariate Newtons method in Mathemathica

Using the previously mentioned Jacobian function, here's a way to do Newton's method in multiple variables.

Here's a simple vector valued function:

In: f[{x_, y_}] := {x^2 y + 1, E^x + y}


The Jacobian:

In: jacobian[f, {x, y}]

Out: {{2 x y, x^2}, {E^x, 1}}


To construct a Jacobian function that we can evaluate we have to create a Mathematica. To evaluate the derivatives in place we use other vector component names and replace them after calculating the derivative:

In: df[{x_, y_}] := jacobian[f, {a, b}] /. {a -> x, b -> y}


With the Jacobian function it's easy to set up the iteration with FixedPointList and an anonymous function:

In: FixedPointList[(# - Inverse[df[#]].f[#]) &, {-0.7, -2.5}, 20]

Out: {{-0.7, -2.5}, ... , {0.703467, -2.02075}}


-a

Jacobian function for Mathematica

Following up on the vector valued functions, here's a way to compute Jacobian matrices for them:

In: jacobian[f_, c_] := Transpose[Map[D[f[c], #] &, c]]


You need to pass in a list of components for the vector argument along with the vector valued function:

In: f[{x_, y_}] := {2 x^2 y, x*y}


In: jacobian[f, {x, y}]


Out: {{4 x y, y}, {2 x^2, x}}


-a

Tuesday, April 27, 2010

One way to use vector valued functions in Mathematica

Struggling with some homework recently, I needed to do iteration on vector valued functions to find fix points. One way to do this is to pass a list/vector with the components as function arguments, and return the list/vector valued result.

For example:

f[{x_, y_}] := {1.01 x - 3*10^-5*x*y, 0.98 y + 10^-5*x*y}


The fix point iteration can then be handled by FixedPointList:


FixedPointList[f, {1000, 100}, 1000]


Plotting the result componentwise:


ListLinePlot[{%%[[All, 1]], %%[[All, 2]]}]


Sunday, April 18, 2010

Spurious corpus failures in IMS Corpus Workbench

At the moment I'm working with Open CWB, also known as the IMS Corpus Workbench. The last couple of days I was getting a lot of corpus errors seemingly for no reason:

[no corpus]> E
Warning:
Data access error (CL: can't load and/or create necessary data)
Perhaps the corpus E is not accessible from the machine you are using.
CQP Error:
Corpus ``E'' is undefined

Looking at the corpus registry, the cwb-encode command evidently had stored the corpus data and info locations as relative paths. Consequently the corpus would only work if cqp was invoked in the same directory as it was encoded from.

So if you give relative paths when running cwb-encode, make sure to fix up the HOME and INFO lines in the registry file afterwards!


-a