Finding the maxima/minima of a function.

50 ビュー (過去 30 日間)
Christopher
Christopher 2013 年 9 月 6 日
コメント済み: KADALI 2023 年 1 月 3 日
My lab TA assigned a small project to find and plot the absolute value of the maxima and minima of a given function. I made a quick and dirty algorithm to do so, but I'm having two issues with it and I simply cannot figure out what is wrong with my code.
1.) For some reason, it isn't grabbing the actual highest value, but rather one close to it.
2.) The max/min plot dips down to nearly zero and then climbs up steadily with the Absolute valued function.
I know there are other ways of doing it, including using the derivative of the function, but I would much rather assistance in finding out what is incorrect in my algorithm, which tests surrounding points in order to find maxima and minima.
Here is the code:
________________
%This program plots the abs val of the maxima and minima of a function.
%This max/min value will then continue to be plotted until a new maxima or
%minima is found.
clear,clc;
t=[0:0.1:20];
y=exp(-t).*sin(pi./2.*t); %Our Function
yAbs=abs(y); %Take the absolute value of the function.
yMaxMin=zeros(201); %Create an array of zeros to be filled w/ data.
for(i=2:200)
%If a point is a maxima in yAbs, it will be a maxima or a minima in y.
if(yAbs(i-1)<yAbs(i)&&yAbs(i+1)>yAbs(i))
plotPoint=yAbs(i); %If the value is a max, store it in plotPoint.
end
yMaxMin(i)=plotPoint; %Store the value of plotPoint into yMaxMin(i)
end
plot(t,yAbs);
hold on
plot(t,yMaxMin);
  2 件のコメント
Muhammad Sheraz Ali
Muhammad Sheraz Ali 2022 年 3 月 15 日
Question: "How do we find the maxima and minima of a continuous function ff on a closed and bounded interval [a,b]?[a,b]?" • Find the derivative 𝑓′(𝑥) of �
• Find all critical values of 𝑓, let's say 𝑐1, 𝑐2, …, 𝑐𝑘. • Compute 𝑓(𝑎) and 𝑓(𝑏), together with 𝑓(𝑐1),𝑓(𝑐2),…,𝑓(𝑐𝑘). • Compare (order) the values in step 3. • Then the minimum value is global minima of the values in step 4 and the maximum is the global maxima.
KADALI
KADALI 2023 年 1 月 3 日
find 1st and 2nd order derivatives matlab of f(X) = x^3+3xy^2-15x^2-15y^2+72x

サインインしてコメントする。

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 6 日
You can use findpeaks function
  1 件のコメント
Christopher
Christopher 2013 年 9 月 7 日
Thank you. I didn't realize that function existed. I rewrote my code and it's now functioning properly!

サインインしてコメントする。

その他の回答 (4 件)

Abhay Chaudhary
Abhay Chaudhary 2019 年 8 月 11 日
%This program plots the abs val of the maxima and minima of a function.
%This max/min value will then continue to be plotted until a new maxima or
%minima is found.
clear,clc;
t=[0:0.1:20];
y=exp(-t).*sin(pi./2.*t); %Our Function
yAbs=abs(y); %Take the absolute value of the function.
yMaxMin=zeros(201); %Create an array of zeros to be filled w/ data.
for(i=2:200)
%If a point is a maxima in yAbs, it will be a maxima or a minima in y.
if(yAbs(i-1)<yAbs(i)&&yAbs(i+1)>yAbs(i))
plotPoint=yAbs(i); %If the value is a max, store it in plotPoint.
end
yMaxMin(i)=plotPoint; %Store the value of plotPoint into yMaxMin(i)
end
plot(t,yAbs);
hold on
plot(t,yMaxMin);
  1 件のコメント
VEMIREDDY
VEMIREDDY 2022 年 12 月 2 日
Maxima and minima of f(x)=x^4-3x^2y^2+4x+y^4

サインインしてコメントする。


Jaideep Banoth
Jaideep Banoth 2021 年 2 月 24 日
if true
% code
end

Ruthika j
Ruthika j 2021 年 10 月 9 日
find the critical points maxima and maxima of the function f(x) = 2x^3-9x^2+12x-3

Dinesh C
Dinesh C 2021 年 12 月 2 日
x*4+y*4-x*2-x*2+1

カテゴリ

Help Center および File ExchangeFeature Detection and Extraction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by