Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Function function m-file program to compute the minimum of values of a f

ID: 3791223 • Letter: W

Question

Write a Function function m-file program to compute the minimum of values of a function over a range. The function (equation/formula) itself should be passed along with a range (lower bound and upper bound) and number of equally-spaced points for generation of independent variable vector. You are NOT to use the MATLAB pre-defined function min or the like. You must program to calculate the minimum using if-else, for and while structures, if necessary Please include MATLAB commands you will use to test your program as if in the command window for the function f(x) = x-3)^2 - 7 for x range from -10 to 10 for 10000 points. % Funcmin: min function value % function fmin = funcmin (f, a, b, n) % compute minimum of values % of function over a range % input: % f = function to be evaluated % a = lower bound of range % b = upper bound of range % n =number of equally-spaced % value points % output: % fmin=minimum of values of function.

Explanation / Answer

% matlab code

function fmin = funcmin(f,a,b,n)
% linearly spaced vector
x = linspace(a,b,n);
fmin = f(x(1));
minIndex = 1;
for i=2:length(x)
if f(x(i)) < fmin
fmin = f(x(i));
index = i;
end
end
fprintf("Min value: %f at index %d ", fmin, index);
end

fmin = funcmin(@(x)((x-3)^2)-7,-10,10,1000);
% output: Min value: -6.999951 at index 650

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote