Ordering of MATLAB operator - matlab

I'm trying to understand a MATLAB code for reserach but seem to be stuck in a basic question regarding the order of the transpose operator and ml divide operator. (please see the code below)
C = randi([0 ,1], [3,3])
D = randi([0 ,1], [3,3])
disp("C-(D')*(C\D')")
disp(C-(D')*(C\D'))
(you may need to run this multiple times until the inverse exists)
Because the transpose operator is computed before the \ operator, it seems taht C\D' should be equal to finding the x s.t. C=(D')*x... However, when I subsrtitute x as C\D' and display C-(D')*x, I don't get a zero... could anyone give me a reason why?

C = [0,0,1;0,1,0;1,0,1];
D = [1,1,0;1,0,1;1,0,0];
%% rule
% "A \ B" equal to "inv(A) * B"
% "B / A" equal to "B * inv(A)"
%% mine
X = C / D'; % X = C * inv(D')
C - X * D' % C - (C * inv(D')) * D' = C - C * (inv(D') * D') = 0
%% yours
Y = C \ D'; % Y = inv(C) * D'
C - D'* Y % C - D' * (inv(C) * D') ≠ 0

Related

How to solve two equations with bvp4c Matlab?

I need to solve two differential equations, where I have two boundary conditions for every equation. Because of that I choose Matlab method bvp4c to solve this system:
p1' = -32 * beta * m1 / (R ^ 4 * p1)
p2' = - ( - 8 * p1' / R - p1' * p2 - 32 * beta * m2 / R ^ 4 ) / p1
I have function file bvpfcn.m where I defined two differential equations:
function dpdz = bvpfcn(z,p)
beta = 1;
m1 = 1;
m2 = 0.1;
ri = 0.7;
z = linspace(0,1,1001);
R = ri - z .* (ri - 1);
dpdz = zeros(2,1001);
dpdz = [- 32 .* beta .* m0 ./ (R .^ 4 .* p(1));
-( - 8 .* dpdz(1) ./ R - dpdz(1) .* p(2) - 32 .* beta .* m1 ./ R .^ 4 ) ./ p(1);
];
end
Then in function file bcfcn.m I defined boundary conditions. My boundary conditions are:
p(1)|(z=0) = 8
p(1)|(z=1) = 1
p(2)|(z=0) = 0
p(2)|(z=1) = 0
And file bcfcn.m is:
function res = bcfcn(pa,pb)
res = [pa(1)-8;
pa(2);
pb(1)-1;
pb(2)];
end
My solution need to be of shape:
shape of expected solution
Because of that shape I am making guess function of cosine type:
function g = guess(z)
g = [0.65.*cos(z)
0.65.*cos(z)];
end
When I execute everything with:
beta = 1;
m1 = 1;
m2 = 0.1;
ri = 0.7;
xmesh = linspace(0,1,1001);
solinit = bvpinit(xmesh, #guess);
sol = bvp4c(#bvpfcn, #bcfcn, solinit);
I got error:
Error using bvparguments (line 111)
Error in calling BVP4C(ODEFUN,BCFUN,SOLINIT):
The boundary condition function BCFUN should return a column vector of length 2.
Error in bvp4c (line 130)
bvparguments(solver_name,ode,bc,solinit,options,varargin);
How can I make column vector when I have 4 boundary conditions? Are my assumpsions ok?
Your column vector from bvpfcn and bcfcn should have same dimensions. Since you have two first order ODEs, you should give only two boundary conditions. You have two extra boundary conditions, these extra conditions will be dependent on other two. You have to find out the independent boundary conditions

How to use "fmincon" to solve matrix minimization

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)

How to apply a function over a matrix and simultaneously pass parameters?

I have a matrix e.g. as follows:
N = magic(100);
I want to apply a function on each column and save its output in another matrix.
Here is my function
function z = nikfoo(y, lambda, p)
m = length(y);
D = diff(speye(m), 2);
w = ones(m, 1);
for it = 1:10
W = spdiags(w, 0, m, m);
C = chol(W + lambda * D' * D);
z = C \ (C' \ (w .* y));
w = p * (y > z) + (1 - p) * (y < z);
end
At first I tried to make a matrix the same size as my data
% I take the size of my matrix
[a, b] = size(N);
% make the same size matrix with zeros
corrNiki = zeros(a,b);
then I try to do it as follows
% I set the lambda as 1000 and p as 0.1
for i = 1:b
corrNiki(i) = nikfoo(N(i),1000,0.1);
end
So now I have these questions
how can I pass this function with the fixed lammda and p and save the results in corrNiki?
how can I also pass different values to the Lamda in a range and in P in a range and then save their outputs ?
for example
Lammda 1000 and P0.1 is saved in data1
Lammda 2000 and P0.01 is saved in data2
.
.
.

How to compute log of multvariate gaussian in matlab

I am trying to compute log(N(x | mu, sigma)) in MATLAB where
x is the data vector(Dimensions D x 1) , mu(Dimensions D x 1) is mean and sigma(Dimensions D x D) is covariance.
My present implementation is
function [loggaussian] = logmvnpdf(x,mu,Sigma)
[D,~] = size(x);
const = -0.5 * D * log(2*pi);
term1 = -0.5 * ((x - mu)' * (inv(Sigma) * (x - mu)));
term2 = - 0.5 * logdet(Sigma);
loggaussian = const + term1 + term2;
end
function y = logdet(A)
y = log(det(A));
end
For some cases I get an error
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =
NaN
I know you will point out that my data is not consistent, but I need to implement the function so that I can get the best approximate instead of throwing an warning. . How do I ensure that I always get a value.
I think the warning comes from using inv(Sigma). According to the documentation, you should avoid using inv where its use can be replaced by \ (mldivide). This will give you both better speed and accuracy.
For your code, instead of inv(Sigma) * (x - mu) use Sigma \ (x - mu).
The following approach should be (a little) less sensitive to ill-conditioning of the covariance matrix:
function logpdf = logmvnpdf (x, mu, K)
n = length (x);
R = chol (K);
const = 0.5 * n * log (2 * pi);
term1 = 0.5 * sum (((R') \ (x - mu)) .^ 2);
term2 = sum (log (diag (R)));
logpdf = - (const + term1 + term2);
end
If K is singular or near-singular, you can still have warnings (or errors) when calling chol.

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)