c10-4 (Numerical Recipes in C), страница 2
Описание файла
Файл "c10-4" внутри архива находится в папке "Numerical Recipes in C". PDF-файл из архива "Numerical Recipes in C", который расположен в категории "". Всё это находится в предмете "цифровая обработка сигналов (цос)" из 8 семестр, которые можно найти в файловом архиве МГТУ им. Н.Э.Баумана. Не смотря на прямую связь этого архива с МГТУ им. Н.Э.Баумана, его также можно найти и в других разделах. Архив можно найти в разделе "книги и методические указания", в предмете "цифровая обработка сигналов" в общих файлах.
Просмотр PDF-файла онлайн
Текст 2 страницы из PDF
On output, p andy will have been reset to ndim+1 new points all within ftol of a minimum function value, andnfunk gives the number of function evaluations taken.{float amotry(float **p, float y[], float psum[], int ndim,float (*funk)(float []), int ihi, float fac);int i,ihi,ilo,inhi,j,mpts=ndim+1;float rtol,sum,swap,ysave,ytry,*psum;psum=vector(1,ndim);*nfunk=0;GET_PSUMfor (;;) {ilo=1;First we must determine which point is the highest (worst), next-highest, and lowest(best), by looping over the points in the simplex.ihi = y[1]>y[2] ? (inhi=2,1) : (inhi=1,2);for (i=1;i<=mpts;i++) {if (y[i] <= y[ilo]) ilo=i;if (y[i] > y[ihi]) {inhi=ihi;ihi=i;} else if (y[i] > y[inhi] && i != ihi) inhi=i;}rtol=2.0*fabs(y[ihi]-y[ilo])/(fabs(y[ihi])+fabs(y[ilo])+TINY);Compute the fractional range from highest to lowest and return if satisfactory.if (rtol < ftol) {If returning, put best point and value in slot 1.SWAP(y[1],y[ilo])for (i=1;i<=ndim;i++) SWAP(p[1][i],p[ilo][i])break;}if (*nfunk >= NMAX) nrerror("NMAX exceeded");*nfunk += 2;Begin a new iteration.
First extrapolate by a factor −1 through the face of the simplexacross from the high point, i.e., reflect the simplex from the high point.ytry=amotry(p,y,psum,ndim,funk,ihi,-1.0);if (ytry <= y[ilo])Gives a result better than the best point, so try an additional extrapolation by afactor 2.ytry=amotry(p,y,psum,ndim,funk,ihi,2.0);else if (ytry >= y[inhi]) {The reflected point is worse than the second-highest, so look for an intermediatelower point, i.e., do a one-dimensional contraction.ysave=y[ihi];ytry=amotry(p,y,psum,ndim,funk,ihi,0.5);if (ytry >= ysave) {Can’t seem to get rid of that high point.
Betterfor (i=1;i<=mpts;i++) {contract around the lowest (best) point.Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).#include <math.h>#include "nrutil.h"#define TINY 1.0e-10A small number.#define NMAX 5000Maximum allowed number of function evalua#define GET_PSUM \tions.for (j=1;j<=ndim;j++) {\for (sum=0.0,i=1;i<=mpts;i++) sum += p[i][j];\psum[j]=sum;}#define SWAP(a,b) {swap=(a);(a)=(b);(b)=swap;}412Chapter 10.Minimization or Maximization of Functionsif (i != ilo) {for (j=1;j<=ndim;j++)p[i][j]=psum[j]=0.5*(p[i][j]+p[ilo][j]);y[i]=(*funk)(psum);}}} else --(*nfunk);}free_vector(psum,1,ndim);Keep track of function evaluations.Recompute psum.Correct the evaluation count.Go back for the test of doneness and the nextiteration.}#include "nrutil.h"float amotry(float **p, float y[], float psum[], int ndim,float (*funk)(float []), int ihi, float fac)Extrapolates by a factor fac through the face of the simplex across from the high point, triesit, and replaces the high point if the new point is better.{int j;float fac1,fac2,ytry,*ptry;ptry=vector(1,ndim);fac1=(1.0-fac)/ndim;fac2=fac1-fac;for (j=1;j<=ndim;j++) ptry[j]=psum[j]*fac1-p[ihi][j]*fac2;ytry=(*funk)(ptry);Evaluate the function at the trial point.if (ytry < y[ihi]) {If it’s better than the highest, then replace the highest.y[ihi]=ytry;for (j=1;j<=ndim;j++) {psum[j] += ptry[j]-p[ihi][j];p[ihi][j]=ptry[j];}}free_vector(ptry,1,ndim);return ytry;}CITED REFERENCES AND FURTHER READING:Nelder, J.A., and Mead, R.
1965, Computer Journal, vol. 7, pp. 308–313. [1]Yarbro, L.A., and Deming, S.N. 1974, Analytica Chimica Acta, vol. 73, pp. 391–398.Jacoby, S.L.S, Kowalik, J.S., and Pizzo, J.T. 1972, Iterative Methods for Nonlinear OptimizationProblems (Englewood Cliffs, NJ: Prentice-Hall).10.5 Direction Set (Powell’s) Methods inMultidimensionsWe know (§10.1–§10.3) how to minimize a function of one variable. If westart at a point P in N -dimensional space, and proceed from there in some vectorSample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use.
Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).}*nfunk += ndim;GET_PSUM.