Dimensional Problem with simple coefficient equation - matlab

I am trying to create this function in MATLAB. From a previous question, I have solved for a as agrid2 and f(a) as fx, whose dimensions are 600*1 and 600*2- these two vectors are absolutely correct:
%calculating gini coefficent
m = 600;
for i = 1:m %expanded length of kgrid of ai previously calculated in invarden.m
mu = sum(agrid2.*fx(m,:));
gini = sum(fx(m,1)*(fx(m,2))*abs(fx(m,1)-fx(m,2)))./(2*mu);
end
However, my code is returning all blank answers.
what did I do wrong? Any advice is greatly appreciated!

It is not clear what the matrices agrid2 and fx are, however mu should be computed outside that for loop, since in the formula (1) it is not included in the summation. So, you should first compute mu, and then G.
Furthermore, from your code it seems that inside the function abs() you are considering f instead of a.
Also, I am pretty sure you need a double for loop, since there are two summations in (1).

Related

Matlab SOR Method Implementation

Using an initial approximation of a zero vector and not considering tolerance I have shorten the code to only require 4 arguments. Such that x1 always equals c, and so on by the equation x(k+1)=x(k)T+c.
However the code doesn't seem to produce the correct approximations that you would expect. Does anyone notice where I messed up? Assuming DLU_decomposition(A) returns the correct matrices.
function x = sor2(A,b,omega,kmax)
[D,L,U] = DLU_decomposition(A);
T=inv(D-omega*L)*(((1-omega)*D)+(omega*U));
c= (omega*inv(D-omega*L))*b;
for k=1:kmax,
if(k==1),
x=c;
end
x=T*x+c;
end
norm(A*x-b)
end
Well I can guess all the confusion comes maybe from the multiplications. You need to calculate the matrices elementwise --> use .* instead of the normal *. Would that deliver the correct approximations?

Matlab Matrix Minimization

I have the following matrix
R=(A-C)*inv(A+B-C-C')*(A-C');
where A and B are n by n matrices. I want to find n*n matrix C such that the determinant of R is minimized, SO:
C=arg min (det(R));
Is there any function in MATLAB that can handle this problem?
It seems like you are trying to find the minimum of an unconstrained multivariable function. This can probably be achieved with fminunc
fun = #(x)x(1)*exp(-(x(1)^2 + x(2)^2)) + (x(1)^2 + x(2)^2)/20;
x0 = [1,2];
[x,fval] = fminunc(fun,x0)
Note that there are no examples in the documentation where a matrix is used, this is probably because horrendous performance could be expected when trying to solve this problem for a matrix of any nontiny size. (This is not because of matlab, but because of the nature of the problem).
It is also good to realize that this method does not (cannot) guarantee an optimum, only a local optimum.

matlab differential equation

I have the following differential equation which I'm not able to solve.
We know the following about the equation:
D(r) is a third grade polynom
D'(1)=D'(2)=0
D(2)=2D(1)
u(1)=450
u'(2)=-K * (u(2)-Te)
Where K and Te are constants.
I want to approximate the problem using a matrix and I managed to solve
the similiar equation: with the same limit conditions for u(1) and u'(2).
On this equation I approximated u' and u'' with central differences and used a finite difference method between r=1 to r=2. I then placed the results in a matrix A in matlab and the limit conditions in the vector Y in matlab and ran u=A\Y to get how the u value changes. Heres my matlab code for the equation I managed to solve:
clear
a=1;
b=2;
N=100;
h = (b-a)/N;
K=3.20;
Ti=450;
Te=20;
A = zeros(N+2);
A(1,1)=1;
A(end,end)=1/(2*h*K);
A(end,end-1)=1;
A(end,end-2)=-1/(2*h*K);
r=a+h:h:b;
%y(i)
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
A(2:end-1,2:end-1)=A(2:end-1,2:end-1)+diag(yi);
%y(i-1)
for i=1:1:length(r)-1
ymin(i)=r(i+1)*(1/(h^2))-1/(2*h);
end
A(3:end-1,2:end-2) = A(3:end-1,2:end-2)+diag(ymin);
%y(i+1)
for i=1:1:length(r)
ymax(i)=r(i)*(1/(h^2))+1/(2*h);
end
A(2:end-1,3:end)=A(2:end-1,3:end)+diag(ymax);
Y=zeros(N+2,1);
Y(1) =Ti;
Y(2)=-(Ti*(r(1)/(h^2)-(1/(2*h))));
Y(end) = Te;
r=[1,r];
u=A\Y;
plot(r,u(1:end-1));
My question is, how do I solve the first differential equation?
As TroyHaskin pointed out in comments, one can determine D up to a constant factor, and that constant factor cancels out in D'/D anyway. Put another way: we can assume that D(1)=1 (a convenient number), since D can be multiplied by any constant. Now it's easy to find the coefficients (done with Wolfram Alpha), and the polynomial turns out to be
D(r) = -2r^3+9r^2-12r+6
with derivative D'(r) = -6r^2+18r-12. (There is also a smarter way to find the polynomial by starting with D', which is quadratic with known roots.)
I would probably use this information right away, computing the coefficient k of the first derivative:
r = a+h:h:b;
k = 1+r.*(-6*r.^2+18*r-12)./(-2*r.^3+9*r.^2-12*r+6);
It seems that k is always positive on the interval [1,2], so if you want to minimize the changes to existing code, just replace r(i) by r(i)/k(i) in it.
By the way, instead of loops like
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
one usually does simply
yi=-r*(2/(h^2));
This vectorization makes the code more compact and can benefit the performance too (not so much in your example, where solving the linear system is the bottleneck). Another benefit is that yi is properly initialized, while with your loop construction, if yi happened to already exist and have length greater than length(r), the resulting array would have extraneous entries. (This is a potential source of hard-to-track bugs.)

What is the purpose of the for iteration in the code?

I have a small code like this. Can anyone tell me what is the purpose of the for iteration?
I can see the result difference before and after the for iteration, I just cannot understand what is
the purpose of the for iteration? Thanks a lot!
A = randn(n,m);
for i=1:m
A(:,i) = A(:,i) / norm(A(:,i));
end
It's ensuring that each column in A is normalised to have unit length.
A(:,1) gives you the first column of A, and norm(A(:,1)) gives you the Euclidean norm (or length) of the vector defined by the first column of A.
The reason that you can't do A=A/norm(A) is because Matlab will calculate the matrix norm, not the norm of each column individually.
There is a Matlab builtin function that will do this for you: normc.
Like David said, it's giving each column unit length, and you can't do A/norm(A) for reasons he mentioned, but you can do:
bsxfun(#rdivide, A, sqrt(sum(A.^2,1)))
which is like virtually doing:
A ./ repmat(sqrt(sum(A.^2,1)),size(A,1),1)
On a side note, I would advise against assigning to the same variable while figuring out an expression.

Large Vector Outer Product Matlab

I want to compute an outer product of the same vector in Matlab. A representative example would be:
x=rand(1e5,1);
sigma=x*x'-spdiags(x,0,length(x),length(x));
Is there any obvious way to speed this up? x*x' is a symmetric matrix, but have not figured out a way to help Matlab use that information to speed things up.
EDIT: There is a way to do this with loops but I cannot see the benefit yet:
for k=1:length(x)
sigma(k:length(x),k)=x(k).*x(k:length(x));
end
The above might work with a sparse array.
Have you considered using pdist with custom distance function
sigmaCompact = pdist( x(:), #(x, Y) x.*Y );
sigma = squareform(sigmaCompact);
up to the special treatment of sigma( k, k );