- Concatenation - https://www.mathworks.com/help/matlab/math/creating-and-concatenating-matrices.html
- Matrices and Arrays - https://www.mathworks.com/help/matlab/matrices-and-arrays.html
Storing output values from a for loop into an "array"
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello everyone,
I am trying to output the values that I get from a for loop into an "array". I am not sure how to store those "list of values" into an array
I have attached my code in reference:
Storing_Values_Into_Array.m
close all; clc;
% Value of j
j = 1i;
% For the region k_o < k_p < sqrt(u_r*e_r)*k_o
% Wavenumber in free space value = k_o
k = 2*pi;
% Permeability Value = u_r
u_r = 1;
% Permittivity Value e_r
e_r = 2.2;
% Value of kz_1
kz_1 = @(kp) sqrt((k).^2*e_r*u_r - (kp).^2);
% Value of kz_2
kz_2 = @(kp) sqrt((kp).^2 - (k).^2);
% Substrate heights = d
D = [0.02 0.04 0.06 0.08 0.10];
% Using a for loop to calculate the roots for T_M
% Solving for k_p values
for i=1:numel(D)
d=D(i);
% Equations for TM(kp)
T_M = @(kp) kz_1(kp).*sin(d.*kz_1(kp)) - e_r*kz_2(kp).*cos(d.*kz_1(kp));
kp_root=[fzero(T_M, [k , k*sqrt(e_r*u_r)])]
fplot(T_M,[k k*sqrt(e_r*u_r)], 'LineWidth',3); hold on
plot(kp_root,0,'o','MarkerSize',8,'MarkerFaceColor','k');
ylim([-5 5]);
xlim([k k*sqrt(e_r*u_r)]);
title('T_M vs k_p')
ylabel('T_M')
xlabel('k_p')
grid on;
ax = gca;
ax.GridLineWidth = 2;
end; hold off
%{
What I am trying to do is store the values I get from "kp_root" in an
array called "kp_list" so I can use that array for calculations later on.
%}
0 Kommentare
Akzeptierte Antwort
Ayush Modi
am 18 Jul. 2024
Bearbeitet: Ayush Modi
am 18 Jul. 2024
Hi Ammar,
I am assuming you want to append the kp_root values you obtain in each iteration of for loop and store them in the variable kp_list. You can achieve it by concatenation.
Here is code snippet for your reference -
% Initialize an empty array kp_list before the for loop
kp_list = []
% After you get the value of kp_root in for loop, concatenate kp_root to kp_list
kp_root=[fzero(T_M, [k , k*sqrt(e_r*u_r)])]
kp_list = [kp_list kp_root]
For more information refer to the following MathWorks documentation:
Weitere Antworten (1)
Vandit
am 18 Jul. 2024
Hello Ammar,
To store the values from "kp_root" in an array called "kp_list", you can initialize an empty array before the for loop and then append each value of "kp_root" to the array inside the loop. Here's the updated code with the "disp" command at the end to display the values stored in the "kp_list" array:
close all; clc;
% Value of j
j = 1i;
% For the region k_o < k_p < sqrt(u_r*e_r)*k_o
% Wavenumber in free space value = k_o
k = 2*pi;
% Permeability Value = u_r
u_r = 1;
% Permittivity Value e_r
e_r = 2.2;
% Value of kz_1
kz_1 = @(kp) sqrt((k).^2*e_r*u_r - (kp).^2);
% Value of kz_2
kz_2 = @(kp) sqrt((kp).^2 - (k).^2);
% Substrate heights = d
D = [0.02 0.04 0.06 0.08 0.10];
% Initialize kp_list array
kp_list = [];
% Using a for loop to calculate the roots for T_M
% Solving for k_p values
for i = 1:numel(D)
d = D(i);
% Equations for TM(kp)
T_M = @(kp) kz_1(kp).*sin(d.*kz_1(kp)) - e_r*kz_2(kp).*cos(d.*kz_1(kp));
kp_root = fzero(T_M, [k , k*sqrt(e_r*u_r)]);
kp_list = [kp_list, kp_root]; % Append kp_root to kp_list
fplot(T_M, [k k*sqrt(e_r*u_r)], 'LineWidth', 3); hold on
plot(kp_root, 0, 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'k');
ylim([-5 5]);
xlim([k k*sqrt(e_r*u_r)]);
title('T_M vs k_p')
ylabel('T_M')
xlabel('k_p')
grid on;
ax = gca;
ax.GridLineWidth = 2;
end
hold off
disp(kp_list);
In the above code snippet, an empty array "kp_list" is initialized before the loop, and the roots found in each iteration(kp_root) are appended to "kp_list" inside the loop so that "kp_list" will contain all the roots after the loop.
To know more about Matrices and Arrays in MATLAB, you can refer to the following documentation:
Hope this helps.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!