You are on page 1of 9

UJIAN 2 OPTIMASI

NAMA: DIAN HASANAH

NIM : F1A118040

1. A. Metode Bagi Dua


clc
clear all
disp(' METODE BAGI DUA ')
syms x
f = input('Masukkan Persamaan = ');
a = input('Masukkan Nilai Batas Bawah = ');
b = input('Masukkan Nilai Batas Atas = ');
eps = input('Epsilon = ');
imax=input('Iterasi Maksimum = ');
fa = subs(f,x,a);
fb = subs(f,x,b);
t =(a+b)/2;
ft = subs(f,x,t);
selisih = abs(b-a);
A = a; B = b; C = (a+b)/2; D = selisih;
disp('================================================================='
)
disp('| Iterasi a b t (b-a) Epsilon
|')
disp('================================================================='
)
for k = 1:imax
t = (a+b)/2;
ft = subs(f,x,t);
disp([k-1, a, b, t, selisih])
if ((fa)*(ft)<=0)
b = t;
fb = ft;
else a = t;
fa = ft;
end
selisih = abs(b-a);
A = [A;a]; B = [B;b]; C = [C;t]; D = [D;selisih];
if (abs(b-a))<=eps
break
end
end
m = [k a b t selisih eps];
fprintf('%4i\t %10.5f\t%10.5f\t%10.5f\t%10.5f\t %5f\n', m)
disp('================================================================='
)
plot([A,B,C,D])
legend('a','b','c','selisih')
xlabel('Iterasi ke ')
ylabel('Iterasi Metode Bagi Dua')
disp(['Akar :',num2str(t)])
disp(['Jumlah Iterasi :',num2str(k)])
Hasil Run:
METODE BAGI DUA
Masukkan Persamaan =
4*x^2+1*x
Masukkan Nilai Batas Bawah =
-2
Masukkan Nilai Batas Atas =
4
Epsilon =
0.00001
Iterasi Maksimum =
10
=================================================================
| Iterasi a b t (b-a) Epsilon |
=================================================================
0 -2 4 1 6

1.0000 1.0000 4.0000 2.5000 3.0000

2.0000 2.5000 4.0000 3.2500 1.5000

3.0000 3.2500 4.0000 3.6250 0.7500

4.0000 3.6250 4.0000 3.8125 0.3750

5.0000 3.8125 4.0000 3.9062 0.1875

6.0000 3.9062 4.0000 3.9531 0.0938

7.0000 3.9531 4.0000 3.9766 0.0469

8.0000 3.9766 4.0000 3.9883 0.0234

9.0000 3.9883 4.0000 3.9941 0.0117

10 3.99414 4.00000 3.99414 0.00586 0.000010


=================================================================
Akar :3.9941
Jumlah Iterasi :10
 
B. Metode Fibonacci
format short
clear all
clc
f=@(x) 4*x^2+1*x;
L=-2;
R=4;
n=6;
Fib=ones(1,n);
for i=3:n+1
Fib (i)=Fib(i-1)+Fib(i-2);
end
for k=1:n
ratio=(Fib(n+1-k)./Fib(n+2-k));
x2=L+ratio.*(R-L);
x1=L+R-x2;
fx1=f(x1);
fx2=f(x2);
rsl(k,:)=[L R x1 x2 fx1 fx2];
if fx1<fx2
R=x2;
elseif fx1>fx2
L=x1;
elseif fx1==fx2
if min (abs(x1),abs(L))==abs (L)
R=x2;
else
L=x1;
end
end
end
Variables={'L','R','x1','x2','fx1','fx2'};
Resl=array2table(rsl);
Resl.Properties.VariableNames(1:size(Resl,2))=Variables;
xopt=(L+R)/2;
fopt=f(xopt);
fprintf('optimal value of x= %f \n', xopt);
fprintf('optimal value of f(x) = %f \n', fopt);

Hasil Run:

optimal value of x= 0.076923


optimal value of f(x) = 0.100592

2. A. Metode Koordinat Siklik

clc
clear
format long
tic;
disp(' CYCLIC COORDINATE METHODE
')
% Function Definition (Enter your Function here):
syms X Y;
f =(X-4)^2-(Y-1*Y)^2;

% Initial Guess (Choose Initial Values):


x(1) = 1;
y(1) = -7;
S = [1 0]';
I = [x(1),y(1)]';

% Tolerance and Step-size:


e = 0.00001;
i = 1;

% Convergence Parameters:
F = subs(f, [X,Y], [x(1),y(1)]);
f_plus = I + e*S;
F_plus = subs(f, [X,Y], [f_plus(1), f_plus(2)]);
f_minus = I - e*S;
F_minus = subs(f, [X,Y], [f_minus(1), f_minus(2)]);

% Search Direction:
if F_minus < F
S = -S;
else
S = S;
end

% Optimization Algorithm:
while ((double(F) > double(F_minus))||(double(F) > double(F_plus)))
syms h; % Step size
g = subs(f, [X,Y], [x(i)+S(1)*h,y(i)+h*S(2)]);
dg_dh = diff(g,h);
h = solve(dg_dh, h); % Optimal Step Size
x(i+1) = I(1)+h*S(1); % New x value
y(i+1) = I(2)+h*S(2); % New y value
i = i+1;
I = [x(i),y(i)]'; % Updated Point
if rem(i,2) == 0
S = [0 1]';
else
S = [1 0]';
end
F = subs(f, [X,Y], [x(i),y(i)]);
f_plus = I + e*S;
F_plus = subs(f, [X,Y], [f_plus(1), f_plus(2)]);
f_minus = I - e*S;
F_minus = subs(f, [X,Y], [f_minus(1), f_minus(2)]);
if double(F_minus) < double(F)
S = -S;
else
S = S;
end
end
toc
% Result Table:
Iter = 1:i;
X_coordinate = x';
Y_coordinate = y';
Iterations = Iter';
T = table(Iterations,X_coordinate,Y_coordinate);

% Output:
fprintf('Initial Objective Function Value: %d\n\n',subs(f,[X,Y],
[x(1),y(1)]));
if ((double(F)>=double(F_minus))||(double(F)>=double(F_plus)))
fprintf('Minimum succesfully obtained...\n\n');
end
fprintf('Number of Iterations for Convergence: %d\n\n', i);
fprintf('Point of Minima: [%d,%d]\n\n', x(i), y(i));
fprintf('Objective Function Minimum Value after Optimization: %f\n\n',
subs(f,[X,Y], [x(i),y(i)]));
disp(T)

Hasil Run:
CYCLIC COORDINATE METHODE
Elapsed time is 2.537411 seconds.
Initial Objective Function Value: 9
Minimum succesfully obtained...
Number of Iterations for Convergence: 2
Point of Minima: [4,-7]
Objective Function Minimum Value after Optimization: 0.000000

Iterations  X_coordinate  Y_coordinate


__________  ____________  ____________
1 1 -7
2 4 -7

B. Metode Hooke and Jeeves


clear;
clc;
close all

disp(' METODE HOOKE AND JEEVES


')
fprintf('===============================================================
===\n');
fprintf(' ITERASI x_k f(x_k)\
n');
fprintf('===============================================================
===\n');

[iters, endpt] = hooke(2,[0, 3], 0.5, 1e-6, 500)

function y = f(x)
z = @(x,y)(x-4)^2-(y-1*y)^2;
y = z(x(1), x(2));
end

function [minf, point] = beat_nearby(delta, point, prebest, nvars)


minf = prebest;
z = point;
for i = 1 : nvars
z(i) = point(i) + delta(i);
ftmp = f(z);
if ftmp < minf
minf = ftmp;
else
delta(i) = 0 - delta(i);
z(i) = point(i) + delta(i);
ftmp = f(z);
if ftmp < minf
minf = ftmp;
else
z(i) = point(i);
end
end
end
point = z;
end

function [iters, endpt] = hooke(nvars, startpt, rho, epsilon, itermax)


[newx, xbefore] = deal(startpt);
delta = abs(startpt * rho);
delta(delta == 0) = rho;
iadj = 0;
steplength = rho;
iters = 0;
fbefore = f(newx);
while iters < itermax && steplength > epsilon
iters = iters + 1;
iadj = iadj + 1;
newx = xbefore;
[newf, newx] = beat_nearby(delta, newx, fbefore, nvars);
keep = 1;
while newf < fbefore && keep == 1
iadj = 0;
for i = 1 : nvars
if newx(i) <= xbefore(i)
delta(i) = 0 - abs(delta(i));
else
delta(i) = abs(delta(i));
end
tmp = xbefore(i);
xbefore(i) = newx(i);
newx(i) = newx(i) * 2 - tmp;
end
fbefore = newf;
[newf, newx] = beat_nearby(delta, newx, fbefore, nvars);
if newf >= fbefore
break
end
keep = 0;
for i = 1 : nvars
keep = 1;
if abs(newx(i) - xbefore(i)) > 0.5 * abs(delta(i))
break
else
keep = 0;
end
end
end
if steplength >= epsilon && newf >= fbefore
steplength = steplength * rho;
delta = delta * rho;
end
fprintf('%5d [%f %8.4f] %8.4f\
n',iters,newx,fbefore);
end

fprintf('==============================================================\
n');
endpt = xbefore;
end

Hasil Run:

METODE HOOKE AND JEEVES


==================================================================
ITERASI x_k f(x_k)
==================================================================
1 [5.000000 3.0000] 0.0000
2 [4.000000 3.0000] 0.0000
3 [4.000000 3.0000] 0.0000
4 [4.000000 3.0000] 0.0000
5 [4.000000 3.0000] 0.0000
6 [4.000000 3.0000] 0.0000
7 [4.000000 3.0000] 0.0000
8 [4.000000 3.0000] 0.0000
9 [4.000000 3.0000] 0.0000
10 [4.000000 3.0000] 0.0000
11 [4.000000 3.0000] 0.0000
12 [4.000000 3.0000] 0.0000
13 [4.000000 3.0000] 0.0000
14 [4.000000 3.0000] 0.0000
15 [4.000000 3.0000] 0.0000
16 [4.000000 3.0000] 0.0000
17 [4.000000 3.0000] 0.0000
18 [4.000000 3.0000] 0.0000
19 [4.000000 3.0000] 0.0000
==============================================================

iters =

19

endpt =

4 3

C. Metode Rosenbrock
close all; clear; clc;

% Setup optimization options


options =
optimoptions(@fminunc,'Display','iter','Algorithm','quasi-newton');

% Define guess values


xy_guess = [0,0];

% Call optimization algorithm


[xy_opt,fval] = fminunc(@rosenbrock_func,xy_guess,options);

% Objective function
function f = rosenbrock_func(in)
% Unpack inputs
x = in(1);
y = in(2);
% The Rosenbrock function in 2D
f = (x - 4)^2 - (y - 1*y)^2;
end

Hasil Run:

First-order
Iteration Func-count f(x) Step-size optimality
0 3 16 8
1 6 9 0.125 6
2 9 0 1 5.96e-08

You might also like