Professional Documents
Culture Documents
1) function iter(a,b,epsilon)
x = log((10^-5)/(b-a))/log(1/2);
fprintf('Number of iterations required = %2.0f \n', x)
iter(-1.5,-1,10^-5)
Number of iterations required = 16
>> iter(0.5,1,10^-5)
Number of iterations required = 16
>> iter(2.5,3,10^-5)
Number of iterations required = 16
ii) x=[-2:0.01:4];
>> y=x.^3-2*x.^2-3*x+2;
>> plot(x,y);
function bisection_method(a,b,tolerance,k)
ya = a^3 -2*a^2 -3*a +2; yb = b^3 -2*b^2 - 3*b +2;
while (abs(b-a) > tolerance)
x(k) = (a+b)/2;
y(k) = (x(k))^3 -2*(x(k))^2 - 3*(x(k)) +2;
if (y(k)==0)
break;
elseif (sign(y(k))== sign(ya))
a = x(k); ya=y(k);
else
b=x(k); yb=y(k);
end
k = k+1;
end
fprintf('Root x = %12.10f found after %2.0f of iterations \n', x(k-1),k-1)
bisection_method(-1.5,-1,10^-5,1)
Root x = -1.3429183960 found after 16 of iterations
>> bisection_method(0.5,1,10^-5,1)
Root x = 0.5293197632 found after 16 of iterations
>> bisection_method(2.5,3,10^-5,1)
Root x = 2.8136062622 found after 16 of iterations
2)
4 iterations
D = D+[xnew(1)^3+2*xnew(1)*xnew(2)+2*xnew(2)^21;3*xnew(1)^2+...
xnew(2)]*(xnew-x)'/((xnew-x)'*(xnew-x));
x = xnew; k = k+1;
end
fprintf('Root x = %12.10f, y=%12.10f found after %2.0f
iterations \n',...
x(1),x(2),k);
Root x = -0.4284725006, y=-0.5507660510 found after 156
iterations
4 iterations
% newton_min
tol=0.0000001; total=100; k=0; epsilon =1;
x=[0;-1]; % initial guess
while ((epsilon > tol) & (k < total)) %Newton method
nabla=[(1-2*x(1)^2)*x(2)*exp(-x(1)^2-x(2)^2);(1-2*x(2)^2)*x(1)*exp(x(1)^2-x(2)^2)];
Hessian=[(-4*x(1)*x(2))*exp(-x(1)^2-x(2)^2)+(1-2*x(1)^2)*x(2)*(2*x(1))*exp(-x(1)^2-x(2)^2) (1-2*x(1)^2)*exp(-x(1)^2-x(2)^2)+(12*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) ; (1-2*x(1)^2)*exp(-x(1)^2x(2)^2)+(1-2*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) -4*x(2)*x(1)*exp(x(1)^2-x(2)^2)+(1-2*x(2)^2)*x(1)*2*x(2)*exp(-x(1)^2-x(2)^2)];
xnew= x -Hessian\nabla; epsilon=norm(xnew-x);
x=xnew; k=k+1;
end
fprintf('Critical point x =%12.10f , y=%12.10f found after %2.0f iterations
\n' , ...
x(1),x(2),k);
newton_min
Critical point x =0.0000000000 , y=0.0000000000 found after 6 iterations
>> chol([(-4*x(1)*x(2))*exp(-x(1)^2-x(2)^2)+(1-2*x(1)^2)*x(2)*(-2*x(1))*exp(-x(1)^2-x(2)^2) (12*x(1)^2)*exp(-x(1)^2-x(2)^2)+(1-2*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) ; (1-2*x(1)^2)*exp(-x(1)^2x(2)^2)+(1-2*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) -4*x(2)*x(1)*exp(-x(1)^2-x(2)^2)+(12*x(2)^2)*x(1)*2*x(2)*exp(-x(1)^2-x(2)^2)])
??? Error using ==> chol
Matrix must be positive definite.
% newton_min
tol=0.0000001; total=100; k=0; epsilon =1;
x=[0.8;-0.8]; % initial guess
while ((epsilon > tol) & (k < total)) %Newton method
nabla=[(1-2*x(1)^2)*x(2)*exp(-x(1)^2-x(2)^2);(1-2*x(2)^2)*x(1)*exp(x(1)^2-x(2)^2)];
Hessian=[(-4*x(1)*x(2))*exp(-x(1)^2-x(2)^2)+(1-2*x(1)^2)*x(2)*(2*x(1))*exp(-x(1)^2-x(2)^2) (1-2*x(1)^2)*exp(-x(1)^2-x(2)^2)+(12*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) ; (1-2*x(1)^2)*exp(-x(1)^2x(2)^2)+(1-2*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) -4*x(2)*x(1)*exp(x(1)^2-x(2)^2)+(1-2*x(2)^2)*x(1)*2*x(2)*exp(-x(1)^2-x(2)^2)];
xnew= x -Hessian\nabla; epsilon=norm(xnew-x);
x=xnew; k=k+1;
end
fprintf('Critical point x =%12.10f , y=%12.10f found after %2.0f iterations
\n' , ...
x(1),x(2),k);
newton_min
Critical point x =0.7071067812 , y=-0.7071067812 found after 5 iterations
>> chol(-[(-4*x(1)*x(2))*exp(-x(1)^2-x(2)^2)+(1-2*x(1)^2)*x(2)*(-2*x(1))*exp(-x(1)^2-x(2)^2) (12*x(1)^2)*exp(-x(1)^2-x(2)^2)+(1-2*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) ; (1-2*x(1)^2)*exp(-x(1)^2x(2)^2)+(1-2*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) -4*x(2)*x(1)*exp(-x(1)^2-x(2)^2)+(12*x(2)^2)*x(1)*2*x(2)*exp(-x(1)^2-x(2)^2)])
??? Error using ==> chol
Matrix must be positive definite.
ans =
0.8578 -0.0000
0 0.8578
Matrix is positive definite Therefore critical point(0.7,-0.7) is a local minimum
% newton_min
tol=0.0000001; total=100; k=0; epsilon =1;
x=[0.8;0.8]; % initial guess
while ((epsilon > tol) & (k < total)) %Newton method
nabla=[(1-2*x(1)^2)*x(2)*exp(-x(1)^2-x(2)^2);(1-2*x(2)^2)*x(1)*exp(x(1)^2-x(2)^2)];
Hessian=[(-4*x(1)*x(2))*exp(-x(1)^2-x(2)^2)+(1-2*x(1)^2)*x(2)*(2*x(1))*exp(-x(1)^2-x(2)^2) (1-2*x(1)^2)*exp(-x(1)^2-x(2)^2)+(12*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) ; (1-2*x(1)^2)*exp(-x(1)^2x(2)^2)+(1-2*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) -4*x(2)*x(1)*exp(x(1)^2-x(2)^2)+(1-2*x(2)^2)*x(1)*2*x(2)*exp(-x(1)^2-x(2)^2)];
xnew= x -Hessian\nabla; epsilon=norm(xnew-x);
x=xnew; k=k+1;
end
fprintf('Critical point x =%12.10f , y=%12.10f found after %2.0f iterations
\n' , ...
x(1),x(2),k);
newton_min
Critical point x =0.7071067812 , y=0.7071067812 found after 5 iterations
>> chol([(-4*x(1)*x(2))*exp(-x(1)^2-x(2)^2)+(1-2*x(1)^2)*x(2)*(-2*x(1))*exp(-x(1)^2-x(2)^2) (12*x(1)^2)*exp(-x(1)^2-x(2)^2)+(1-2*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) ; (1-2*x(1)^2)*exp(-x(1)^2x(2)^2)+(1-2*x(1)^2)*x(2)*2*x(2)*exp(-x(1)^2-x(2)^2) -4*x(2)*x(1)*exp(-x(1)^2-x(2)^2)+(12*x(2)^2)*x(1)*2*x(2)*exp(-x(1)^2-x(2)^2)])
??? Error using ==> chol
Matrix must be positive definite.
ans =
0.8578 0.0000
0 0.8578
Matrix in negative definite therefore(0.7,0.7) is a local max.