Why do I receive the error in horzcat?

1 view (last 30 days)
Tilemachos Marmaras
Tilemachos Marmaras on 25 Nov 2022
Edited: Torsten on 25 Nov 2022
I'm supposed to solve a convex optimization problem, where I take a specific Newton step and later I use a terminating condition which gives me the solution. However, I receive the horzcat error in the Newton step, even though I believe it is completely correct and it follows exactly the theory. Here's the code:
A = 0.1;
b = 0.5;
x0 = 1;
tol = 1e-3;
[x, xx] = Optimization_Lab1(A, b, x0, tol)
Error using horzcat
Dimensions of arrays being concatenated are not consistent.

Error in solution>Optimization_Lab1 (line 39)
H = [blkdiag(ddf(x), zeros(n)) A' ; A zeros(m)];
function [x, xx] = Optimization_Lab1(A, b, x0, tol)
% [x, xx] = opt(A, b, x0, tol)
%
% Solves
% min f(x) = sum(x log(x))
% s.t. Ax = b
% with Newton method.
%
% Input:
% x0 : feasible startingpoint
% tol : tolerance
%
% Output:
% x : answer
% xx : solution sequence
%
% Initialize constants
ALPHA = 0.1;
BETA = 0.5;
MAXITER = 100;
% Initialize variables
[m, n] = size(A);
xx = zeros(n,MAXITER);
% Define function, gradient and Hessian
f = @(x) sum(x.*log(x));
df = @(x) log(x)+1;
ddf = @(x) diag(1./(x));
% Main loop
x = x0;
for outer = 1:MAXITER
% Find search direction
g = -df(x);
H = [blkdiag(ddf(x), zeros(n)) A' ; A zeros(m)];
dxv = H\[g; zeros(n,1); zeros(m,1)];
dx = dxv(1:m);
xx(:,outer) = x;
% Check terminating condition
l2 = g'*dx;
if (l2/2 <= tol)
xx = xx(:,1:outer);
x = xx(:,outer);
return;
end
% Calculate steplength
t = 1;
% Check to see that we are in the domain of f
while(min((x+t*dx)) <= 0); t = t * BETA; end
% Calculate steplength
while(f(x+t*dx) > f(x) + t*ALPHA*df(x)'*dx); t = t*BETA; end
% Take step
x = x + t*dx;
end
end
Input arguments: A=0.1 B=0.5 x0=1 tol=100
The error shows up in the second line under the comment "Search direction".

Answers (2)

Torsten
Torsten on 25 Nov 2022
Edited: Torsten on 25 Nov 2022
blkdiag(ddf(x), zeros(n))
is 2x2,
A'
is 1x1.
Thus it's not possible to concatenate the two horizontally in the command
H = [blkdiag(ddf(x), zeros(n)) A' ; A zeros(m)];
I don't know what the intention of the author of the code was - so I can't help you on how to modify this line.
It will be best to contact the author directly.

Image Analyst
Image Analyst on 25 Nov 2022
This happens when you're trying to stitch together, side-by-side, arrays that don't have the same number of rows. For example you can stitch together a 3x2 array with a 3x7 array because they both have 3 rows, but you can't stitch an array with 100 rows next to an array with only 3 rows. You'd have different number of rows in each column and that's not allowed because arrays must be fully rectangular with no "ragged" edges. All elements must be filled with something.
% This will work
m1 = rand(3,2);
m2 = rand(3,7);
m = [m1, m2]
m = 3×9
0.3880 0.2764 0.6785 0.2982 0.6680 0.6617 0.0910 0.0217 0.6808 0.3682 0.4101 0.2743 0.2297 0.1835 0.3036 0.7122 0.3195 0.5432 0.2983 0.6628 0.6712 0.0703 0.2836 0.4501 0.0758 0.1870 0.6469
% This won't work.
m1 = rand(3,7);
m2 = rand(100,7);
m = [m1, m2]
Error using horzcat
Dimensions of arrays being concatenated are not consistent.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!