Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

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

1 visualizzazione (ultimi 30 giorni)
jose sanchez
jose sanchez il 11 Apr 2016
Chiuso: MATLAB Answer Bot il 20 Ago 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 Commenti
Walter Roberson
Walter Roberson il 11 Apr 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.

Risposte (1)

Image Analyst
Image Analyst il 11 Apr 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.

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by