Creating a Matrix Output from Element Integration Inside Matrix

20 次查看(过去 30 天)
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 个评论
dpb
dpb 2024-7-28
"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.

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2024-7-28
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 个评论
Ammar
Ammar 2024-7-28
Hi dpb,
Thank you for the quick response. I appreciate the insight on this.
I guess I should've just clarified what my "end goal" was from the beginning.
The reason why I'm trying to generate a "matrix" for my "K" variable is so that I can perform an integration on each individual element.
See the .pdf file attached:
Z_Matrix.pdf
See my MATLAB code attached for reference:
Z_Calculation.m
Essentially, objective is to calculate a "Z Matrix" that integrates the "K" variables across different ranges for different elements.
% Assuming:
N = 3;
L = 1/2;
z_prime_start = L / (2*N);
delta_z_prime = L / N;
z_prime_end = ( (2*N) - 1 ) / z_prime_start
z_prime = z_prime_start:delta_z:z_prime_end
z_start = 0;
delta_z = L/N;
z_end = L;
%{
What I am thinking of doing is using a for loop for the "z" variables to
keep "increasing" the boundaries by "L/N", since I eventually want to reach
"L"
%}

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by