You are on page 1of 9

1)

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)

epsilon = 1; tol = 0.000001; Imax = 100; k = 1;


x(1)= 1;
while ((epsilon > tol) & (k < Imax))
x(k+1) = cos(x(k));
epsilon = abs(x(k+1)-x(k));
k = k+1;
end
fprintf('Root x = %12.10f is found after %2.0f iterations \n',x(k),k-1)

>> Root x = 0.7390855264 is found after 34 iterations


ii) labs = fzero('x - cos(x)', 1);
epsilon = 1; tol = 0.000001; total = 100; k = 1; x(1) = 1;
Error(1) = abs(x(1) - labs);
while ((epsilon > tol) && (k < total))
f = x(k) - cos(x(k));
fDer = 1 + sin(x(k));
x(k+1) = x(k) - f/fDer;
epsilon = abs(x(k+1) -x(k));
Error(k+1) = abs(x(k+1) - labs);
k = k+1;
end
fprintf('Root x = %12.10f found after %2.0f iterations\n', x(k), k-1)

>> Root x = 0.7390851332 found after

4 iterations

3 b) Newton Raphson Method


tol = 0.0000001; total = 100; k = 0; epsilon = 1;
x=[1;1];
while ((epsilon > tol) & (k < total))
F = [x(1)^3+2*x(1)*x(2)+2*x(2)^2-1;3*x(1)^2+x(2)];
DF = [3*x(1)^2+2*x(2) 2*x(1)+4*x(2);6*x(1) 1];
xnew = x - DF\F; epsilon = norm(xnew-x);
x=xnew; k = k+1;
fprintf('x = %12.10f, y=%12.10f, k= %2.0f \n',x(1),x(2),k);
end
x = 0.3548387097, y=0.8709677419, k= 1
x = -0.2409617764, y=0.8907469247, k= 2
x = 0.2484984091, y=0.5334594417, k= 3
x = -0.5158832519, y=0.9544313824, k= 4
x = -0.0584070418, y=0.6176193010, k= 5
x = 0.8048484549, y=0.2922870515, k= 6
x = 0.2493863833, y=0.7390336344, k= 7
x = -0.5270631548, y=0.9752349484, k= 8
x = -0.0671258256, y=0.6211094111, k= 9
x = 0.7518530049, y=0.3163301515, k= 10
x = 0.2135251536, y=0.7326116531, k= 11
x = -0.7210844636, y=1.0605969986, k= 12
x = -0.2150973612, y=0.6292682189, k= 13
x = 0.2962100376, y=0.5210846092, k= 14
x = -0.3473903956, y=0.8806242919, k= 15
x = 0.1037222729, y=0.5782329894, k= 16
x = -4.5014774080, y=2.8336957381, k= 17
x = -2.5698394540, y=-8.6185489008, k= 18
x = -1.5532732079, y=-4.1377521771, k= 19
x = -0.9926349492, y=-2.0130266554, k= 20
x = -0.6707395400, y=-1.0388246285, k= 21
x = -0.4974839678, y=-0.6524184146, k= 22
x = -0.4354872156, y=-0.5574165531, k= 23
x = -0.4285390546, y=-0.5507923332, k= 24
x = -0.4284725055, y=-0.5507660506, k= 25
x = -0.4284725008, y=-0.5507660517, k= 26
c) Broyden Method
tol = 0.0000001; total = 200; k = 0; epsilon = 1;
x = [1;1];
D = [1 0;0 1];
while ((epsilon > tol) & (k < total))
F = [x(1)^3+2*x(1)*x(2)+2*x(2)^2-1;3*x(1)^2+x(2)];
xnew = x-D\F; epsilon = norm(xnew-x);

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) Golden Search Method


tol = 0.000001; k = 0;
options = optimset('TolX', tol);
[xmin,fmin] = fminbnd ('(x^2)/2 - sin(x)', -1, 3, options);
a = -1; b = 3;
s = (sqrt(5) - 1)/2;
a1 = a+ (1-s)*(b-a);
fa1 = (a1^2)/2 - sin(a1);
b1 = a+s*(b-a);
fb1 = (b1^2)/2 - sin(b1);
while (b-a > tol)
if fa1>fb1
a =a1;
a1 =b1;
fa1=fb1;
b1 = a+s*(b-a);
fb1 = (b1^2)/2 - sin(b1);
else
b=b1;
b1=a1;
fb1=fa1;
a1 = a+ (1-s)*(b-a);
fa1 = (a1^2)/2 - sin(a1);
end
x=b1; Error = abs(x-xmin);k=k+1;
end
fprintf('x = %12.10f, Error = %12.10f, k = %2.0f \n', x,Error,
k)
x = 0.7390852931, Error = 0.0000001614, k = 32
b) labs = fzero('x - cos(x)', 1);
epsilon = 1; tol = 0.000001; total = 100; k = 1; x(1) = 1;

Error(1) = abs(x(1) - labs);


while ((epsilon > tol) && (k < total))
f = x(k) - cos(x(k));
fDer = 1 + sin(x(k));
x(k+1) = x(k) - f/fDer;
epsilon = abs(x(k+1) -x(k));
Error(k+1) = abs(x(k+1) - labs);
k = k+1;
end
fprintf('Root x = %12.10f found after %2.0f iterations\n', x(k), k-1)

>> Root x = 0.7390851332 found after

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.

>> 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.

Therefore (0,0) must be a saddle point.

Second saddle point.

% 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.

>> 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)])

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.

>> 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)])

ans =

0.8578 0.0000
0 0.8578
Matrix in negative definite therefore(0.7,0.7) is a local max.

5 II) tol = 0.000001; total = 100; k = 0; epsilon =1;


x = [2;0.6];
s = [6*x(1) - x(2); 4*x(2) - x(1)];
while ((epsilon >tol) & (k < total))
tmin = fminsearch(@(t)(3*(x(1)-t*s(1))^2 + 2*(x(2)-t*s(2))^2
- (x(1)-t*s(1))*(x(2)-t*s(2))),1);
xnew = x-tmin*s;
x = xnew;
s = [6*x(1) - x(2); 4*x(2) - x(1)];
epsilon = norm(s);
k = k+1;
end
fprintf('Minimum found at x = %12.10f, y = %12.10f after %2.0f
iterations \n',...
x(1), x(2),k);
Minimum found at x = 0.0000000182, y = 0.0000001266 after 11
iterations

You might also like