Skip to content
Snippets Groups Projects
Commit d6c403ce authored by manxilin's avatar manxilin
Browse files

dd

parent d4923ec6
No related branches found
No related tags found
No related merge requests found
f = @(x)(x-log(x));
df = @(x) (1- 1./x);
d2f = @(x) 1./(x.^2);
x=0.01:0.01:2;
y=f(x);
dy= df(x);
d2y=d2f(x);
figure,
subplot(1,3,1), plot(x,y,'b-',1,1,'ro'), title('f(x)'),grid on,
subplot(1,3,2), plot(x,dy), title('1st-order derivative'),grid on,
subplot(1,3,3), semilogy(x,d2y), title('2nd-order derivative'),grid on,
disp('The minimizer is x=1, which can be obtained by solving f''(x)=0.')
disp('The minimizer is unique, since d2f>0, i.e., f strictly convex.')
\ No newline at end of file
disp('The gradient is [8+2x1; 12-4x2], and the Hessian is [2, 0; 0, -4].')
disp('Set the gradient equals 0, we obtain only one stationary point [-4;3].')
disp('Since Hessian is neither positive definite nore negative definite, the stationary point is just a saddle point.')
x1=-9:0.05:1;
x2=-2:0.05:8;
[X,Y]=meshgrid(x1,x2);
F=8*X+12*Y+X.^2-2*Y.^2;
figure,
v=[-20:20];
[c,h]=contour(X,Y,F,v,'linewidth',2);
colorbar, axis image,
xlabel('x_1','fontsize',14),
ylabel('x_2','fontsize',14),
\ No newline at end of file
02610/week1/Solutions/Exe2.jpg

1.62 MiB

02610/week1/Solutions/Exe3.jpg

1.54 MiB

02610/week1/Solutions/Exe4.jpg

1.5 MiB

clear all, close all,
%% f1
f1 = @(x)(x.^2+x+1);
df1 = @(x) (2*x+1);
d2f1 = @(x) 2*ones(length(x));
x=-2:0.01:2;
y1=f1(x);
dy1= df1(x);
d2y1=d2f1(x);
figure,
subplot(1,3,1), plot(x,y1,'b-'), title('f(x)'),grid on,
subplot(1,3,2), plot(x,dy1), title('1st-order derivative'),grid on,
subplot(1,3,3), semilogy(x,d2y1), title('2nd-order derivative'),grid on,
%% f2
f2 = @(x)(-x.^2+x+1);
df2 = @(x) (-2*x+1);
d2f2 = @(x) -2*ones(length(x));
x=-2:0.01:2;
y2=f2(x);
dy2= df2(x);
d2y2=d2f2(x);
figure,
subplot(1,3,1), plot(x,y2,'b-'), title('f(x)'),grid on,
subplot(1,3,2), plot(x,dy2), title('1st-order derivative'),grid on,
subplot(1,3,3), semilogy(x,d2y2), title('2nd-order derivative'),grid on,
%% f3
f3 = @(x)(x.^3-5*x.^2+x+1);
df3 = @(x) (3*x.^2-10*x+1);
d2f3 = @(x) 6*x-10;
x=-4:0.01:4;
y3=f3(x);
dy3= df3(x);
d2y3=d2f3(x);
figure,
subplot(1,3,1), plot(x,y3,'b-'), title('f(x)'),grid on,
subplot(1,3,2), plot(x,dy3), title('1st-order derivative'),grid on,
subplot(1,3,3), semilogy(x,d2y3), title('2nd-order derivative'),grid on,
%% f4
f4 = @(x)(x.^4+x.^3-10*x.^2-x+1);
df4 = @(x) (4*x.^3+3*x.^2-10*x-1);
d2f4 = @(x) 12*x.^2+6*x-10;
x=-4:0.01:4;
y4=f4(x);
dy4= df4(x);
d2y4=d2f4(x);
figure,
subplot(1,3,1), plot(x,y4,'b-'), title('f(x)'),grid on,
subplot(1,3,2), plot(x,dy4), title('1st-order derivative'),grid on,
subplot(1,3,3), semilogy(x,d2y4), title('2nd-order derivative'),grid on,
disp('f1 is convex.')
disp('At local minimizer, the value of the 2nd derivative is positive.')
dips('At local maximizer, the value of the 2nd derivative is negative.')
x1=0:0.05:5;
x2=x1;
[X,Y]=meshgrid(x1,x2);
F=X.^2+Y.^2;
figure,
v=[-20:20];
[c,h]=contour(X,Y,F,v,'linewidth',2);
colorbar, axis image,
xlabel('x_1','fontsize',14),
ylabel('x_2','fontsize',14),
disp('The feasible set is a convext set.')
%%
f = @(x)(x.*log(x));
df = @(x)(log(x)+1);
d2f = @(x)(1./x);
x=0.01:0.01:5;
y=f(x);
dy= df(x);
d2y=d2f(x);
figure,
subplot(1,3,1), plot(x,y,'b-',1,1), title('f(x)'),grid on,
subplot(1,3,2), plot(x,dy), title('1st-order derivative'),grid on,
subplot(1,3,3), semilogy(x,d2y), title('2nd-order derivative'),grid on,
disp('The objective function is a convex function in the feasible set.')
\ No newline at end of file
...@@ -17,13 +17,13 @@ plot(x,y); ...@@ -17,13 +17,13 @@ plot(x,y);
myFuncd = @(x) 1-x.^-1; myFuncd = @(x) 1-x.^-1;
myFuncdd = @(x) x.^-2; myFuncdd = @(x) x.^-2;
figure; figure;
subplot(3,1,1); subplot(1,3,1);
plot(x,y); plot(x,y);
title('function'); title('function');
subplot(3,1,2); subplot(1,3,2);
plot(x,myFuncd(x)); plot(x,myFuncd(x));
title('1st-order derivatons'); title('1st-order derivatons');
subplot(3,1,3); subplot(1,3,3);
plot(x,myFuncdd(x)); plot(x,myFuncdd(x));
title('2nd-order derivations'); title('2nd-order derivations');
%% 1.1 is the function convex? %% 1.1 is the function convex?
...@@ -57,7 +57,7 @@ figure; ...@@ -57,7 +57,7 @@ figure;
subplot(1,2,1); subplot(1,2,1);
mesh(X,Y,y); mesh(X,Y,y);
subplot(1,2,2); subplot(1,2,2);
v = [0:2:10 10:5:100 100:20:200]; v = [-20:20];
[c,h] = contour(x1,x2,y,v,'linewidth',2); [c,h] = contour(x1,x2,y,v,'linewidth',2);
color bar, axis image, color bar, axis image,
xlabel('x1'); xlabel('x1');
......
...@@ -9,8 +9,8 @@ clear all; close all; clc; ...@@ -9,8 +9,8 @@ clear all; close all; clc;
% a和b取何值令f(x)有且只有一个最优解? % a和b取何值令f(x)有且只有一个最优解?
% x1 = x2 = (2-a)/(9+(1+a)^2) % x1 = x2 = (2-a)/(9+(1+a)^2)
% eigen value is (2-a) or (4+a) % eigen value is (2-a) or (4+a)
% -4<=a<=2 % my answer: -4<=a<=2
% solution: a neq 2 or a neq -4
%% %%
% f(x) = 1/2*x^T*Q*x-b^T*x % f(x) = 1/2*x^T*Q*x-b^T*x
% dx = Q*x-b^T = 0 % dx = Q*x-b^T = 0
...@@ -19,6 +19,7 @@ clear all; close all; clc; ...@@ -19,6 +19,7 @@ clear all; close all; clc;
%% %%
% min(b^T*x) % min(b^T*x)
% df(x) = b inequal 0 % df(x) = b inequal 0
% doesn't meet the optimization necessary condition % my answer: doesn't meet the optimization necessary condition
% 这就是一个一次函数,minimizer就是和\Omega的交点
%% %%
...@@ -176,5 +176,5 @@ title('2-nd order derivation'); ...@@ -176,5 +176,5 @@ title('2-nd order derivation');
% the equal constrainit function is linear % the equal constrainit function is linear
% the inequal constraint function is concave % the inequal constraint function is concave
% f(x) = x*log(x) is convex (negative entrophy) % f(x) = x*log(x) is convex (negative entrophy)
% x is convex, thus this is not a convex problem % x is convex, thus this is a convex problem
function y = myFun(x) function y = myFun(x)
assert(size(x)<=1);
assert(min(x)>0);
y = x - log(x); y = x - log(x);
end end
\ No newline at end of file
02805 @ e9ec7e44
Subproject commit e9ec7e44cb6c0a4e94165cd5b2215343be419edb
socialgraphs2020 @ d9b2c8cc
Subproject commit d9b2c8cc557174e783b53bf8133bd185a888c801
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment