function Blatt2() h=1; tol=0.01; z0=[-10,10]; disp(['Funktion aus (a), Startpunkt z0']); [x,y]=Minimum(z0, h, tol, @f1) z0=[0,5]; disp(['Funktion aus (b), Startpunkt z01']); [x,y]=Minimum(z0, h, tol, @f2) z0=[0.5,5]; disp(['Funktion aus (b), Startpunkt z02']); [x,y]=Minimum(z0, h, tol ,@f2) end function u=f1(z) u=1/6*z(1)^2+z(2)^2+1/4*z(1)+1; end function u=f2(z) u=1/4*(z(1)^4*z(2)^2 + z(1)^4) - z(1)^3*z(2)^2 - z(1)^3 + z(1)*z(2)^2 + z(1) + 5*z(2)^2 + 5; end function [fznew, z_current] = Minimum(z0, h0, tol, f) % exercise 2.6 i = 1; z_current = z0; dir = [0,1;... %N -1,0;... %W 1,0;... %O 0,-1]; %S % compute first value at current point and save some output - ensure column vectors for convenience fz(i,1) = f(z_current); h_it(i,1) = h0; z(i,:) = z_current; h=h0; while h > tol % as long as tolerance isn't reached % compute function value in each direction fzN = f(z_current + h*dir(1,:)); fzW = f(z_current + h*dir(2,:)); fzO = f(z_current + h*dir(3,:)); fzS = f(z_current + h*dir(4,:)); % compute min and argmin [fznew, ind] = min([fzN, fzW, fzO, fzS, fz(i,1)]); % If minimal function value doesn't differ from the current halve the step size if abs(fz(i,1) - fznew) < 1e-12 h = h/2; else % else move in the respective direction z_current = z_current + h*dir(ind,:); h=h0; end i = i+1; % save function value at new point and some output fz(i,1) = fznew; h_it(i,1) = h; z(i,:) = z_current; end a=[h_it,z,fz] end