Finding the peak value of a function
Mostrar comentarios más antiguos
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 comentario
KSSV
el 5 de Abr. de 2019
max function should work right?
Respuestas (1)
Star Strider
el 5 de Abr. de 2019
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.
Categorías
Más información sobre Descriptive Statistics en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!