% This program finds the maximum of the function. % It finds the local maximums, and hopefully finds % the global maximum too. clear step_x=0.01; % x=-2:0.01:2; % y=exp(1./(1+x.^2))+0.1*sin(20*x); x=-10:step_x:10; y=(x-2).*cos(x); plot(x,y) hold on grid on y1=inline('(x-2).*cos(x)'); % y1=inline('exp(1./(1+x.^2))+0.1*sin(20*x)'); lok_max=[]; lok_min=[]; for i=x(2):step_x:x(end-1) if (y1(i)>y1(i-0.01)) && (y1(i)>y1(i+0.01)) lok_max=[lok_max; i y1(i)]; plot(i, y1(i),'rs','MarkerFaceColor','r') pause(0.05) elseif (y1(i)