central difference A matrix - matlab

Hi im trying to code the following coefiicient matrix A in a for loop in Matlab. N is the total number of elements. And i and j are obtained from k. Could someone help me please code the A matrix correctly for any N value. The A and b are shown
for k = 1:N
A(k,k) = 4; % the diagonal element, corresponding to Tij, is always 4
% In the following, we look at the four neighbours of (i,j) that are
% involved in the 5-point formula
i = mod(k-1,nx)+1;
j = (k-i)/nx+1; %get (i,j) from k
%boundary condtion
if i == 1
b(k,1) = Tl;
elseif i == nx
b(k,1) = Tr;
elseif j == 1
b(k,1) = Tb;
elseif j == ny
b(k,1) = Tt;
end
%A matrix construction
if i>1 && j>1
if i == j
A(j-1,i) = -1;
A(j,i+1) =- 1;
A(j,i-1) = -1;
A(j+1,i) = -1;
end
end
end

Related

NULLSPACE - RREF Command in Matlab bugs

Can someone help me debug this Matlab code? Like I have a matrix A, and I need to find A^k by diagonalization method. Below is my original code, but there is a problem with this part:
m = (M - R(i,1)*eye(NumRowsR));
disp(rref(m));
t = null(rref(m));
disp(t);
This part can't give me the nullspace of matrix t after reduced for some reasons (I recently did some research on the Internet and see some people said about the bug of rref and null). The problem is that it keeps showing me elementary matrix
1 0 0
0 1 0
0 0 1
for the eigenvalues. Does anyone know how to fix it? I will be very much appreciated!
Below is my original code, for a better view:
syms x NumRowsM NumColsM NumRowsR NumColsR NumRowst NumColst k numeigen
M = input('Input a square matrix M: ');
k = input('Input the power of matrix M: ');
[NumRowsM, NumColsM]=size(M);
if (NumRowsM ~= NumColsM)
disp('Not valid input. Matrix must be a square matrix!')
else
%Find eigenvalues:
R = solve(det(M - x*eye(NumRowsM)), x);
[NumRowsR, NumColsR] = size(R);
if (or(NumRowsR == 0,NumColsR == 0))
disp('No eigenvalues. The matrix is not diagonalizable')
else
numeigen = 0;
F = zeros(NumRowsR, NumRowsR);
d = zeros(NumRowsR,1);
for i = 1:NumRowsR
m = (M - R(i,1)*eye(NumRowsR));
disp(rref(m));
t = null(rref(m));
disp(t);
[NumRowst, NumColst] = size(t);
if (NumColst == 0)
if (i == NumRowsR && numeigen > NumRowsR)
disp('Matrix not diagonalizable due to invalid eigenvalue');
return
else
continue
end
else
numeigen = numeigen + 1;
if (NumColst == 1)
for j = 1:NumRowsR
[n, d(j)] = numden(sym(t(j,1)));
end
for j = 1:NumRowsR
F(j,i) = sym(t(j,1)) * lcm(sym(d));
end
else
for k = 1:NumColst
for j = 1:NumRowsR
[n, d(j)] = numden(sym(t(j,k)));
end
for j = 1:NumRowsR
F(j,k) = sym(t(j,k)) * lcm(sym(d));
end
end
end
end
end
disp(F);
D = (F\M)*F;
disp('The power k of the matrix M is: ');
T = F*(D^k)/(F);
disp(T)
end
end

Nested for loop error or indexing error in MATLAB

I have created this code from scratch. I want to make a plot and/or histogram of my "Observed" and "State" (these are 2 matrices). Some problem occurs at the 200th iteration, where my State matrix just becomes all 0's, there is no data being input into the State matrix. Can anyone troubleshoot the code? My possible states are {1,2,3}.
UPDATE:
When I adjust my n value, it adjusts how much of length T it will fill. So, n=5, only runs for 1/5 of T and n=1, run for entire length of T. I need an nxT matrix at the end (5X1000). The problem lies in the way I setup my for loops.
I still cannot solve the error though.
%Initialize A,pi,T
N = 3; # of states
%A is transition prob matrix
A = [.99,.005,.005;.005,.990,.005;.005,.005,.990];
%pi is initial state vector
pi = [1/3,1/3,1/3];
%T is # of observations per simulation
T = 1000;
%n is # of simulations
n = 5;
%Allocate space for the state matrix
State = zeros(n,T);
Observe = zeros(n,T);
%Create dummy emission matrix, must be row stochastic
B = ones(n,T)./T;
%loop over # of simulations
for i=1:1:n
x = rand(1);
if x <= (1/3)
State(i,1) = 1;
elseif x > (1/3) && x <= (2/3)
State(i,1) = 2;
else
State(i,1) = 3;
end
if State(i,1) == 1
b = -1;
elseif State(i,1) == 2
b = 0;
else
b = 1;
end
Observe(i,1)= normrnd(b,1);
for k=2:1:T
%Possible state 1,2,3
State(k) = randsample(N, 1, true, A(State(k-1),:));
if State == 1
c = -1;
elseif State == 2
c = 0;
else
c = 1;
end
Observe(i,k)= normrnd(c,1);
end
end
State(k) = randsample(N, 1, true, A(State(k-1),:));
This line is missing index (i) in position 1 inside State(k-1). It should be:
State(i,k) = randsample(N, 1, true, A(State(i,k-1),:));

Time Complexity Analysis Of A Recursion

The following code is calculating the determinant using recursion.
For the "for" loop with have O(n) then we call the function again with n-1 elements so we have to multiply each time we call the function?
something like O(n)O(n-1)...*O(1)?
function y = detm(A)
n = length(A);
y = 0;
if n == 1
y = A(1,1);
elseif n == 2
y = A(1,1).*A(2,2)-A(1,2).*A(2,1);
elseif n > 2
for i = 1:n
temp = A(2:end,:);
temp(:,i) = [];
if mod(i,2) == 0
y = y - A(1,i)*detm(temp);
else
y = y + A(1,i)*detm(temp);
end
end
end
end

Jacobi method to solve linear systems in MATLAB

How would you code this in MATLAB?
This is what I've tried, but it doesn't work quite right.
function x = my_jacobi(A,b, tot_it)
%Inputs:
%A: Matrix
%b: Vector
%tot_it: Number of iterations
%Output:
%:x The solution after tot_it iterations
n = length(A);
x = zeros(n,1);
for k = 1:tot_it
for j = 1:n
for i = 1:n
if (j ~= i)
x(i) = -((A(i,j)/A(i,i)) * x(j) + (b(i)/A(i,i)));
else
continue;
end
end
end
end
end
j is an iterator of a sum over each i, so you need to change their order. Also the formula has a sum and in your code you're not adding anything so that's another thing to consider. The last thing I see that you're omitting is that you should save the previous state of xbecause the right side of the formula needs it. You should try something like this:
function x = my_jacobi(A,b, tot_it)
%Inputs:
%A: Matrix
%b: Vector
%tot_it: Number of iterations
%Output:
%:x The solution after tot_it iterations
n = length(A);
x = zeros(n,1);
s = 0; %Auxiliar var to store the sum.
xold = x
for k = 1:tot_it
for i = 1:n
for j = 1:n
if (j ~= i)
s = s + (A(i,j)/A(i,i)) * xold(j);
else
continue;
end
end
x(i) = -s + b(i)/A(i,i);
s = 0;
end
xold = x;
end
end

Merge result after for function

Good morning everybody
I work to develop mathematical model to solve one of the industrial engineering problem but I have a problem in the write of the MATLAB code. So I simplified this problem in the following code. I need to merge all the result of X in one matrix after the for function used to use it in the next step (in this simple case this matrix will be 40*3)
LIST=randi([0,1],[4,3]);
for i = 1:10
j=i
V=randi([0,1],[4,3]);
for m = 1:4
for n = 1:2
if V(m,n)== 1;
X(m,n) = LIST(m,n);
elseif V(m,n)== 0;
X(m,n) = 2;
end
end
end
for m = 1:4
for n = 3
if V(m,n)== 1;
X(m,n) = LIST(m,n);
elseif V(m,n)== 0;
X(m,n) = 3;
end
end
end
X
end
Thank you for your time and your consideration
At each iteration of the outer loop (for i = 1:10) the values in the X matrix are overwritten.
In order to store all the values you need to increment the row value of the X matrix of the value of max limit of the second loop (for m = 1:4) that is 4.
You can do it by modifying the indexing of the X matrix as follows:
X(m+(i-1)*4,n)
You can make your script more "general" by identifying the limit evaluating it as the number of rows in the LIST matrix using the function size function:
[n_row,n_col]=size(LIST)
In this way
at the first iteration of the outer loop, the row index will range from 1 to 4.
at the second iteration itr will range from 1+(2-1)*4 to 4+(2-1)*4 that is from 5 to 8
and so on
This is the updated code
LIST=randi([0,1],[4,3]);
[n_row,n_col]=size(LIST)
for i = 1:10
j=i
V=randi([0,1],[4,3]);
% for m = 1:4
for m = 1:n_row
for n = 1:2
if V(m,n)== 1;
% X(m,n) = LIST(m,n);
X(m+(i-1)*n_row,n) = LIST(m,n);
elseif V(m,n)== 0;
% X(m,n) = 2
X(m+(i-1)*n_row,n) = 2;
end
end
end
% for m = 1:4
for m = 1:n_row
for n = 3
if V(m,n)== 1;
% X(m,n) = LIST(m,n)
X(m+(i-1)*n_row,n) = LIST(m,n);
elseif V(m,n)== 0;
% X(m,n) = 3;
X(m+(i-1)*n_row,n) = 3;
end
end
end
X
end
Hope this helps.
Qapla'