lokal minimum and maximum

7 次查看(过去 30 天)
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;

采纳的回答

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

更多回答(2 个)

Mischa Kim
Mischa Kim 2014-3-1
编辑:Mischa Kim 2014-3-1
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 2014-3-2
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!

Translated by