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.

2 ビュー (過去 30 日間)
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)')

採用された回答

Walter Roberson
Walter Roberson 2024 年 11 月 23 日
移動済み: Walter Roberson 2024 年 11 月 23 日
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 件のコメント
mohammed
mohammed 2024 年 11 月 24 日
編集済み: mohammed 2024 年 11 月 24 日
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 2024 年 11 月 24 日
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>

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by