Finding max value of a function in specific range
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
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))
Accepted Answer
0 votes
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 Comments
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
on 29 Sep 2018
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
on 29 Sep 2018
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
on 29 Sep 2018
Which code?copy paste all of it
Alin Brad
on 29 Sep 2018
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
More Answers (0)
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Tags
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)