Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

I'm having trouble writing this function can anyone help?

1 visualización (últimos 30 días)
jose sanchez
jose sanchez el 11 de Abr. de 2016
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Chapter 15 #5
Mathematically speaking, a critical point occurs when the derivative of a function equals zero. It is possible that a local minimum or a local maximum occurs at a critical point. A local minimum is a point where the function's value to the left and right of it are larger, and a local maximum is a point where the function's value to the left and right are smaller.
Write a function that finds the local minimum and local maximum points. Call the function findPoints. It should take in a vector of x values and a vector of y values and return two vectors:
the first vector should contain the x values where the minimum points occur
the second vector should contain the x values where the maximum points occur. You may assume that the local minimums and maximums occur at integer values. This will take care of any decimals created when using diff.
Example:
x = linspace(-5, 5, 1000); y = x.^3 -12*x; [min max] = findPoints(x, y) should return min = 2 max = -2
  4 comentarios
Walter Roberson
Walter Roberson el 11 de Abr. de 2016
It is firmly recommended that you never name a variable "min" or "max" as doing so interferes with using the MATLAB routines of those names, and makes it more difficult for other people to read the code.

Respuestas (1)

Image Analyst
Image Analyst el 11 de Abr. de 2016
Try
for k = 2 : length(y)-1
if y(k) > y(k-1) && y(k) > y(k+1)
% It's a local max
elseif y(k) < y(k-1) && y(k) < y(k+1)
% It's a local min
end
end
See if you can finish the rest of the code to store the index k as a max or min, and to get the x values there.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by