#include #include #include #include #include "nr.h" #include "nrutil.h" #define NDIM 2 float func(float x[]) { int i; float f=0.0; f = (x[1]-1.0)*(x[1]-1.0) + 100*(x[2]-3.0)*(x[2]-3.0); // f = pow(x[1]-x[2], 2) + pow(x[1]+x[2], 20); return f; } void dfunc(float x[], float d[]) { d[1] = 2.0*(x[1]-1.0); d[2] = 200.0*(x[2]-3.0); // d[1] = 2*(x[1]-x[2]) + 20*pow(x[1]+x[2], 19); // d[2] = 2*(x[2]-x[1]) + 20*pow(x[1]+x[2], 19); } int main(void) { int i,j,iter; float fret,sr2,x,*p,*xi; p=vector(1,NDIM); xi=vector(1,NDIM); printf("\nMinimum of a 2-d quadratic centered\n"); printf("at (1.0,3.0). \n"); printf("%9s %12s %12s \n","x","y","functin value"); frprmn(p, NDIM, 1.0e-3, &iter, &fret, func, dfunc); printf("iteration = %i \n", iter); free_vector(xi,1,NDIM); free_vector(p,1,NDIM); return 0; }