Creating a Matrix Output from Element Integration Inside Matrix
14 views (last 30 days)
Show older comments
EDIT:
I made an edit to the description to clarify what I am trying to do.
Hello everyone,
I am trying to output a square matrix that is N x N in size. In my case, N = 3.
I have a variable that's called "Kernel". This variable is calculated as an equation in terms of "z" and "z_prime".
My values in z_prime will vary from L / 2N to (2N-1)*(L/2N) , in steps of "L/N".
My "z" variable will be used as an "integration variable" as seen below.
I am trying to calculate an output called the "Z Matrix" which will integrate the "Kernal" variable across specific ranges.
I attached a .pdf file to clarify my end goal.
See the .pdf file attached:
Z_Matrix.pdf
I attached my MATLAB Code for reference.
See MATLAB .m file attached:
Z_Calculation.m
In my attempt, I have tried to use for loops and creating function handles to make it easier to generate this matrix.
I know that there is a way to use "nested" for loops to generate a square matrix of an "N X N" size. However, I wasn't quite sure on how to implement that in MATLAB for my case.
I have tried to look through different questions/answers on the Mathworks Forum regarding square matrices. However, I wasn't able to find anything that was relevant to my case.
%I'm thinking of attempting something like this
% NOTE: This is not all correct MATLAB syntax, this is mostly just logic written out/pseudocode
% I am trying to see how I can implement this by using nested for loops
% Values for N and L
N = 3;
% Since this will be a square matrix, it will be an "M X N" matrix.
M = N;
L = 1/2;
% Range for z_prime
z_prime_start = L / (2*N);
delta_z_prime = L / N;
z_prime_end = (2N-1)*z_prime_start;
z_prime = z_prime_start:delta_z_prime:z_prime_end;
%{
The Ranges for the "z" variable that will be used to
integrate the "Kernel" variable
%}
z_start = 0;
delta_z = L/N;
z_end = L/N;
% Each "Row" will have a specific "z_prime value"
% Row 1 = Kernal(z,z_prime_1)
for i = 1:N
for j = 1:M
loop_z_prime = z_prime(i,j)
Kernal(i,j) = Kernal(z,loop_z_prime)
Z(i,j) = integral(Kernal(i,j),z_start,z_end)
end
end
% The "z" boundaries for integration will keep increasing by "L/N" until
% the very last element in the "Z" matrix.
1 Comment
dpb
on 28 Jul 2024
"showing the type of matrix I am trying to generate"
By that graphic, each row is a repitition of the same value for however many columns...each cell in each row is
K(z,z_prime_N)
where N in z_prime_N is 1, 2, 3, ... but is the same for all columns in each row 1:N and you've said above that z is fixed. Hence, so will be K for each n in 1:N
Something's missing.
Per usual show us and actual numeric case and cut the size down to, say, 5x5; enough to demonstrate the algorithm and input/outputs,but not too much to look at on a screen.
Answers (1)
dpb
on 28 Jul 2024
Rereading the Q? title, I guess the above supposition I thought an error is actually what you're looking to do.
If that is the case, then the simple thing is just
N=23; % set the desired size
L=???; % set L
z=???; % and z
z_p=[L/(2*N):L/N:(2N-1)*(L/2N); % compute z prime vector
M=K(z,z_p); % and evaluate K for inputs (assume vectorized, if not, use explicit loop)
M=[M repmat(M,1,N-1); % add replicated columns to the desired size...
The last uses a key feature of MATLAB -- automagic reallocation on demand. The [] puts the two pieces, the vector M and the (N-1) copies of it together into a new M.
2 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!