Classnotes - Coursework of Optimization
Classnotes - Coursework of Optimization
02311940000102
Optimization C
Hudzaifa Dhiaul Ahnaf
02311940000102
Optimization C
Hudzaifa Dhiaul Ahnaf
02311940000102
Optimization C
Hudzaifa Dhiaul Ahnaf
02311940000102
Optimization C
Hudzaifa Dhiaul Ahnaf
02311940000102
Optimization C
Hudzaifa Dhiaul Ahnaf
02311940000102
Optimization C
Problem A3
Univariate
Powells
Problem A4
Univariate
Second Function
%%
clear all;
clc ;
%% Level Sets
[X,Y]=meshgrid(-10:5);
z=f(X,Y);
contour(X,Y,z,[1:6])
%% First State
syms x1 x2 xl
eps = 0.01;
i = 1;
n = 2;
S = eye(2);
err = 1 ;
%%
x = [0;0];
% f = symfun(2*x1^2 + x2^2 + 2*x1*x2 - 6*x1 - 4*x2 , [x1 , x2])
f = symfun(3*x1^2 + x2^2 - 2*x1*x2 - 10*x1 + 2*x2 , [x1 , x2])
%% Iteration
while err > 0
x_temp = x(:,i);
if mod(i,2) == 1 % Odd
s = S(:,1)
else
s = S(:,2) % even
end
fval = f(x_temp(1),x_temp(2))
xp_temp = x_temp + eps*s
xn_temp = x_temp - eps*s
fval_p = f(xp_temp(1),xp_temp(2))
fval_n = double(f(xn_temp(1),xn_temp(2)))
grad_fopt = gradient(fval_opt)
lambda_opt = solve(grad_fopt)
i = i+1
err = abs(((f(x(1,i),x(2,i)))-(f(x(1,i-1),x(2,i-1))))/...
(f(x(1,i-1),x(2,i-1)))) %determine the convergence criterion
end
axis square
x1 = linspace(-2.5,2.5);
x2 = linspace(-2.5,2.5);
[A,B] = meshgrid(x1,x2);
f_fig = f(A,B);
levels = 10:10:350;
figure(1), contour(x1,x2,f_fig,levels,...
'LineWidth',1.2), colorbar
hold on;
plot(x(1,:),x(2,:))
With unicariate, the first equation will be solved within the 15th iteration, final X is
[1.0078 ; 0.9922]
With univariate, the second equation will be solved within 11st iteration, will get final solution of X :
[1.9959; 0.9959]
Powell’s
%%
clear all;
clc ;
%% First State
syms x1 x2 xl
eps = 0.1;
i = 1;
n = 2;
S = [1 0 0 0 0 0; 0 1 0 0 0 0]
err = 1 ;
m = 1
%% Obj fun
x = [0;0];
f = symfun(2*x1^2 + x2^2 + 2*x1*x2 - 6*x1 - 4*x2 , [x1 , x2])
% f = symfun(3*x1^2 + x2^2 - 2*x1*x2 - 10*x1 + 2*x2 , [x1 , x2])
Hudzaifa Dhiaul Ahnaf
02311940000102
Optimization C
%% Iteration
while err > 0.001
x_temp = x(:,i);
if i == 1
s = S(:,2)
elseif mod(i,3) == 2
s = S(:,(i-(2*m-1)))
elseif mod(i,3) == 0
s = S(:,i-(2*m-1))
elseif mod(i,3) == 1
s = x(:,i) - S(:,(i-(2*m)));
S(:,i-1) = s
m = m + 1;
end
fval = double(f(x_temp(1),x_temp(2)))
xp_temp = x_temp + eps*s
xn_temp = x_temp - eps*s
fval_p = double(f(xp_temp(1),xp_temp(2)))
fval_n = double(f(xn_temp(1),xn_temp(2)))
axis square
x1 = linspace(-50,50);
x2 = linspace(-50,50);
[A,B] = meshgrid(x1,x2);
f_fig = f(A,B);
levels = 10:10:350;
asd = figure(1), contour(x1,x2,f_fig,levels,...
'LineWidth',1.2), colorbar
hold on;
plot(x(1,:),x(2,:))
With powell’s the first equation solved within 9th iteration, and resulting in x = [0. 7097 ; 1. 4354]
And second equation solved within 8th iteration, resulting in x = [1. 9848 ; 1.0042]
Problem A5
When we use different starting point, the start line will change differently depending on the how far the
optimized point is, when we change the epsilon or the step, increasing to 0.000 … 01 wil get the
optimized value more time because the iteration will increasing or decreasing slowly. Or reducing the
epsilon to 1 will get the result more fast.
Problem B1
Hudzaifa Dhiaul Ahnaf
02311940000102
Optimization C
Hudzaifa Dhiaul Ahnaf
02311940000102
Optimization C
Problem B2
Cauchy
Problem B3
%%
clear all;
clc ;
%% First State
syms x y lambda
eps = 0.01;
n = 2;
err = 1 ;
G = [0 ; 0];
i = 1
%%
f = symfun( 100*y^2 + 100*x^4 - 200*y*x^2 + x^2 - 2*x + 1 , [x y]);
X0 = [-0.75;1];
%% b) Direction S
while err > 0.01
gradxx = gradient(f,x) %diff the f by x
gradyy = gradient(f,y)
S = -G %assign G to S by multiply by -
Hudzaifa Dhiaul Ahnaf
02311940000102
Optimization C
if i > 2
err = double(abs((f(X0(1,i),X0(2,i))-f(X0(1,i-1),X0(2,i-1)))/(f(X0(1,i-
1),X0(2,i-1)))))
%determine the convergence criterion
end
i = i+1
end
%%
axis square
x = linspace(-2.5,2.5);
y = linspace(-2.5,5);
[A,B] = meshgrid(x,y);
f_fig = f(A,B);
levels = 10:10:350;
figure(1), contour(x,y,f_fig,levels,...
'LineWidth',1.2), colorbar
hold on;
plot(X0(1,:),X0(2,:))