2D fused Lasso with Matlab CVX - matlab

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.

Related

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

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.

Error plotting symbolic variable in MATLAB?

I am trying to learn fourier transform from book"signals and systems laboratory with matlab alex palamides"
On page 312, following code is given which demonstrates that convolution can be implemented by multiplying the fourier transforms of two signals and then taking the inverse fourier of product
syms t w
x1=heaviside(t)-heaviside(t-2);
x2=heaviside(t)-heaviside(t-4);
X1=fourier(x1,w);
X2=fourier(x2,w);
right=ifourier(X1*X2,t)
ezplot(right)
I tried MATLAB 2019 and MATLAB 2020 but i get same problem in both
Actually When i try to run above code in my MATLAB i don't get output like the one in book, instead i get following error
Error using inlineeval (line 14)
Error in inline expression ==> (t.*pi.*sign(t) + fourier(cos(2.*w)./w.^2, w, -t) +
fourier(cos(4.*w)./w.^2, w, -t) - fourier(cos(6.*w)./w.^2, w, -t) - fourier(sin(2.*w)./w.^2, w,
-t).*1i - fourier(sin(4.*w)./w.^2, w, -t).*1i + fourier(sin(6.*w)./w.^2, w, -t).*1i)./(2.*pi)
Undefined function 'fourier' for input arguments of type 'double'.
Error in inline/feval (line 33)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in ezplotfeval (line 53)
z = feval(f,x(1),y(1));
Error in ezplot>ezimplicit (line 271)
u = ezplotfeval(f, X, Y);
Error in ezplot (line 167)
hp = ezimplicit(cax, f{1}, vars, labels, args{:});
Error in sym/ezplot (line 66)
h = ezplot(fhandle(f)); %#ok<EZPLT>
Error in Untitled (line 7)
ezplot(right)
Snapshot of book page also attached here
I found the same question on MATLAB Answers. The solution as posted by
Walter Roberson is to rewrite X1*X2 in terms of exp before taking the inverse fourier transform. Quoting from MATLAB Answers:
In your release of MATLAB, ezplot() was not compatible with plotting symbolic expressions, and fplot() had to be used instead.
However, the ifourier() is giving unusable results that neither ezplot() nor fplot() can use.
The work-around, valid from R2012a, is:
right = ifourier( rewrite(X1*X2, 'exp'), t);
fplot(right, [0 8])
Result (on R2021b):
It looks like matlab cannot transform the function X1*X2, so it returns the inverse fourier transform as an unevaluated call to fourier.
>> right
right =
(pi*t*sign(t) + fourier(cos(2*w)/w^2, w, -t) + fourier(cos(4*w)/w^2, w, -t) - fourier(cos(6*w)/w^2, w, -t) - fourier(sin(2*w)/w^2, w, -t)*1i - fourier(sin(4*w)/w^2, w, -t)*1i + fourier(sin(6*w)/w^2, w, -t)*1i)/(2*pi)
ezplot, or fplot can not plot a expression like this.

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