find the peak of a function in matlab

To find the peak of a function in Matlab, you can use the findpeaks function which is part of the Signal Processing Toolbox. This function returns the location and magnitude of all local maxima in the input signal.

Here's an example code:

main.m
% define the input function
x = linspace(0,5*pi,100);
y = sin(x).*exp(-x/5);

% find the peaks
[peak_mags, peak_locs] = findpeaks(y,x);

% plot the function and the peaks
plot(x,y)
hold on
plot(peak_locs, peak_mags, 'o')
hold off
230 chars
13 lines

In this example, we define a function y and use linspace to generate input values for the x variable. We then apply the sin and exp functions to generate the y-values. Finally, we pass these into findpeaks and plot the function together with the identified peaks.

gistlibby LogSnag