0

I'm trying to minimize an objective function that contains absolute terms including some of the variables. The target function looks like this (I'll only write down two terms, the actual problem contains between 500 and 5000, depending on other parameters):

min |f_11 * x_1 + f_21 * x_2 - y_1| + |f_12 * x_1 + f_22 * x_2 - y_2|

There can also be different types of constraints. Since I don't have the Symbolic Toolbox I have no clue how to put this into Matlab. I thought of interpreting this as a quadratic program, where I square each term and get the squareroot of it. With an anonymous function this would look like this I think:

f = @(X) sqrt((F*X - Y) .* (F*X - Y)) * ones(size(Y));

Where F and Y contain the values of f_ij and y_j. So in my case F ís of size ix2, Y is of size ix1 and X is of size 1x2.

The problem here is, I can't calculate the numerical hessian via the DERIVESTsuite (http://www.mathworks.com/matlabcentral/fileexchange/13490-adaptive-robust-numerical-differentiation). I'll get the error:

Error using  * 
Inner matrix dimensions must agree.

Error in calcHq>@(W)sqrt((F*W-Y).*(F*W-Y))*ones(size(Y)) (line 16)
    f = @(W) sqrt((F*W - Y) .* (F*W - Y)) * ones(size(Y));

Error in hessdiag>@(xi)fun(swapelement(x0,ind,xi)) (line 60)
    @(xi) fun(swapelement(x0,ind,xi)), ...

Error in derivest (line 337)
      f_x0(j) = fun(x0(j));

Error in hessdiag (line 59)
  [HD(ind),err(ind),finaldelta(ind)] = derivest( ...

Error in hessian2 (line 74)
[hess,err] = hessdiag(fun,x0);

I assume there is some problem with the elementwise multiplication, but I really can't figure out what I'm doing wrong.

Maybe someone can give me a hint on what I'm doing wrong.

2
  • This might be just an issue with the dot product. Make sure that in the products F*X and (...)*ones(...) the first vector is a row vector and the second is a column vector (of the same length, of course). The dimensions you wrote seem to suggest otherwise. Commented Apr 27, 2015 at 10:58
  • You may have use of the matlab debugger. The tool is really good and extremely easy to use. Learning this will make your life easier. The guess is that the problem is here f = @(W) sqrt((F*W - Y) .* (F*W - Y)) * ones(size(Y));, unless you have misinterpreted the function. However, for your own sake, you may want to start by calling f and make sure that you get output you expect. Do this by setting a breakpoint on the line after f is assigned and do the function call in the command window, using some input. Commented Apr 27, 2015 at 11:15

1 Answer 1

1

Ok guys, thank you very much. I just found out what I did wrong and it is so embarassing ... The order of multiplications is wrong ...

f = @(X) sqrt((F*X - Y) .* (F*X - Y)) * ones(size(Y));

This gives back an ixi matrix, while

f = @(X) ones(size(Y)) * sqrt((F*X - Y) .* (F*X - Y));

gives back a scalar.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.