Function in abs() resolves to double but there's still an error

I'm using the optimisation toolbox and my objective is to try and find a vector of length sz, x1, such that the "covariance" between the target and a different known vector, x0, also of length sz, is as close to zero as possible. To do this, I've set the objective function to be
abs( dot(x1, x0) / sz - 1/4 )
However, I get an error that says "Incorrect number or types of inputs or outputs for function 'abs'". The input is one double and the output is one double so I don't understand what the problem is. Searching for the error online doesn't help either. Please explain why this error is occuring.
Thanks.

6 Comments

Can you share the snippet of code and error?
sz = 2000;
lower = zeros(sz,1);
upper = ones(sz,1);
u0 = rand(sz,1);
f1 = uniform00(:,1);
% Create optimization variables
uk116 = optimvar("uk1",2000,1,"LowerBound",lower,"UpperBound",upper);
% Set initial starting point for the solver
initialPoint15.uk1 = u0;
% Create problem
problem2 = optimproblem;
% Define problem objective
problem2.Objective = abs( dot(uk116, f1) / sz - 1/4);
Here, uniform00 is a 2000x4 matrix imported from a csv file. Each column represents values derived from a uniform random variable.
The error on the final line is "Incorrect number or types of inputs or outputs for function 'abs'."
If f1 is a double array, try
% Create optimization variables
uk1 = optimvar('uk1',2000,1,"LowerBound",lower,"UpperBound",upper);
% Set initial starting point for the solver
initialPoint15.uk1 = u0;
% Create problem
problem2 = optimproblem;
% Define problem objective
f = @(x) abs( dot(x, f1) / sz - 1/4);
obj = fcn2optimexpr(f,uk1);
problem2.Objective = obj;
or use
(dot(uk116, f1) / sz - 1/4)^2
instead of
abs( dot(uk116, f1) / sz - 1/4)
@Christopher Your problem is quite ill-posed. basically, you are trying to find an x1 that lies within the intersection of a hyperplane and a box. If the problem has a solution, there will be an infinite number of them.
Thanks. What should I use if I just want any solution?
Much appreciated, Chris.
@Torsten Of course! Using ^2 instead of abs(); why didn't I think of that!? Thanks. ^_^

Sign in to comment.

Answers (0)

Categories

Products

Release

R2022b

Asked:

on 17 Mar 2023

Edited:

on 19 Mar 2023

Community Treasure Hunt

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

Start Hunting!