How can I plot slope including maximum peaks?

Hello, How can I plot slope including peaks point? It should look like exp. I tried envelope but It does not work for my code.
Thank you.

 Accepted Answer

If you have the Signal Processing Toolbox, use the findpeaks function to define the peaks and their locations. You can then use those values directly in your exponential regression.
EDIT
Try this:
y = [12 14 2 8 9 4 6 7 9 5 6];
x = 1:length(y);
ym = mean(y);
yz = y - ym; % Create Symmetry About Mean
[pospks, poslocs] = findpeaks(yz); % Positive Peaks & Locations
[negpks, neglocs] = findpeaks(-yz); % Negative Peaks & Locations
pkvct = [pospks negpks]; % Combine In One Vector FOr Sorting
[locs,idx] = sort([poslocs neglocs]); % Sort
pks = pkvct(idx);
objfcn = @(b,x) b(1).*exp(b(2).*x); % Exponential Objective Function
[B,resnorm] = fminsearch(@(b) norm(pks - objfcn(b,locs)), rand(2,1)); % Fit Data (EStimate Parameters)
figure(1)
plot(x, y, '-g')
hold on
% plot(locs, pks+ym, 'pg')
plot(locs, objfcn(B,locs)+ym, '-r')
hold off
text(5, 13, sprintf('y(x) = %.3f + e^{%.3f\\cdotx} + %.3f', B, ym))

2 Comments

Thank you for answer. My graph is changing when I run the Matlab.
My pleasure.
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (1)

Why would you try envelope()? diff() would give the slope
slopes = diff(y)

Tags

Community Treasure Hunt

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

Start Hunting!