Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2016-4-11
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.

回答(1 个)

Image Analyst
Image Analyst 2016-4-11
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.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by