Finding max value of a function in specific range
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Dear All, I wrote this code to find the max value of function y within the (-0.5,0.5) range of x, however, it returns a erroe message (Warning: Integer operands are required for colon operator when used as index Subscript indices must either be real positive integers or logicals.) It looks not possible to use negative or non-integer values as indices. How can find the max value within the required range. Thanks
f = -8:0.002:8;
y=(1./((f.^2)+1))-(1.5./((f+0.9).^2+1))-1.5./((f-0.9).^2+1);
yn=y./max(y);
[pks, locs] = findpeaks (yn(-0.5: 0.5))
採用された回答
yn(-0.5: 0.5)
This line returns the values of yn of the index in the braces. Problem is that those are not valid indices, as they need to be integers. If you need to return the values of yn where f>-.5 and f>.5 then you can use logical indexing instead
yn(f>-0.5 & f<0.5)
If you look at the data in this range, then you see that it's a "negative peak", or valley. To find this type of peaks, you need to use the findpeaks function as follows:
[pks, locs] = findpeaks (-1.*yn(f>-0.5 & f<0.5));
to get the actual peak value, we need to multiply the peak values by -1 again
pks=pks.*-1;
In my opinion, the better way would be to use both arrays as input
[pks, fx] = findpeaks (-1.*yn,f)
You can then select the peaks of interest
pks(fx>-.5 & fx<.5).*-1
9 件のコメント
NO, I need the values of y within f range(-0.5,0.5) It does not work also! I got this message Error using findpeaks Expected X to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64
Write out the values by the exact line I gave you. Perhaps it is empty.
What do you mean by x, do you mean f?
This is probably what you are looking for:
[pks, locs] = findpeaks (yn(f>-0.5 & f<0.5))
However, if you want to find "negative peak", then
[pks, locs] = findpeaks (-1.*yn(f>-0.5 & f<0.5))
pks=pks.*-1;
Note also that the index returned refers to the indexed values.
Alin Brad
2018 年 9 月 29 日
sorry, yes f

Now it gives me the values pks =-24.9716 and locs = 250, however ccording to plot should be another values?
That is a plot of y, not yn. Are you looking to find the peaks in y or yn??
Locs refers to the index, you need to use the second method in my updated answer to get the value of f corresponding to the peak of interest.
f = -8:0.002:8;
y=(1./((f.^2)+1))-(1.5./((f+0.9).^2+1))-1.5./((f-0.9).^2+1);
yn=y./max(y);
[pks, fx] = findpeaks (-1.*yn,f)
pks(fx>-.5 & fx<.5).*-1
plot(f,yn)
fx =
0
ans =
20.233542462092245
just change yn to y if you are looking to find the local minimum in y.
The absolute best way would probably be to use function handles instead.
y = @(f) (1./((f.^2)+1))-(1.5./((f+0.9).^2+1))-1.5./((f-0.9).^2+1);
x=-0.05:0.001:0.05;
[pks,xlocs]=findpeaks(y(x),x)
Alin Brad
2018 年 9 月 29 日
When i run your code I got this message,
Error using uddpvparse (line 122) Invalid Parameter/Value pairs.
Error in findpeaks>parse_inputs (line 84) hopts = uddpvparse('dspopts.findpeaks',varargin{:});
Error in findpeaks (line 59) [X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(X,varargin{:});
jonas
2018 年 9 月 29 日
Which code?copy paste all of it
Alin Brad
2018 年 9 月 29 日
i used your code
f = -8:0.002:8;
y=(1./((f.^2)+1))-(1.5./((f+0.9).^2+1))-1.5./((f-0.9).^2+1);
yn=y./max(y);
[pks, fx] = findpeaks (-1.*yn,f)
pks(fx>-.5 & fx<.5).*-1
plot(f,yn)
Hmm, maybe you are using an older version of findpeaks.. Try this instead:
y = @(f) (1./((f.^2)+1))-(1.5./((f+0.9).^2+1))-1.5./((f-0.9).^2+1);
fx=-0.05:0.001:0.05;
[pks,id]=findpeaks(y(fx))
fval=fx(id)
This was written on my phone so there could be typo. If the peak is
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Descriptive Statistics についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
