I am trying to store all the values ​​of (f) and all the corresponding peaks of the resulting wave in two columns and then plot f vs. peaks, but there is a problem.

1 vue (au cours des 30 derniers jours)
clc
clear all
tspan = [0 5];
Ic = 0;
A=1;
results = [];
% Simulate using ode45 in a loop
for f = 2:6
signal = @(t, y) A*2*pi*f*cos(2*pi*f*t);
[t, y] = ode45(signal, tspan, Ic);
pks = findpeaks(y(:,1),'minpeakHeight',0.99);
results = [results; f; pks];
end
plot(results(:,1),results(:,2),'bo-')
xlabel('f (Hz)')
ylabel('peaks (cm)')

Réponse acceptée

Walter Roberson
Walter Roberson le 23 Nov 2024
Déplacé(e) : Walter Roberson le 23 Nov 2024
findpeaks() is returning different numbers of peaks for each frequency.
clc
clear all
tspan = [0 5];
Ic = 0;
A=1;
results = {};
% Simulate using ode45 in a loop
for f = 2:6
signal = @(t, y) A*2*pi*f*cos(2*pi*f*t);
[t, y] = ode45(signal, tspan, Ic);
pks = findpeaks(y(:,1),'minpeakHeight',0.99);
results{end+1} = {f, pks};
end
celldisp(results)
results{1}{1} = 2 results{1}{2} = 0.9985 0.9988 0.9992 0.9941 0.9976 results{2}{1} = 3 results{2}{2} = 0.9985 0.9988 0.9992 0.9941 0.9976 0.9951 0.9970 0.9984 results{3}{1} = 4 results{3}{2} = 0.9985 0.9988 0.9992 0.9941 0.9976 0.9951 0.9970 0.9984 0.9987 0.9992 0.9941 results{4}{1} = 5 results{4}{2} = 0.9985 0.9988 0.9992 0.9941 0.9976 0.9951 0.9970 0.9984 0.9987 0.9992 0.9941 0.9975 0.9953 0.9971 0.9984 results{5}{1} = 6 results{5}{2} = 0.9985 0.9988 0.9992 0.9941 0.9976 0.9951 0.9970 0.9984 0.9987 0.9992 0.9941 0.9975 0.9953 0.9971 0.9984 0.9987 0.9992
plot(t,y)
  4 commentaires
mohammed
mohammed le 24 Nov 2024
Modifié(e) : mohammed le 24 Nov 2024
Good job, but I wished f and pks were collected in two columns (two vectors), I think it required to be extracted from the cells into two columns.
Walter Roberson
Walter Roberson le 24 Nov 2024
The different f have different number of pks . You cannot extract the pks into a numeric array -- not unless you pad unused columns with NaN or inf.
tspan = [0 5];
Ic = 0;
A=1;
f_vector = [];
pks_array = [];
% Simulate using ode45 in a loop
f_list = 2:6;
for fidx = 1:length(f_list)
f = f_list(fidx);
signal = @(t, y) A*2*pi*f*cos(2*pi*f*t);
[t, y] = ode45(signal, tspan, Ic);
pks = findpeaks(y(:,1),'minpeakHeight',0.99);
f_vector(fidx) = f;
pks_array(1:fidx-1,end+1:length(pks)) = nan;
pks_array(fidx,1:length(pks)) = pks;
pks_array(fidx,length(pks)+1:end) = nan;
end
plot(f_vector, pks_array.', 'bo')
pks_array
pks_array = 5×17
0.9985 0.9988 0.9992 0.9941 0.9976 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.9985 0.9988 0.9992 0.9941 0.9976 0.9951 0.9970 0.9984 NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.9985 0.9988 0.9992 0.9941 0.9976 0.9951 0.9970 0.9984 0.9987 0.9992 0.9941 NaN NaN NaN NaN NaN NaN 0.9985 0.9988 0.9992 0.9941 0.9976 0.9951 0.9970 0.9984 0.9987 0.9992 0.9941 0.9975 0.9953 0.9971 0.9984 NaN NaN 0.9985 0.9988 0.9992 0.9941 0.9976 0.9951 0.9970 0.9984 0.9987 0.9992 0.9941 0.9975 0.9953 0.9971 0.9984 0.9987 0.9992
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by