How to use "fmincon" to solve matrix minimization - matlab

Question: How to use "fmincon" to solve the following minimization matrix problem?
I am trying to find the f such that
a * ( b – ( inv(a) * inv(inv(a) + transpose(c)*inv(f)*c) * (inv(a)*d + transpose(c) * inv(f) * e) ) )^2
is minimized subject to:
f > 0
++++Variables:
a is (8x8) matrix which is known.
b is (8x1) column vector which is known.
c is (1x8) column vector which is known.
d is (8x1) scalar which is known.
e is (1x1) scalar which is known.
and
f is a scalar and is unknown.

Thanks to the #m7913d, I solved the code via Isqnonlin:
clc;
clear all;
% random inputs A, B, C, D and E
a = rand(8,8)'*rand(8,8);
b = 2*rand(8,1) - 1;
c = 2*rand(1,8) - 1;
d = 2*rand(8,1) - 1;
e = 2*rand(1,1) - 1;
% minimization term
fun = #(f) a * ( b - ( inv(a) * inv(inv(a)+c'*inv(f)*c) * (inv(a)*d+c' * inv(f) * e) ) );
f = lsqnonlin(fun,0.1,0,+inf)

Related

Simplifying function by removing a loop

What would be the best way to simplify a function by getting rid of a loop?
function Q = gs(f, a, b)
X(4) = sqrt((3+2*sqrt(6/5))/7);
X(3) = sqrt((3-2*sqrt(6/5))/7);
X(2) = -sqrt((3-2*sqrt(6/5))/7);
X(1) = -sqrt((3+2*sqrt(6/5))/7);
W(4) = (18-sqrt(30))/36;
W(3) = (18+sqrt(30))/36;
W(2) = (18+sqrt(30))/36;
W(1) = (18-sqrt(30))/36;
Q = 0;
for i = 1:4
W(i) = (W(i)*(b-a))/2;
X(i) = ((b-a)*X(i)+(b+a))/2;
Q = Q + W(i) * f(X(i));
end
end
Is there any way to use any vector-like solution instead of a for loop?
sum is your best friend here. Also, declaring some constants and creating vectors is useful:
function Q = gs(f, a, b)
c = sqrt((3+2*sqrt(6/5))/7);
d = sqrt((3-2*sqrt(6/5))/7);
e = (18-sqrt(30))/36;
g = (18+sqrt(30))/36;
X = [-c -d d c];
W = [e g g e];
W = ((b - a) / 2) * W;
X = ((b - a)*X + (b + a)) / 2;
Q = sum(W .* f(X));
end
Note that MATLAB loves to handle element-wise operations, so the key is to replace the for loop at the end with scaling all of the elements in W and X with those scaling factors seen in your loop. In addition, using the element-wise multiplication (.*) is key. This of course assumes that f can handle things in an element-wise fashion. If it doesn't, then there's no way to avoid the for loop.
I would highly recommend you consult the MATLAB tutorial on element-wise operations before you venture onwards on your MATLAB journey: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html

Translate an equation into a code

Can you help me please translate this equation into a code?
I tried this code but its only the first part
theSum = sum(M(:, y) .* S(:, y) ./ (1 + K(:, y)))
EDIT: sorry, I was having a brainfart. The answer below makes no assumptions on the nature of M, K, etc, which is why I recommended functions like that. But they're clearly matrices. I'll make another answer, I'll leave this here for reference though in case it's useful
I would start by making the M, K, X, L, and O expressions into simple functions, so that you can easily call them as M(z,y), X(z,y) (or X(z,j) depending on the input you need ) etc.
Then you will convert each summation into a for loop and collect the result (you can think about vectorisation later, right now focus on translating the problem). The double summation is essentially a nested for loop, where the result of the inner loop is used in the outer one at each outer iteration.
So your end result should look something like:
Summation1 = 0;
for z = 1 : Z
tmp = M(z,y) / K(z,y) * (X(z,y) / (1 + L(z,y));
Summation1 = Summation1 + tmp;
end
Summation2 = 0;
for j = 1 : Y
if j ~= y
for z = 1 : Z
tmp = (M(z,j) * X(z,j) * O(j)) / (K(z,j)^2 * (1 + L(z,j)) * X(z,y);
Summation2 = Summation2 + tmp;
end
end
end
Result = Summation1 - Summation2;
(Btw, this assumes that all operations are on scalars. If M(z,y) outputs a vector, adjust for elementwise operations appropriately)
IF M, K, etc are all matrices, and all operations are expected to be element-wise, then this is a vectorised approach for this equation.
Left summation is
S1 = M(1:Z,y) ./ K(1:Z,y) .* X(1:Z,y) ./ (1 + L(1:Z,y));
S1 = sum(S1);
Right summation is (assuming (O is a horizontal vector)
S2 = M(1:Z, 1:Y) .* X(1:X, 1:Y) .* repmat(O(1:Y), [Z,1]) ./ ...
(K(1:Z, 1:Y) .^ 2 .* (1 + L(1:Z, 1:Y))) .* X(1:Z, 1:Y);
S2(:,y) = []; % remove the 'y' column from the matrix
S2 = sum(S2(:)); % add all elements
End result: S1 - S2
this is lambda version vectorized:
equation = #(y,M,K,X,L,O) ...
sum(M(:,y)./K(:,y).*X(:,y)./(1+L(:,y))) ...
-sum(sum( ...
bsxfun( ...
#times ...
,M(:,[1:y-1,y+1:end]) ...
.* X(:,[1:y-1,y+1:end]) ...
.* O(:,[1:y-1,y+1:end]) ...
./ (K(:,[1:y-1,y+1:end]) .^ 2 ...
.*(1+ L(:,[1:y-1,y+1:end]))) ...
,X(:,y) ...
) ...
));
%%% example:
y = 3;
Y = 5;
Z = 10;
M = rand(Y, Z);K = rand(Y, Z);X = rand(Y, Z);L = rand(Y, Z);O = rand(Y, Z);
equation(y,M,K,X,L,O)

How can I get a proper double form for a symbolic matrix?

How can I get a proper double form from this symbolic Matrix in MatLab? I've tried everything but I prefer not using feval or inline function as they're not recommended
This is the code to get the matrix
function T = Romberg (a, b, m, f)
T = zeros(m, m);
T = sym(T);
syms f(x) c h;
f(x) = f;
c = (subs(f,a)+subs(f,b)) / 2;
h = b - a;
T(1,1) = h * c;
som = 0 ;
n = 2;
for i = 2 : m
h = h / 2;
for r = 1 : n/2
som = som + subs(f,(a + 2*(r-1)*h));
T(i,1) = h * (c + som);
n = 2*n;
end
end
r = 1;
for j = 2 : m
r = 4*r;
for i = j : m
T(i,j) = (r * T(i, j-1) - T(i-1,j-1)/(r-1));
end
end
end
And with an input like this
Romberg(0, 1, 4, '2*x')
I get a symbolic matrix output with all the
3 * f(3)/2 + f(1)/2 + f(5)/2
I would like to have a double output.
Can you please help me?
Thank you very much in advance!

Optimization with Lagrange multipliers

I have an optimization problem which I have to solve using Lagrange multipliers.
I have the following code:
clear all
clc
p = #(r,sig) (r./sig.^2) .* exp(-r.^2/2*(sig.^2));
sig = 1;
Pa = 1;
Pc = 20;
a = 0.5;
b = 1;
phi = #(h_AB,h_AC,h_CB) abs(h_AB).^2 +((b .* a.^2*abs(h_AC).^2 .* abs(h_CB).^2)
.* Pc ./ (1 + b * a .^2 .* abs(h_CB) .^2 * Pc));
f1 = #(h_AB,h_AC,h_CB) .5*log((1+phi(h_AB,h_AC,h_CB)*Pa)/(1+abs(h_AC).^2*Pa));
f2 = #(h_AB,h_AC,h_CB) f1(h_AB,h_AC,h_CB) .* p(h_AB,sig) .* p(h_AC,sig).* (h_CB,sig);
q = integral3(f2,-inf,inf,-inf,inf,-inf,inf);
The constant b that I have defined should be a variable (q be a function of b), so that I would find e = jacobian(q,[b]); in the next step.
The problem is that I can't define b as symbolic while other functions are not symbolic and when I define all the functions in symbolic form, I get out of memory solving e = jacobian(q,[b]);

How Can I Solve Matrix Equation in Matlab

Say I have the following matrix equation
X - B*X*C = D
Where,
X: 3 by 5, to be solved;
B: 3 by 3;
C: 5 by 5;
D: 3 by 5;
Is there any convenient method that I can use for solving the system? fsolve?
In case B or C are invertible, you can check the matrix cookbook section 5.1.10 deals with similar settings:
X * inv(C) - B * X = D * inv(C)
Can be translated to
x = inv( kron( eye, -B ) + kron( inv(C)', eye ) ) * d
where x and d are vector-stack of X and D respectively.
You can use MATLAB's dlyap function:
X = dlyap(B,C,D)