1. 程式人生 > >Matlab繪製Griewank函式三維影象

Matlab繪製Griewank函式三維影象

function y = Griewank(x)
% Griewan函式
% 輸入x,給出相應的y值,在x = ( 0 , 0 ,…, 0 )處有全域性極小點0.
% 編制人:
% 編制日期:
[row,col] = size(x);
if  row > 1 
    error( ' 輸入的引數錯誤 ' );
end
y1 = 1 / 4000 * sum(x.^ 2 );
y2 = 1 ;
for  h = 1 :col
    y2 = y2 * cos(x(h) / sqrt(h));
end
y = y1 - y2 + 1 ;
y =- y;

function DrawGriewank()
% 繪製Griewank函式圖形
x=[ -10:0.1 : 10 ];
y =[ -10 :0.1 : 10 ];
[X,Y] = meshgrid(x,y);
[row,col] = size(Y);
for  l = 1 :col
     for  h = 1 :row
        z(h,l) = Griewank([X(h,l),Y(h,l)]);
    end
end
surf(X,Y,z);
shading interp