lokal minimum and maximum

4 views (last 30 days)
Rasmus
Rasmus on 1 Mar 2014
Answered: Image Analyst on 2 Mar 2014
I want to find the lokal minimum, and mark it in the plot. I succed in the first go, but in the second, not so good. For somereason it doesnt agree with me. Here is the code i type - I hope anyone can help me.
function opgave31
disp('opgave a')
title('a') x=(-5:5);
f=funktion(x);
plot(x,f)
hold on
y_ny=zeros(1,length(x));
plot(x,y_ny,'r')
x1=fzero(@funktion,-4); x2=fzero(@funktion,1); x3=fzero(@funktion,2);
plot(x1,y_ny,'b*',x2,y_ny,'b*',x3,y_ny,'b*')
close all
disp('opgave b')
title('b')
s=funktion2(x);
plot(x,s)
min=fminsearch(@funktion2,1,5)
max=fminsearch(@(x) -funktion2(x),-5,5)
x5=fzero(@funktion2,min); x6=fzero(@funktion2,max);
close all
plot(x,s,'r-',min,y_ny,'b*')
function y=funktion(x)
y=x.^3+x.^2-10*x+8
function t=funktion2(x)
t=x.^3-6*x.^2-x+30;

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 1 Mar 2014
You can use findpeaks(y) to find maximums, and findpeaks(-y) to find minimums

More Answers (2)

Mischa Kim
Mischa Kim on 1 Mar 2014
Edited: Mischa Kim on 1 Mar 2014
Hello Rasmus, how about doing it symbolically?
syms x
t = x^3 - 6*x^2 - x + 30;
rootsdt = double(solve(diff(t)==0));
dt2 = diff(t,2);
if subs(dt2,rootsdt(2))>0
x = rootsdt(2);
else
x = rootsdt(1);
end
ezplot(t)
hold on
plot(x, subs(t),'rs')

Image Analyst
Image Analyst on 2 Mar 2014
If you don't have the Signal Processing Toolbox, which is where findpeaks() is located, then, if you have the Image Processing Toolbox, you can use imdilate() to find local maximum and imerode() to find local minimums. They're different than findpeaks. findpeaks() finds peaks, it does not find local maxima. So if you have a peak, like a Gaussian, it will find the very tip top apex of the hump. Of course there are local maximum all along the curve, even going down the sides of the hump, right? The imdilate() function is a local max in a window that slides along your signal so it will get the local max at every location, not just at a peak.

Community Treasure Hunt

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

Start Hunting!