fortran 编程黄金分割法求极小值
- 格式:docx
- 大小:13.66 KB
- 文档页数:1
黄金分割法迭代次数与精度研究function [ x,fval,iter] = gold_section_method(a,b,d) %黄金分割法主程序%x 极致点%fval 函数极值%iter 迭代次数%a b 搜索区间%d 精度iter=0;while abs(b-a)>diter = iter+1;x1=a+0.382*(b-a);x2=a+0.618*(b-a);f1=Function1(x1);f2=Function1(x2);if f1>f2a=x1;elseb=x2;endendx=(a+b)/2;fval = Function1(x);end算例函数fval = x^3-1;fval = x^4-1;fval = x^5-1;结果分析:表一算例结果表一为算例计算结果,通过查表可知:1.计算精度越小,迭代次数越多。
2.在相同精度、搜索区间条件下,仅修改算例对迭代次数无影响。
3.计算精度减小10倍,迭代次数增加5次左右。
这种收敛表现与黄金分割法的缩小区间比例有关。
下面为对特点3进行简单的推导:迭代一次,区间长度由l变为0.618l,当取精度为ε,计算迭代次数为k,此时的条件为:b a ε-<此时对精度进行调节'0.1εε=则迭代次数需增加p ,使得:()(0.618)0.1p b a ε-⨯<又 b a ε-<p ⇒≈5上述推导过程证明了黄金分割法的迭代次数与精度关系,同时也说明黄金分割法的收敛速度是均匀的。
黄⾦分割法及其代码线性搜索之黄⾦分割法及其应⽤摘要最优化理论和⽅法⽇益受到重视,已经渗透到⽣产、管理、商业、军事、决策等各个领域,⽽最优化模型与⽅法⼴泛应⽤于⼯业、农业、交通运输、商业、国防、建筑、通讯和政府机关等领域。
伴随着计算机技术的⾼速发展,最优化理论与⽅法的迅速进步为解决实际最优化问题的软件也在飞速发展。
其中,MATLAB 软件已经成为最优化领域应⽤最⼴的软件之⼀。
有了MATLAB这个强⼤的计算平台,既可以利⽤MATLAB优化⼯具箱(OptimizationToolbox)中的函数,⼜可以通过算法变成实现相应的最优化计算。
在最优化计算中⼀维最优化⽅法是优化设计中最简单、最基本的⽅法。
⼀维搜索,⼜称为线性搜索,⼀维问题是多维问题的基础,在数值⽅法迭代计算过程中,都要进⾏⼀维搜索,也可以把多维问题化为⼀些⼀维问题来处理。
⼀维问题的算法好坏,直接影响到最优化问题的求解速度。
⽽黄⾦分割法是⼀维搜索⽅法中重要的⽅法之⼀,它适⽤于任何单峰函数求最⼩值的问题,甚⾄于对函数可以不要求连续,是⼀种基于区间收缩的极⼩点搜索算法。
关键词:最优化、黄⾦分割法、MATLAB软件、⼀维搜索引⾔数学科学不仅是⾃然科学的基础,也是⼀切重要技术发展的基础。
最优化⽅法更是数学科学⾥⾯的⼀个巨⼤的篇幅,在这个信息化的时代,最优化⽅法⼴泛应⽤于⼯业、农业、国防、建筑、通信与政府机关、管理等各领域;它主要解决最优计划、最优分配、最优决策、最佳设计、最佳管理等最优化问题。
⽽最优解问题是这些所有问题的中⼼,是最优化⽅法的重中之重,在求最优解问题中,有多种⽅法解决,我们在这⾥着重讨论⽆约束⼀维极值问题,即⾮线性规划的⼀维搜索⽅法之黄⾦分割法。
黄⾦分割法也叫0.618法,属于区间收缩法,⾸先找出包含极⼩点的初始搜索区间,然后按黄⾦分割点通过对函数值的⽐较不断缩⼩搜索区间。
当然要保证极⼩点始终在搜索区间内,当区间长度⼩到精度范围之内时,可以粗略地认为区间端点的平均值即为极⼩值的近似值。
Fortran中求最⼤值、最⼩值、平均数、众数和分布program mh3implicit none!This code is used to calculate the maximum,minimum,average and mode of high temperature for a year. The distribution of temperature!is also given.The file 'temperatures.txt' is read. Written by shuirongbinginteger, parameter ::t=365 !Data lengthinteger, parameter ::s=10 !The number of equally separated rangesinteger :: ireal*8, dimension(t) :: temreal*8 :: maxm,maximumreal*8 :: minm,minimumreal*8 :: aver,averagereal*8 :: mode,modeeinteger, dimension(s) ::bininteger ::binnopen(2,file='temperatures.txt',form='formatted')do i=1,tread(2,222) tem(i) !load data into the matrix tem222 format(F10.0)enddoaverage=aver(tem,t) !calculate the average by function aver(.,.)minimum=minm(tem,t) !calculate the minimum by function minm(.,.)maximum=maxm(tem,t) !calculate the maximum by function maxm(.,.)mode=modee(tem,t) !calculate the mode by function modee(.,.)do i=1,sbin(i)=binn(tem,t,s,i) !Count the number of data points within each rangeenddo!Print results in a formatted way:print *,'Here is the statistic of temperatures for the year 2009(in degrees Fahrenheit)'print 333,'The average high temperature for the year was:',averageprint 333,'The minimum high temperature for the year was:',minimumprint 333,'The maximum high temperature for the year was:',maximumprint 333,'The mode high temperature for the year was:',modeprint *,'Temperature Distribution for the year was:'print 334,'10-19','20-29', '30-39', '40-49', '50-59', '60-69', '70-79', '80-89', '90-99', '100-110'print 335, bin333 format(A50,F5.2)334 format(10A12)335 format(10I12)end program mh3!%%%%Functions are listed below%%%%%%!Take averagereal function aver(tem,t)implicit noneinteger ::tinteger ::ireal*8, dimension(t) ::temreal*8 :: summ=0.00do i=1,tsumm=summ+tem(i)enddoaver=summ/real(t)returnend function aver!Take minimumreal function minm(tem,t)implicit noneinteger ::tinteger ::ireal*8, dimension(t) ::temminm=tem(1)do i=2,tif (minm>=tem(i)) thenminm=tem(i)endifenddoreturnend function minm!Take maximumreal function maxm(tem,t)implicit noneinteger ::tinteger ::ireal*8, dimension(t) :: temmaxm=tem(1)do i=2,tif (maxm<=tem(i)) thenmaxm=tem(i)endifend doreturnend function maxmreal function modee(tem,t)implicit noneinteger ::treal*8 :: minimum,minm,maximum,maxm,modeeeinteger, parameter ::tt=100integer ::i,jreal*8, dimension(t) :: temreal*8, dimension(tt,2) ::Mminimum=minm(tem,t)maximum=maxm(tem,t)modeee=0do i=1,ttM(i,1)=minimum+1.00*(i-1)M(i,2)=0.00enddoif (M(tt,1)<maximum) thenprint *,'Error:please reset the gap between min and max!' stopendifdo i=1,ttdo j=1,tif (tem(j)==M(i,1)) thenM(i,2)=M(i,2)+1endifenddoenddodo i=1,ttif (modeee<=M(i,2)) thenmodeee=M(i,2)modee=M(i,1)endifenddoend function modeeinteger function binn(tem,t,s,i)implicit noneinteger ::t,s,i,jreal*8, dimension(t) ::teminteger ::imin,imax,mimin=10imax=110m=(imax-imin)/sbinn=0do j=1,tif (tem(j)>=imin*i .AND. tem(j)<imin*(i+1)) then binn=binn+1endifenddoend function binn。
C语⾔黄⾦分割法确定极⼩值#include <stdio.h>#include <math.h>#define F(x) (3*x*x*x-4*x+2)double fun1(double x);void goAndBackSectionPrint(double x1, double h, double (*f)(double), double *ret1, double *ret2);void goldenSecton(double x0, double h, double (*f)(double), double e);int main(void){goldenSecton(0, 1, fun1, 0.1);return 0;}//进退法打印区间//x1 搜索初值//h 搜索步长//f 待搜索函数f(x)的指针//ret1 ret2 返回区间指针void goAndBackSectionPrint(double x1, double h, double (*f)(double), double *ret1, double *ret2){double a1 = x1;double y1 = (*f)(a1);double a2 = a1 + h;double y2 = f(a2);double a3 = 0;double y3 = 0;if(y2 > y1){h = -h;a3 = a1;y3 = y1;a1 = a2;y1 = y2;a2 = a3;y2 = y3;}a3 = a2 + h;y3 = f(a3);while(y3 < y2){h = 2*h;a1 = a2;y1 = y2;a2 = a3;y2 = y3;a3 = a2 + h;y3 = f(a3);}*ret1 = a1;*ret2 = a3;printf("a1 = %f, y1 = %f.\n", a1, y1);printf("a2 = %f, y2 = %f.\n", a2, y2);printf("a3 = %f, y3 = %f.\n", a3, y3);}//黄⾦分割法//进退法打印区间//x0 搜索初值//h 搜索步长//f 待搜索函数f(x)的指针//e 误差要求void goldenSecton(double x0, double h, double (*f)(double), double e){double a, b;const double ratio = 0.618;goAndBackSectionPrint(x0, h, f, &a, &b);printf("确定区间为 [%f, %f].\n", a, b);double a1 = b - ratio*(b - a);double y1 = f(a1);double a2 = a + ratio*(b - a);double y2 = f(a2);do{if(y1 >= y2){a = a1;a1 = a2;y1 = y2;a2 = a + ratio*(b - a);y2 = f(a2);}else{b = a2;a2 = a1;y2 = y1;a1 = b - ratio*(b - a);y1 = f(a1);}}while((fabs((b-a)/b) >= e) || (fabs((y2-y1)/y2) >= e)); printf("a = %f, f(a) = %f.\n", a, f(a));printf("b = %f, f(b) = %f.\n", b, f(b));double minVal = (a+b)/2;printf("函数的最⼩值点为:%f。
黄金分割法黄金分割法也叫0.618法,它是一种基于区间收缩的极小值点搜索算法,当用进退法确定搜索区间后,我们只知道极小值点包含于搜索区间内,但是具体是哪个点,无法得知。
1. 算法原理黄金分割法的思想很直接,既然极小值点包含于搜索区间内,那么可以不断地缩小搜索区间,就可以使搜索区间的端点逼近到极小值点。
[]a,b 为搜索区间,黄金分割法首先根据黄金比例产生两个内点12,x x 。
120.382*()0.618*()x a b a x a b a =+-=+-然后根据()1f x ,()2f x 的大小关系来重新选择搜索区间。
(1) 若()()12f x f x <,则搜索区间变为1[,]x b ;(2) 若()()12f x f x >,则搜索区间变为2[,]a x 。
2. 算法步骤用黄金分割法求无约束问题min (),f x x R ∈的基本步骤如下:(1) 选定初始区间11[,]a b 及精度0ε>,计算试探点:11110.382*()a b a λ=+-11110.618*()a b a μ=+-。
(2) 若k k b a ε-<,则停止计算。
否则当()()k k ff λμ>时转步骤(3)。
当()()k k f f λμ≤转步骤(4)。
(3) 置 11111110.382*()k k k k k kk k k k a b b a b a λλμμ+++++++=⎧⎪=⎪⎨=⎪⎪=+-⎩转步骤(5) (4) 置11111110.382*()k k k k k kk k k k a a b a b a μμλλ+++++++=⎧⎪=⎪⎨=⎪⎪=+-⎩转步骤(5) (5) 令1k k =+,转步骤(2)。
3. 算法的MATLAB 实现在MATLAB 中编程实现黄金分割法的函数为:min HJ 。
功能:用黄金分割法求解一维函数的极值。
调用格式:[,min ]min (,,,)x f HJ f a b eps =其中,f :为目标函数;a :极值区间的左端点;b :极值区间的右端点;e p s :精度;x :目标函数取最小值时的自变量值;m i n f :目标函数的最小值。
已知:F(x)=x4-4x3-6x2-16x+4,求极小值,极小值点,区间,迭代次数?用进退法确定区间,用黄金分割法求极值。
#include <stdio.h>#include <math.h>#define e 0.001#define tt 0.01float f(double x){float y=pow(x,4)-4*pow(x,3)-6*pow(x,2)-16*x+4;return(y);}finding(float *p1,float*p2){float x1=0,x2,x3,t,f1,f2,f3,h=tt;int n=0;x2=x1+h;f1=f(x1);f2=f(x2);if(f2>f1) {h=-h;t=x2;x2=x1;x1=t;}do{ x3=x2+h;h=2*h;f3=f(x3);n=n+1;}while(f3<f2);if(x1>x3) {t=x1;x1=x3;x3=t;}*p1=x1;*p2=x3;return(n);}gold(float *p){float a,b,x1,x2,f1,f2; int n=0;finding(&a,&b);do{x1=a+0.382*(b-a);x2=a+0.618*(b-a);f1=f(x1);f2=f(x2);n=n+1;if(f1>f2) a=x1;else b=x2;}while((b-a)>e);*p=(x1+x2)/2;return(n);}main(){float a,b,x,min;int n1,n2;n1=finding(&a,&b);n2=gold(&x);min=f(x);printf("\n The area is %f to %f.",a,b); printf("\n The nunmber 1 is %d.",n1);printf("\n The min is %f and the result is %f.",x,min);printf("\n The nunmber 2 is %d.",n2)二插法已知:F(x1,x2)=4*x1-x2的平方-12;求极小值,极小值点,迭代次数?用复合形法求极值。