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)
Afficher commentaires plus anciens
mohammed
le 23 Nov 2024
Commenté : Walter Roberson
le 24 Nov 2024
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)')
0 commentaires
Réponse acceptée
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)
plot(t,y)
4 commentaires
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
Plus de réponses (0)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!