filling an array with varivbles from a loop
2 views (last 30 days)
Show older comments
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);
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 Comments
Matt J
on 10 Dec 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
on 21 Dec 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
logspace(-1,1,8).'
Answers (1)
SAI SRUJAN
on 21 Dec 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.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!