Trouble creating an orthonormal matrix with given vector in matlab - matlab

Similar problem to this question matrix that forms an orthogonal basis with a given vector but I'm having problems with my implementation. I have a given vector, u, and I want to construct an arbitrary orthonormal matrix V such that V(:,1) = u/norm(u). The problem I'm getting is that V'*V isn't I (but is diagonal). Here's my code:
function V = rand_orth(u)
n = length(u);
V = [u/norm(u) rand(n,n-1)];
for i = 1:n
vi = V(:,i);
rii = norm(vi);
qi = vi/rii;
for j = i+1:n
rij = dot(qi,V(:,j));
V(:,j) = V(:,j) - rij*qi;
end
end
end

Simply normalize columns of V once you've finished the orthogonalization. Note there's also no need to normalize u (first column) initially, since we will normalize the whole matrix at the end:
function V = rand_orth(u)
n = length(u);
V = [u, rand(n,n-1)];
for i = 1:n
vi = V(:,i);
rii = norm(vi);
qi = vi/rii;
for j = i+1:n
rij = dot(qi,V(:,j));
V(:,j) = V(:,j) - rij*qi;
end
end
V = bsxfun(#rdivide, V, sqrt(diag(V'*V))');
end

Related

Arnoldi algorithm in Matlab

function [V,H] = Arnoldi(A,v,m)
[n,~] = size(A);
V = zeros(n,m+1);
H = zeros(n,n);
V(:,1) = v/norm(v);
for k = 2:m
V(:,k) = A*V(:,k-1);
for j = 1:(k-1);
H(j,k-1) = V(:,j).'*V(:,k);
V(:,k) = V(:,k)- H(j,k-1)*V(:,j);
end
H(k,k-1) = norm(V(:,k));
V(:,k) = V(:,k)/H(k,k-1);
end
end
This is my implementation of the Arnoldi algorithm. We already used wikipedia but did not find an answer there.
A is a square matrix, v is our starting vector and m is the number of iterations/ dimension of Krylov subspace. It does not give the wanted Hessian H and we can not figure out where we go wrong. Can anybody help us?

Preventing using for loop in MATLAB

I have written the below MATLAB code. I want to know how can I optimize it without using for loop.
Any help will be very appreciated.
MATLAB code:
%Some parameters:
s = 50;
k = 50;
r = 0.1;
v = 0.2;
t = 2;
n=10000;
% Calculate CT by calling EurCall function
CT = EurCall(s, k, r, v, t, n);
%Function EurCall to be called
function C = EurCall(s, k, r, v, t, n)
X = zeros(n,1);
hh = zeros(n,1);
for ii = 1 : n
X(ii) = normrnd(0, 1);
SS = s*exp((r - v^2/2)*t + v*X(ii)*sqrt(t));
hh(ii) = exp(-r*t)*max(SS - k, 0);
end %end for loop
C = (1/n) * sum(hh);
end %end function
Vectorized Approach:
Here is a vectorized approach that I think replicates the same functionality as the original script. Instead of looping this example declares X as a vector of size n by 1. By using element-wise multiplication .* we can effectively calculate the remaining vectors SS and hh without need to loop through the indices. In this case SS and hh will also be vectors of size n by 1. I do agree with comment above that MATLAB's for-loops are no longer inherently slow.
%Some parameters:
s = 50;
k = 50;
r = 0.1;
v = 0.2;
t = 2;
n=10000;
% Calculate CT by calling EurCall function
[CT] = EurCall(s, k, r, v, t, n);
%Function EurCall to be called
function [C] = EurCall(s, k, r, v, t, n)
X = zeros(n,1);
hh = zeros(n,1);
mu = 0; sigma = 1;
%Creating a vector of normal random numbers of size (n by 1)%
X = normrnd(mu,sigma,[n 1]);
SS = s*exp((r - v^2/2)*t + v.*X.*sqrt(t));
hh = exp(-r*t)*max(SS - k, 0);
C = (1/n) * sum(hh);
end %end function
Ran using MATLAB R2019b

matlab vectorization and avoid for loop

There are two matrix X and M and I need to obtain the following matrix D
m = 20; n = 10;
X = rand(m,n);
M = rand(m,m);
M = (M + M')/2;
D = zeros(n,n);
for i = 1:n
for j = 1:n
D(i,j) = X(:,i)'*M*X(:,j);
end
end
When n and m are large, the computation of D is very slow. Is there any way to speed up?
The answer would be:
D = 0.5*X.'*(M+M')*X
(This is a slight modification of the solution provided by Divakar, so that the correct matrix D is returned)

cant find my code error in LU decomposition [duplicate]

I am trying to implement my own LU decomposition with partial pivoting. My code is below and apparently is working fine, but for some matrices it gives different results when comparing with the built-in [L, U, P] = lu(A) function in matlab
Can anyone spot where is it wrong?
function [L, U, P] = lu_decomposition_pivot(A)
n = size(A,1);
Ak = A;
L = zeros(n);
U = zeros(n);
P = eye(n);
for k = 1:n-1
for i = k+1:n
[~,r] = max(abs(Ak(:,k)));
Ak([k r],:) = Ak([r k],:);
P([k r],:) = P([r k],:);
L(i,k) = Ak(i,k) / Ak(k,k);
for j = k+1:n
U(k,j-1) = Ak(k,j-1);
Ak(i,j) = Ak(i,j) - L(i,k)*Ak(k,j);
end
end
end
L(1:n+1:end) = 1;
U(:,end) = Ak(:,end);
return
Here are the two matrices I've tested with. The first one is correct, whereas the second has some elements inverted.
A = [1 2 0; 2 4 8; 3 -1 2];
A = [0.8443 0.1707 0.3111;
0.1948 0.2277 0.9234;
0.2259 0.4357 0.4302];
UPDATE
I have checked my code and corrected some bugs, but still there's something missing with the partial pivoting. In the first column the last two rows are always inverted (compared with the result of lu() in matlab)
function [L, U, P] = lu_decomposition_pivot(A)
n = size(A,1);
Ak = A;
L = eye(n);
U = zeros(n);
P = eye(n);
for k = 1:n-1
[~,r] = max(abs(Ak(k:end,k)));
r = n-(n-k+1)+r;
Ak([k r],:) = Ak([r k],:);
P([k r],:) = P([r k],:);
for i = k+1:n
L(i,k) = Ak(i,k) / Ak(k,k);
for j = 1:n
U(k,j) = Ak(k,j);
Ak(i,j) = Ak(i,j) - L(i,k)*Ak(k,j);
end
end
end
U(:,end) = Ak(:,end);
return
I forgot that If there was a swap in matrix P I had to swap also the matrix L. So just add the next line after after swapping P and everything will work excellent.
L([k r],:) = L([r k],:);
Both functions are not correct.
Here is the correct one.
function [L, U, P] = LU_pivot(A)
[m, n] = size(A); L=eye(n); P=eye(n); U=A;
for k=1:m-1
pivot=max(abs(U(k:m,k)))
for j=k:m
if(abs(U(j,k))==pivot)
ind=j
break;
end
end
U([k,ind],k:m)=U([ind,k],k:m)
L([k,ind],1:k-1)=L([ind,k],1:k-1)
P([k,ind],:)=P([ind,k],:)
for j=k+1:m
L(j,k)=U(j,k)/U(k,k)
U(j,k:m)=U(j,k:m)-L(j,k)*U(k,k:m)
end
pause;
end
end
My answer is here:
function [L, U, P] = LU_pivot(A)
[n, n1] = size(A); L=eye(n); P=eye(n); U=A;
for j = 1:n
[pivot m] = max(abs(U(j:n, j)));
m = m+j-1;
if m ~= j
U([m,j],:) = U([j,m], :); % interchange rows m and j in U
P([m,j],:) = P([j,m], :); % interchange rows m and j in P
if j >= 2; % very_important_point
L([m,j],1:j-1) = L([j,m], 1:j-1); % interchange rows m and j in columns 1:j-1 of L
end;
end
for i = j+1:n
L(i, j) = U(i, j) / U(j, j);
U(i, :) = U(i, :) - L(i, j)*U(j, :);
end
end

Drazin inverse of a matrix

Is there an algorithm that computes the Drazin inverse of a singular matrix? I would like to apply it either in MATLAB or Mathematica.
Read this article:
Fanbin Bu and Yimin Wei, The algorithm for computing the Drazin inverses of two-variable polynomial matrices, Applied mathematics and computation 147.3 (2004): 805-836.
in appendix there are several MATLAB code. The first one is this:
function DrazinInverse1a = DrazinInverse1(a)
%-----------------------------------------
%Compute the Drazin Inverse of a matrix 'a' using the limited algorithm.
%Need computing the index of 'a'.
global q1 q2 s1 s2
[m,n] = size(a);
if m~= n
disp('Matrix is must be square!')
end
%-----------------------------------------
% Computer the index of A and note r = rank(A^k).
[k,r,a1,a] = index(a);
F = eye(n);
g = -trace(a);
g = collect(g);
for i = 1:r-1
G = g*eye(n);
F = a*F+G;
g = -trace(a*F)/(i+1);
g = collect(g);
end
DrazinInverse1a = a1*F;
DrazinInverse1a = -1/g*DrazinInverse1a;
DrazinInverse1a = simplify(DrazinInverse1a);
end
function [k,r,a1,a] = index(a)
%To compute the index of 'a'.
k = 0;
n = length(a);
r = n;
a0 = a;
r1 = rank(a);
a1 = eye(n);
while r ~= r1
r = r1;
a1 = a;
a = a*a0;
r1 = rank(a);
k = k+1;
end
r = sym2poly(r);
end