Why do I receive the error in horzcat?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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)
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".
0 Kommentare
Antworten (2)
Torsten
am 25 Nov. 2022
Bearbeitet: Torsten
am 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
am 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]
% This won't work.
m1 = rand(3,7);
m2 = rand(100,7);
m = [m1, m2]
0 Kommentare
Siehe auch
Kategorien
Mehr zu Linear Programming and Mixed-Integer Linear Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!