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))

 Accepted Answer

jonas
jonas on 29 Sep 2018
Edited: jonas on 29 Sep 2018
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.
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)
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{:});
Which code?copy paste all of it
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

Sign in to comment.

More Answers (0)

Asked:

on 29 Sep 2018

Edited:

on 29 Sep 2018

Community Treasure Hunt

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

Start Hunting!