Disciplined convex programming error: Invalid quadratic form(s): not a square - matlab

I want to optimize the objective function using the CVX Matlab tool, knowing that the optimization process is normally executed using other metaheuristic algorithms.
w_BB=zeros(4,1);
cvx_begin
variable w_BB complex
expression P
P=w_BB'*W_RF0'*A(:,2)*A(:,2)'*W_RF0*w_BB;
minimize( P )
subject to
(w_BB'*W_RF0'*A(:,1)) == 1;
cvx_end
and gives
Error using .* (line 262)
Disciplined convex programming error:
Invalid quadratic form(s): not a
square.
Error in * (line 36)
z = feval( oper, x, y );
Error in CVXmainRun (line 49)
P=w_BB'*W_RF0'*A(:,2)*A(:,2)'*W_RF0*w_BB;
>>
The goal is to find the optimum w_BB that acheive minimum value.

Related

2D fused Lasso with Matlab CVX

I wrote a 2D fused lasso code here.
[m n] = size(circle);
cvx_begin
variable theta(m, n);
minimize( norm(circle-theta, 'fro'));
subject to
sum(sum(abs(theta(:,1:n-1)-theta(:,2:n)))) == 0;
sum(sum(abs(theta(1:m-1,:)-theta(2:m,:)))) == 0;
cvx_end
Weirdly, the program report,
In cvxprob (line 28) In cvx_begin (line 41) Error using cvxprob/newcnstr (line 192) Disciplined convex programming error:
Invalid constraint: {convex} == {constant}
Error in == (line 12) b = newcnstr( evalin( 'caller', 'cvx_problem',
'[]' ), x, y, '==' );
After I remove abs() in the constraint, the program could run, but that's not what constraints I expect to be.
I think you can try stacking the matrices into vectors, then use L1 norm. In CVX, it's just norm(variable, 1). It will do the same as you wrote here: sum of absolute elementary-wise differences.

Error: A and B must be floating point scalars

I am trying to optimize the function using fminsearch and function handle
but, I get the error A and B must be floating point scalars. In detalis,
Error using integral (line 86)
A and B must be floating point scalars.
Error in #(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3))
Error in #(x)((x-f2(x,p)).^2)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in #(p)(integral(#(x)((x-f2(x,p)).^2),-3,3))
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
How can I solve this?
i think x-3 become a problem but i cannot deal with it.
x should be variable in f2 in order to do integral with respect to x in q3
Thank you in advance
sigma=0.1;
f2=#(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3));
q3=#(p)(integral(#(x)((x-f2(x,p)).^2),-3,3));
[p, fval] = fminsearch(q3,[0.1 0.4]);
The problem is that when you integrate over x, the function "integrate" gives f2 a vector to evaluate. Which is the same reason you use the dot notation in the function "q3".
The quick fix is to use arrayfun around the "f2" - but you should really consider to adapt to what user20160 suggested in the comments, namely to make full functions, then it will be easier to debug and as it takes in a vector, you can make a for loop, which runs over the inputted endpoints. Standard for-loops are faster than standard arrayfun.

MATLAB errors when using Lagrange Multipliers?

I have code that, when run, should correctly use Lagrange Multipliers to find the maximum/minimum of a function here:
clear all
syms x y L;
f = x^4+2*y^4;
g = x^2+5*y^2+2*y^2-10;
firstpart=jacobian(f,[x y])-L*jacobian(g,[x y]);
[Lsoln,xsoln,ysoln]=solve(firstpart,x^2+5*y^2+2*y^2-10);
subs(f,{x,y},{xsoln,ysoln})
% The coordinates that correspond with the greatest and smallest values
% above are the maximum and minimum, respectively.
However, when I run it, I get four errors:
Error using sym.getEqnsVars>checkVariables (line 92) The second
argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 62)
checkVariables(vars);
Error in solve>getEqns (line 450) [eqns, vars] =
sym.getEqnsVars(argv{:});
Error in solve (line 225) [eqns,vars,options] = getEqns(varargin{:});
Could anyone help?
You are passing two equations as individual arguments zu solve, that is not possible. You have to put both into an array
[Lsoln,xsoln,ysoln]=solve([firstpart,x^2+5*y^2+2*y^2-10] );

"Unable to prove `expr` literally..." error when trying to compare a symbol inside a function

I just started learning MATLAB and I'm trying to normalize a bump function given by
function b = bump(x)
region1 = abs(x) < 1
b(region1) = (exp(-1./(1 - x(region1).^2)))
region2 = abs(x) >= 1
b(region2) = 0
end
To do this, I need to divide by the definite integral from -1 to 1. However, when I input
syms x;
int(bump(x), -1, 1)
I get a long error message, which says
Error using symengine (line 58)
Unable to prove 'abs(x) < 1' literally. To test the statement mathematically, use isAlways.
Error in sym/subsindex (line 1554)
X = find(mupadmex('symobj::logical',A.s,9)) - 1;
Error in sym>privformat (line 2357)
x = subsindex(x)+1;
Error in sym/subsref (line 1578)
[inds{k},refs{k}] = privformat(inds{k});
Error in bump (line 3)
b(region1) = (exp(-1./(1 - x(region1).^2)))
I tried replacing abs(x)<1 with what I think is the suggested isAlways(abs(x)<1), and that removes the error, although it gives the wrong answer (it says the integral is zero).
I don't understand what does the error message means.
syms x defines x as a symbolic variable, invoking symbolic computation on x. This probably isn't what you want.
Instead, define x as some kind of input matrix, e.g. x = zeros(3);. Or, to do numeric integration, use the integral function:
integral(#bump, -1, 1)

Using fgoalattain() or solve() for Matlab

I am trying to solve a problem (think excel "goal seek") where given a specific rate of return, matlab can solve for the variable "y" (which is used to multiply a matrix).
I've tried using fgoalattain (i don't understand it) and solve
here is the function which basically adds some arrays together to make a cashflow with y being a double. ppam_gen is a n*1 matrix. other_tot is a n*1 matrix.
down_pmt is a double.
function ansirr = cirr(y)
cf_gen_oth = y*ppam_gen + oth_tot
dp = [-down_pmt]
cashflow = [dp;cf_gen_oth]
ansirr = irr(cashflow)
%ansirr = irr([dp; y*ppam_gen + oth_tot])
end
x = fgoalattain(#cirr,0.001,.15,abs(.15))
does not work, the following error is given:
Error using roots (line 28)
Input to ROOTS must not contain NaN or Inf.
Error in irr (line 134)
coeff = roots(fliplr(cf(:,loop)')); % Find roots of polynomial
Error in ppa_model/cirr (line 139)
ansirr = irr(cashflow)
Error in goalcon (line 26)
f = feval(funfcn{3},x,varargin{:});
Error in
fgoalattain>#(y,varargin)feval(cfun{3},y,neqgoals,funfcn,confcn,WEIGHT,GOAL,x,errCheck,varargin{:})
(line 473)
cfun{3} = #(y,varargin)
feval(cfun{3},y,neqgoals,funfcn,confcn,WEIGHT,GOAL,x,errCheck,varargin{:});
Error in nlconst (line 746)
[nctmp,nceqtmp] = feval(confcn{3},x,varargin{:});
Error in fgoalattain (line 519)
[xnew,ATTAINFACTOR,LAMBDA,EXITFLAG,OUTPUT]=...
Error in ppa_model (line 152)
r = fgoalattain(#cirr,0.001,.15,abs(.15))
and using symbolic toolbox doesn't work either.
syms y
solve(irr([-down_pmt ; y*ppam_gen + oth_tot]) == 0.15)
throws the error:
Error using assignin
Attempt to add "y" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
Error in syms (line 66)
assignin('caller',x,sym(x));
Error in ppa_model (line 152)
syms y