回 帖 发 新 帖 刷新版面

主题:急!!!!!哪为大侠能给我个黄金分割法MATLAB寻优程序??

哪为大虾能给我一个黄金分割发  和 fibonicca法MATLAB寻优程序???急,还有两天就交报告了!!!!
谢了![em22]

回复列表 (共4个回复)

沙发

工作室可有偿提供解决方案,欢迎咨询

板凳


1.  黄金分割法求极小值

    功能:用黄金分割法求f(x)在区间[a,b]上的近似极小值。当且仅当f(x)在[a,b]上为单峰时次方法适用

----------------------------------------------------------------------

function [S,E,G]=golden(f,a,b,delta,epsilon)

% input  - f is the object function input as a string 'f'

%        - a and b are the end points of the interval

%        - delta is the tolerance for the abscissas

%        - epsilon is the tolerance for the ordinates

% output - S=(p,yp) contains the abscissa p and the ordinate yp of  
%          the minimum

%        - E=(dp,dy) contains the error bounds for p and yp

%        -G is an n*4 matrix : the kth row contains

%        -[ak ck dk bk]: the values of a,c,d and b at the kth interation

r1= (sqrt(5)-1)/2;

r2=r1^2;

h=b-a;

ya=feval(f,a);

yb=feval(f,b);

c=a+r2*h;

d=a+r1*h;

yc=feval(f,c);

yd=feval(f,d);

k=1;

A(k)=a;B(k)=b;C(k)=c;D(k)=d;

while (abs(yb-ya)>epsilon) | (h>delta)

      k=k+1;

      if (yc<yd)

         b=d;
   
         yb=yd;

         d=c;

         yd=yc;

         h=b-a;

         c=a+r2*h;

         yc=feval(f,c);

      else 

         a=c;

         ya=yc;

         c=d;

         yc=yd;

         h=b-a;

         d=a+r1*h;

         ya=feval(f,d);

      end

      A(k)=a;B(k)=b;C(k)=c;D(k)=d;

end

dp=abs(b-a);

dy=abs(yb-ya);

p=a;

yp=ya;

if (yb<ya)

   p=b;

   yp=yb;

end;

G=[A' C' D' B'];

S=[p yp];

E=[dp dy];

3 楼


2. 斐波那契法求极小值

   功能:用斐波那契法求f(x)在区间[a,b]上的近似极小值。当且仅当f(x)在[a,b]上为单峰时次方法适用

----------------------------------------------------------------------

function X=fibonacci(f,a,b,tol,e)

% input  - f is the object function input as a string 'f'

%        - a and b are the end points of the interval

%        - tol : length of uncertainy

%        - e :distinguishability of constant

% output - X : x and y coordinates of minimum

%   note : this function calls the m-file fib.m while had been edited 


i=1;

F=1;

while F<=(b-a)/tol

      F=fib(i);

      i=i+1;

end

n=i-1;

A=zeros(1,n-2);B=zeros(1,n-2);

A(1)=a;

B(1)=b;

c=A(1)+(fib(n-2)/fib(n))*(B(1)-A(1));

d=A(1)+(fib(n-1)/fib(n))*(B(1)-A(1));

k=1;

while k=n-3

      if feval(f,c)>feval(f,d)

         A(k+1)=c;

         B(k+1)=B(k);

         c=d;

         d=A(k+1)+(fib(n-k-1)/fib(n-k))*(B(k+1)-A(k+1));

      else

         A(k+1)=A(k);

         B(k+1)=d;

         d=c;

         c=A(k+1)+(fib(n-k-2)/fib(n-k))*(B(k+1)-A(k+1));

      end

      k=k+1;

end

if feval(f,c)>feval(f,d)

   A(n-2)=c;

   B(n-2)=B(n-3);

   c=d;

   d=A(n-2)+(0.5+e)*(B(n-2)-A(n-2));

else 

   A(n-2)=A(N-3);

   B(n-2)=d;

   d=c;

   c=A(n-2)+(0.5+e)*(B(n-2)-A(n-2));

end

if feval(f,c)>feval(f,d)

   a=c;

   b=B(n-2);

else

   a=A(n-2);

   b=d;

end

X=[(a+b)/2 feval(f,(a+b)/2)];

-------------------------------------------------------------
% 下面的程序用来生成斐波那契数

function y=fib(n)

fz(1)=1;

fz(2)=1;

for k=3:n

    fz(k)=fz(k-1)+fz(k-1);

end

y=fz(n);

4 楼


收益颇深

我来回复

您尚未登录,请登录后再回复。点此登录或注册