├── CS17S016_CVX.pdf ├── CS17S016_Q0.m ├── CS17S016_Q1.m ├── CS17S016_Q2.m ├── CS17S016_Q3.m ├── CS17S016_Q4.m ├── CS17S016_Q5.m ├── Plots ├── Plot_Q1.jpg ├── SVM.jpg ├── speed.jpg └── trade-off.jpg └── README.md /CS17S016_CVX.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niravnb/Convex-optimization-problems-using-CVX/b22e9526337f3b4c3be6b05a248e4ea1a52ba33f/CS17S016_CVX.pdf -------------------------------------------------------------------------------- /CS17S016_Q0.m: -------------------------------------------------------------------------------- 1 | cvx_begin 2 | variables x y u v z 3 | 4 | % a) 5 | x+2*y==0; 6 | x-y==0; 7 | 8 | % b) 9 | square_pos( square( x + y ) ) <= x - y 10 | % OR 11 | variable t 12 | square( x+y ) <= t; 13 | square( t ) <= x - y 14 | % OR 15 | ( x + y )^4 <= x - y; 16 | 17 | % c) 18 | 19 | inv_pos(x) + inv_pos(y) <= 1; 20 | 21 | % d) 22 | norm( [ u ; v ] ) <= 3*x + y; 23 | max( x , 1 ) <= u; 24 | max( y , 2 ) <= v; 25 | 26 | % e) 27 | x >= inv_pos(y); 28 | x >= 0; 29 | y >= 0; 30 | 31 | % OR 32 | geomean([x,y])>=1 33 | % OR 34 | [ x 1; 1 y ] == semidefinite(2) 35 | 36 | % f) 37 | 38 | quad_over_lin(x + y , sqrt(y)) <= x - y + 5; 39 | 40 | % g) 41 | 42 | pow_pos(x,3) + pow_pos(y,3) <= 1; 43 | x>=0; 44 | y>=0; 45 | 46 | % h) 47 | x+z <= 1+geo_mean([x-quad_over_lin(z,y),y]); 48 | x>=0; 49 | y>=0; 50 | 51 | cvx_end -------------------------------------------------------------------------------- /CS17S016_Q1.m: -------------------------------------------------------------------------------- 1 | clc; clear all; close all; 2 | %% Initialization 3 | n = 100; 4 | network = rand(n,2); % Generating a network with 100 nodes 5 | l = 0; % lower bound 6 | 7 | cost = zeros(n,n); 8 | for i = 1:n 9 | for j=1:n 10 | cost(i,j) = norm(network(i) - network(j))/sqrt(2); % Computing cost matrix 11 | end 12 | end 13 | 14 | b = zeros(n,1); % external supply 15 | b(1:n) = 200; 16 | b(n/2 + 1:end) = -200; 17 | 18 | %% Solving using CVX for different values of u_ij 19 | 20 | U_ranges = [5,10,30,50,100]; % ranges of u_ij upper bound 21 | cost_function_trend = zeros(length(U_ranges),1); 22 | itr = 1; 23 | 24 | for u = U_ranges 25 | cvx_begin 26 | variables x(n,n) 27 | minimize(sum(sum(cost.*x))); % Minimizing total cost across the network 28 | 29 | for i = 1:n 30 | b(i) + sum(x(:,i)) - sum(x(i,:)) == 0; % conservation of flow 31 | end 32 | 33 | for i = 1:n 34 | for j = 1:n 35 | x(i,j) >= l; % lower bound 36 | x(i,j) <= u; % upper bound 37 | end 38 | end 39 | 40 | 41 | % Other method to specify constraints 42 | % b + sum(x,1)' - sum(x,2) == zeros(n,1); % conservation of flow 43 | % 44 | % x(:)>=l*ones(n*n,1); % lower bound 45 | % x(:)<=u*ones(n*n,1); % upper bound 46 | 47 | cvx_end 48 | 49 | cost_function_trend(itr) = cvx_optval; 50 | itr = itr+1; 51 | end 52 | 53 | %% Plotting Total Cost vs Upper Bound u_ij 54 | 55 | plot(U_ranges,cost_function_trend,'LineWidth',2); 56 | title('Total Cost vs Upper Bound u_{ij}'); 57 | xlabel('Upper Bound u_{ij}'); 58 | ylabel('Total Cost'); 59 | print('-djpeg','Plot_Q1.jpg', '-r300'); % Saving image 60 | close all; -------------------------------------------------------------------------------- /CS17S016_Q2.m: -------------------------------------------------------------------------------- 1 | clc; clear all; 2 | 3 | %% Initializing A's 4 | 5 | A0 = [0.16, 0.43, 0.36, 0.37; 0.43, 1.60, 1.07, 0.84; 0.36, 1.07, 0.87, 0.65; 0.37, 0.84, 0.65, 1.19]; 6 | A1 = [1.88, 1.36, 0.57,1.84; 1.36, 1.26, 0.39, 1.23; 0.57, 0.39, 0.82, 1.14; 1.84, 1.23, 1.14, 2.43]; 7 | A2 = [1.38,1.24, 1.10,1.17; 1.24, 1.49, 1.22, 1.20; 1.10, 1.22, 1.38, 1.17; 1.17, 1.20, 1.17, 1.15]; 8 | 9 | %% a) Minimize the maximum eigenvalue lambda_1(x) 10 | 11 | fprintf('Solving Part (a): \n'); 12 | 13 | cvx_begin sdp 14 | variables x(2) t 15 | minimize(t); % Minimizing objective 16 | A0 + x(1)*A1 + x(2)*A2 <= t*eye(4); % constraints 17 | cvx_end 18 | 19 | %% b) Minimize the spread of the eigenvalues, lambda_1(x) - lambda_m(x) 20 | 21 | fprintf('Solving Part (b): \n'); 22 | 23 | cvx_begin sdp 24 | variables x(2) t1 t2 25 | minimize(t1 - t2); % Minimizing objective 26 | A0 + x(1)*A1 + x(2)*A2 <= t1*eye(4); 27 | A0 + x(1)*A1 + x(2)*A2 >= t2*eye(4); 28 | cvx_end 29 | 30 | %% c)Minimize the condition number of A(x), subject to A(x) > 0. 31 | 32 | 33 | fprintf('Solving Part (c): \n'); 34 | 35 | cvx_begin sdp 36 | variables y(2) t s 37 | minimize(t); % Minimizing objective 38 | s*A0 + y(1)*A1 + y(2)*A2 <= t*eye(4); 39 | s*A0 + y(1)*A1 + y(2)*A2 >= eye(4); 40 | s>=0; 41 | cvx_end 42 | 43 | %% d) Minimize the sum of the absolute values of the eigenvalues. 44 | 45 | fprintf('Solving Part (d): \n'); 46 | 47 | cvx_begin sdp 48 | variables x(2) 49 | variable A_plus(4,4) symmetric 50 | variable A_minus(4,4) symmetric 51 | minimize(trace(A_plus) + trace(A_minus)); % Minimizing objective 52 | A0 + x(1)*A1 + x(2)*A2 == A_plus - A_minus; 53 | A_plus >= 0; 54 | A_minus >= 0; 55 | cvx_end -------------------------------------------------------------------------------- /CS17S016_Q3.m: -------------------------------------------------------------------------------- 1 | clc; clear all; 2 | %% Initializing A & B 3 | A = [-0.94, 1.19; -1.67,1.19; 0.13,-0.04; 0.29,0.33; -1.15,0.18]; 4 | B = [-0.19;0.72;-0.59;2.18;-0.14]; 5 | m = length(A); 6 | %% a) Using norm. 7 | 8 | fprintf('Solving Part (a): \n'); 9 | 10 | cvx_begin 11 | variables x(2,1) 12 | minimize(norm(A*x-B,3/2)); % Minimizing objective 13 | cvx_end 14 | 15 | 16 | 17 | %% b) Formulate this problem as an SDP. 18 | 19 | fprintf('Solving Part (b): \n'); 20 | 21 | cvx_begin sdp 22 | variables x(2) t(m,1) u(m,1) s(m,1) 23 | minimize(sum(t)); % Minimizing objective 24 | 25 | A*x - B <= s; % LMI Constraints 26 | A*x - B >= -s; 27 | 28 | 29 | for i = 1:m 30 | [u(i) s(i); s(i) t(i)] >= 0; % PSD Constraints 31 | u(i) <= sqrt(s(i)); 32 | end 33 | 34 | cvx_end -------------------------------------------------------------------------------- /CS17S016_Q4.m: -------------------------------------------------------------------------------- 1 | clc; clear all; close all; 2 | %% Loading data from file 3 | veh_speed_sched_data 4 | 5 | %% Sloving using CVX 6 | cvx_begin 7 | variable t(n) % Transit times of the segments (doing change of variables) 8 | minimize(sum(a*d.^2.*inv_pos(t)+b*d+c*t)) % Minimize transformed objective 9 | t<=d./smin; 10 | t>=d./smax; 11 | tau_min<=cumsum(t); 12 | tau_max>=cumsum(t); 13 | cvx_end 14 | 15 | %% Ploting graph of Optimal Speed vs Segment 16 | s=d./t; % optimal speed 17 | stairs(s,'LineWidth',2); % Using stairs to show constant speed over the segments 18 | title('Optimal Speed vs Segment'); 19 | xlabel('Segment i'); 20 | ylabel('Optimal Speed s_i'); 21 | print('-djpeg','speed.jpg', '-r300'); % Saving image 22 | close all; 23 | -------------------------------------------------------------------------------- /CS17S016_Q5.m: -------------------------------------------------------------------------------- 1 | clc; clear all; close all; 2 | 3 | %% Loading data from xlsx file 4 | 5 | data_C1 = xlsread('Q5Data_Classification', 'Data1'); % Class 1 data 6 | data_C2 = xlsread('Q5Data_Classification', 'Data2'); % Class 2 data 7 | 8 | n = 2; % Dimension 9 | N = length(data_C1); % Class 1: Number of data points 10 | M = length(data_C2); % Class 2: Number of data points 11 | 12 | %% Setting range of gamma's from 0 to 2 with step size of 0.05 13 | g = [0:0.05:2]; 14 | 15 | F1 = zeros(length(g),1); % for storing norm(a) for all values of gamma 16 | F2 = zeros(length(g),1); % for storing 1'*u + 1'*v for all values of gamma 17 | optimal_values = zeros(length(g),1); % for storing optimal values for all values of gamma 18 | 19 | %% Solution via CVX 20 | for l = 1:length(g) % Running for all values of gamma 21 | cvx_begin 22 | variables a(n) b(1) u(N) v(M) 23 | minimize (norm(a) + g(l)*(ones(1,N)*u + ones(1,M)*v)) % Minimizing |a||_2 + gamma*(1'*u + 1'*v) 24 | data_C1*a - b >= 1 - u; 25 | data_C2*a - b <= -(1 - v); 26 | u >= 0; 27 | v >= 0; 28 | cvx_end 29 | F1(l) = norm(a); % Storing norm(a) for current gamma 30 | F2(l) = (ones(1,N)*u + ones(1,M)*v); % storing 1'*u + 1'*v for current gamma 31 | optimal_values(l) = cvx_optval; % storing optimal value for current gamma 32 | end 33 | 34 | [sorted,ind] = sort(optimal_values); 35 | 36 | %% Plotting optimal trade-off curve (pareto frontier) 37 | figure 38 | plot(F1,F2,'LineWidth',2); % Plotting norm(a) vs. 1'*u + 1'*v 39 | title('Optimal Trade-off Curve (pareto frontier)'); 40 | xlabel('F1: Norm(a)'); 41 | ylabel('F2: 1*u + 1*v'); 42 | hold on; 43 | scatter(F1(3),F2(3),'r','filled'); % highlighting chosen optimal trade-off point 44 | str = ['Chosen optimal trade-off point at F1 = ',num2str(F1(3)),' F2 = ',num2str(F2(3)),' for \gamma = 0.1']; 45 | dim = [0.2 0.4 0.4 0.4]; 46 | annotation('textbox',dim,'String',str,'FitBoxToText','on'); % Placing text box on plot 47 | print('-djpeg','trade-off.jpg', '-r300'); % Saving image 48 | close all; 49 | 50 | %% Choosing g = 0.1 as optimal trade-off by observing from the above plot and finding optimal variables 51 | g = 0.1; 52 | cvx_begin 53 | variables a(n) b(1) u(N) v(M) 54 | minimize (norm(a) + g*(ones(1,N)*u + ones(1,M)*v)) 55 | data_C1*a - b >= 1 - u; 56 | data_C2*a - b <= -(1 - v); 57 | u >= 0; 58 | v >= 0; 59 | cvx_end 60 | 61 | %% Displaying results 62 | t_min = min([data_C1(:,1);data_C2(:,1)]); % Finding min of x-axis 63 | t_max = max([data_C1(:,1);data_C2(:,1)]); % Finding max of x-axis 64 | tt = linspace(t_min-1,t_max+1,100); 65 | p = -a(1)*tt/a(2) + b/a(2); % Finding linear discriminator 66 | p1 = -a(1)*tt/a(2) + (b+1)/a(2); % Finding linear slab for class 1 67 | p2 = -a(1)*tt/a(2) + (b-1)/a(2); % Finding linear slab for class 2 68 | 69 | figure 70 | scatter(data_C1(:,1),data_C1(:,2), 'b', 'filled'); % Plotting class 1 data 71 | hold on; 72 | scatter(data_C2(:,1), data_C2(:,2), 'r','filled'); % Plotting class 2 data 73 | 74 | plot(tt,p, '-k', tt,p1, '--k', tt,p2, '--k','LineWidth',2); % Plotting linear discriminator 75 | title('Support Vector Classifier for \gamma = 0.1'); 76 | xlabel('X'); 77 | ylabel('Y'); 78 | print('-djpeg','SVM.jpg', '-r300'); % Saving image 79 | close all; 80 | -------------------------------------------------------------------------------- /Plots/Plot_Q1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niravnb/Convex-optimization-problems-using-CVX/b22e9526337f3b4c3be6b05a248e4ea1a52ba33f/Plots/Plot_Q1.jpg -------------------------------------------------------------------------------- /Plots/SVM.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niravnb/Convex-optimization-problems-using-CVX/b22e9526337f3b4c3be6b05a248e4ea1a52ba33f/Plots/SVM.jpg -------------------------------------------------------------------------------- /Plots/speed.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niravnb/Convex-optimization-problems-using-CVX/b22e9526337f3b4c3be6b05a248e4ea1a52ba33f/Plots/speed.jpg -------------------------------------------------------------------------------- /Plots/trade-off.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niravnb/Convex-optimization-problems-using-CVX/b22e9526337f3b4c3be6b05a248e4ea1a52ba33f/Plots/trade-off.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Convex-optimization-problems-using-CVX 2 | Solving Convex optimization problems using CVX on Matlab 3 | --------------------------------------------------------------------------------