Borrar filtros
Borrar filtros

filling an array with varivbles from a loop

1 visualización (últimos 30 días)
Luke Marchisin
Luke Marchisin el 10 de Dic. de 2023
Comentada: Voss el 21 de Dic. de 2023
I need to fill 2 arrays with the values magnitude_db and phase_deg gained from a loop. however when i run the code it just fills the entire array with the last value. any help would be greatly appreciated.
A=.1;
omega=logspace(.1,10);
t = 0:0.01:500;
u=sin(A*t);
y = eval_system(u,t);
Unrecognized function or variable 'eval_system'.
phaseArray=[1,50];
for i=1:50
f=omega(i);
[magnitude_dB, phase_deg] = compute_mag_phase(t,u,y,f);
magArray(1,i)=magnitude_dB;
phaseArray(1,i)=phase_deg;
end
  3 comentarios
Matt J
Matt J el 10 de Dic. de 2023
The problem doesn't come from any of the code you've shown us. It comes from whatever eval_system and compute_mag_phase are doing.
Voss
Voss el 21 de Dic. de 2023
@Luke Marchisin: Note that logspace(a,b) generates values between 10^a and 10^b, so logspace(0.1,10) gives you values between 10^0.1 and 10^10. Perhaps you meant to say logspace(-1,1), which would give you values between 10^-1=0.1 and 10^1=10.
format long g
logspace(0.1,10,8).' % generating only 8 values, for brevity of display
ans = 8×1
1.0e+00 * 1.25892541179417 32.6802758941013 848.342898244073 22022.0194998737 571666.650191362 14839817.8896756 385224842.003675 10000000000
logspace(-1,1,8).'
ans = 8×1
0.1 0.193069772888325 0.372759372031494 0.719685673001152 1.38949549437314 2.68269579527973 5.17947467923121 10

Iniciar sesión para comentar.

Respuestas (1)

SAI SRUJAN
SAI SRUJAN el 21 de Dic. de 2023
Hi Luke,
I understand that you are facing an issue while trying to fill two arrays 'magArray' and 'phaseArray' with the magnitude in decibels and phase in degrees, respectively, for each frequency in the 'omega' array. You are using a function 'compute_mag_phase' to calculate the magnitude and phase for each frequency.
Note that 'phaseArray=[1,50]' generates a vector with dimensions 1 x 2, where the initial element is 1 and the subsequent element is 50. This does not reshape 'phaseArray' into an array with dimensions 1 x 50.
The error message "Unrecognized function or variable 'eval_system' " indicates that the function 'eval_system' is not defined in your MATLAB path or current workspace. You need to define this function or ensure it is added to the path.
In the code snippet you provided, observe the following line:
[magnitude_dB, phase_deg] = compute_mag_phase(t,u,y,f);
It is evident that 'f' is the sole variable that varies with each iteration. Therefore, it is crucial to verify that the function 'compute_mag_phase' is properly designed to yield distinct values corresponding to the different 'f' values, as expected.
Here is the corrected version of your code with proper initialization of the arrays and a placeholders for the 'eval_system' and 'compute_mag_phase' functions, which you will need to define:
A = 0.1;
omega = logspace(0.1, 10, 50);
t = 0:0.01:500;
u = sin(A * t);
y = eval_system(u, t); % Define 'eval_system' function
% Initialize arrays to hold 50 elements each
magArray = zeros(1, 50);
phaseArray = zeros(1, 50);
for i = 1:50
f = omega(i);
[magnitude_dB, phase_deg] = compute_mag_phase(t, u, y, f); % Define 'compute_mag_phase' function
magArray(1,i) = magnitude_dB; % Store magnitude in dB
phaseArray(1,i) = phase_deg; % Store phase in degrees
end
I hope this helps.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by