% m23_fminunc.m - minimisation of m22.m function using Matlab's fminunc function m23() x = [-3:.1:3]; y = x; [xx,yy] = meshgrid(x,y); ff = xx.^2+yy.^2+yy.*sin(xx)-sin(2*yy); hold off, contour(xx,yy,ff,15) % contour plot colorbar, grid on, hold on LW = 'linewidth'; MS = 'markersize'; plot(-.2714,.5635,'.k',MS,30) % the solution x = input('initial x? '); y = input('initial y? '); xold = x; yold = y; X = fminunc(@f,[x;y]) % minimisation function f = f(X) % compute function x = X(1), y = X(2) f = x.^2+y.^2+y.*sin(x)-sin(2*y); plot([xold x],[yold y],'-r',LW,1.2) % plot line to current point plot(x,y,'.r',MS,15) % plot current point disp(' '), pause xold = x; yold = y; end end