lokal minimum and maximum
조회 수: 8 (최근 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;
댓글 수: 0
채택된 답변
Azzi Abdelmalek
2014년 3월 1일
You can use findpeaks(y) to find maximums, and findpeaks(-y) to find minimums
댓글 수: 0
추가 답변 (2개)
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')
댓글 수: 0
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!