Finding the peak value of a function
显示 更早的评论
I have written this clumsy code for finding the peak value of a function. But its not giving me an output cuz of the interval steps of 0.01 I guess.
x=0;
f=[];
i=1;
while x<4
f(i)=2*sin(x).*exp(-x/4);
i=i+1;
x=x+0.01;
end
peak=max(f)
peakhalf=peak/2
i=1;
x=0;
while x<4
if f(i)==peakhalf
disp(x)
end
i=i+1;
x=x+0.01;
end
How else do I find the peak vlaue of the function?
回答(1 个)
Star Strider
2019-4-5
You have the correct idea, however it’s because ‘peakhalf’ may not corresond exactly to any elements of ‘f’.
You have to include a tolerance (‘tol’ here) to locate the closest ‘x’ coordinate of ‘peakhalf’.
A slight re-write to include that (and a few other slight improvements):
x=0;
f=[];
i=1;
while x < 4
f(i)=2*sin(x).*exp(-x/4);
x=x+0.01;
xv(i) = x;
i=i+1;
end
peak=max(f)
peakhalf=peak/2
i=1;
x=0;
tol = 0.01;
while (i <= numel(f)) && (x<4)
if abs(f(i)-peakhalf) <= tol
disp(xv(i))
end
i=i+1;
xp=x+0.01;
end
Experiment to get the result you want.
类别
在 帮助中心 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!