Write a MATLAB function that attempts to locate the maximum and minimum values o
ID: 3753659 • Letter: W
Question
Write a MATLAB function that attempts to locate the maximum and minimum values of an arbitrary function, f(x), over a certain range. The function being evaluated should be passed to the function as a calling argument. Be sure to check that there are a valid number of input arguments and that the MATLAB "help" & "lookfor" commands are properly supported.
The function should have the following input arguments:
first_value—The first value of x to search
last_value—The last value of x to search
num_steps—The number of steps to include in the search
func—The name of the function to search
The function should have the following output arguments:
xmin—The value of x at which the minimum was found
min_value—The minimum value of found
xmax—The value of x at which the maximum was found
max_value—The maximum value found
(If you could please correctly code this hw problem & I will rate, thanks!)
Explanation / Answer
function [xmin,min_value,xmax,max_value]=find_min_max(first_value,last_value,num_steps,func)
% Input:
% first_value—The first value of x to search
% last_value—The last value of x to search
% num_steps—The number of steps to include in the search
% func—The name of the function to search
% Output:
% xmin—The value of x at which the minimum was found
% min_value—The minimum value of found
% xmax—The value of x at which the maximum was found
% max_value—The maximum value found
x=linspace(first_value,last_value,num_steps);
xmin=x(1);
xmax=xmin;
min_value=func(x(1));
max_value=min_value;
for i=x
if func(i)>max_value
max_value=func(i);
xmax=i;
end
if func(i)<min_value
min_value=func(i);
xmin=i;
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.