FindMinimum v. NMinimize and an external program
- To: mathgroup at smc.vnet.net
- Subject: [mg125161] FindMinimum v. NMinimize and an external program
- From: EcoTheory <carroll.ian at gmail.com>
- Date: Sat, 25 Feb 2012 01:56:03 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
Can anyone say whether the following behavior is expected and why? I'd like to avoid learning how to use MathLink, but I need to use FindMinimum on a large external calculation and my way is broken.
Say there's a command line program called 'prog' that calculates y = (x-1)^2, reading x from a named pipe called 'in' and writing y to a named pipe called 'out'. Mathematica interacts with prog by reading and writing to the pipes. Like ...
> in = OpenWrite["in"];
> f[x_?NumericQ]:=(Write[in, CForm[x]]; First[ReadList["!cat out", Number]])
Calling NMinimize on f gives the correct answer, but FindMinimum doesn't. Specifically, FindMinimum[f[x], {x, 0}] returns the message FindMinimum::fmgz. That is, Mathematica thinks the gradient is zero.
I can't find a reason the two functions should behave differently. Any thoughts?
A couple details for the DIYers. Here's prog:
#include <stdio.h>
#include <math.h>
int main()
{
FILE * io;
int j;
double x,y;
while((j = scanf("%lf", &x)) != EOF)
{
y = pow((x-1.0), 2);
io = fopen("out", "w");
fprintf(io, "%e\n", y);
fclose(io);
}
return;
}
Run prog with a redirect for stdin, as in
/prog < in