How do I make this sum loop work to fill select matrix elements?

7 views (last 30 days)
I have a summation loop which is within a nested loop to generate a matrix. I'm not sure how to make it work properly for a start, and then only fill cells which are not on row 1, or column 1.
The code so far is
close all
clear
clc
n = 5;
n_pot = 5;
Vec = [1,1,1,1,1,1,1]
syms k
Sum = sum(Vec,1:n)
Vt = zeros(1,n);
V_sum = zeros(1,n);
A = zeros(n);
for rows = 1:n
for cols = 1:n
for i = 1:n_pot
Vt(i) = Vt(i) + (Vec(i)*(Kron(rows+cols,i) + Kron(abs(cols-rows),i)));
V_sum = Vt(i);
A(rows,cols) = V_sum
end
end
end
function d = Kron(k,j)
if k == j
d = 1;
else
d = 0;
end
end
The Vec vector is only there for ease of manipulation on my part and will be replaced with a regular vector from graph fitting data.
The matrix I'm expecting to get is of the form
Matrix = / / / / /
/ / Vec(1)+Vec(3) Vec(2)+Vec(3) Vec(3)+Vec(5)
/ Vec(1)+Vec(3) / Vec(1)+Vec(5) Vec(2)+Vec(6)
/ Vec(2)+Vec(3) Vec(1)+Vec(5) / Vec(1)
/ Vec(3)+Vec(5) Vec(2)+Vec(6) Vec(1) /
Following this equation, I'm looking to implement the summation portion, and the first term is already worked in.
  1 Comment
Abhishek Gupta
Abhishek Gupta on 15 Feb 2021
Can you provide the following clarifications?
  1. Your output matrix, i.e., A, contains the value for the second term of the equation, right? If so, then Is 'Vec' in the code corresponds to the 'Sn' in the equation? Also, I don't see any (-1/2) term in your code.
  2. What is your final object? Do you want to avoid filling the first row and first column of the matrix?
  3. Do you also want to avoid diagonal cells of the matrix? From '/,' you mean NaN?
If you want to solve the second term of the given equation avoiding m and m' corresponding to the first row, first column, and diagonal cells of the matrix, see the answer below.

Sign in to comment.

Answers (1)

Abhishek Gupta
Abhishek Gupta on 15 Feb 2021
Hi,
As per my current understanding, you want to solve the second term of the equation, avoiding m and m' corresponding to the first row, first column, and diagonal elements of the matrix.
Here is the implementation of the same: -
A = nan(n); % output matrix
for rows = 1:n
for cols = 1:n
% if statement to avoid filling 1st row, 1st column, and diagonal elements
if ~((rows == 1) || (cols == 1) || (rows == cols))
secondTerm = 0; % initialize second term of the equation
% calculate the second term
for i = 1:n_pot
secondTerm = secondTerm + (Vec(i)*(Kron(rows+cols,i) + Kron(abs(cols-rows),i)));
end
A(rows,cols) = (-1/2)*secondTerm;
end
end
end
  1 Comment
Domantas Laurinavicius
Domantas Laurinavicius on 15 Feb 2021
I've since worked it out
H_cos = ZeroBased(zeros(n_basis));
for cols = 0:n_basis
for rows = 0:n_basis
if rows == 0 % Only row 1
H_cos(rows,cols) = V_rows0(cols+1)*(-(2)^(-0.5)); % V_rows0 had to be created otherwise V_vals(1) is cut off and the row is wrong.
% Issue with only the first if statement probably caused by 0 indexed matrix
elseif cols == 0 % Only column 1
H_cos(rows,cols) = V_long(rows)*(-(2)^(-0.5));
elseif rows == cols
H_cos(rows,cols) = (rows^2 + sum(V_long))*Kron(rows,cols) - 0.5*V_long(rows+cols);
else
for i = 1:n_pot
Vt(0) = V_vals(1)*(Kron(rows+cols,0) + Kron(abs(cols-rows),0));
Vt(i) = Vt(i-1) + V_vals(i)*(Kron(rows+cols,i) + Kron(abs(cols-rows),i));
H_cos(rows,cols) = -0.5*Vt(i);
end
H_cos(0,0) = sum(V_vals); % H_cos(0,0) = sum(V_vals) % Necessary to have an exception statement for 0,0
end
end
end
H_cos
This works for now, but the ZeroBased() func is incompatible with eig() so I'll have to fix that.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!