Hello, I really need some help with Matlab. Here\'s the problem: Write a functio
ID: 2079127 • Letter: H
Question
Hello, I really need some help with Matlab.
Here's the problem:
Write a function m file that will take a function f, a vector x, and a calculation type calcType as inputs. It should return either the minimum value of f(), the maximum value of f(), or the average value of f(), over the interval of values defined by x, depending on the value of calcType. You can choose what calcType must be for each case (you can use numbers, words, abbreviations, etc.). Make sure you document your function well so that help processFunc explains clearly what it does and how to use it.
Use as your function definition:
function [output] = processFunc(f, x, calcType)
Write a script m file, which will call your function with the values for f and x in the table below, and confirm you get the indicated values for the minimum, maximum, and average:
And here's the table:
Please help!
function (f) x Minimum Maximum Average f = @(x)sin(x) linspace(pi/4,pi/2) .7071 1 .8998 f = @(x)x.^3-4*x.^2+2 linspace(-4,6) -126 74 -9.5017 f = @(x) x.^3-4*x.^2+2 linspace(-2,2) -22 1.9984 -3.4411Explanation / Answer
function [output] = processFunc(f,x,calcType)
%%
% processFunction will take a function f, a vector x, and a calculation type
% calcType as inputs. It should return either the minimum value of f(),
% the maximum value of f(), or the average value of f(), over the interval
% of values defined by x, depending on the value of calcType.
%
% f = expression of the function as a string; Ex: 'sin(x)'
% x = expression for x as a string; Ex: 'linspace(pi/4,pi/2)'
% calcType = is the integer to calculate Min, Max or Average of function
% if the calcType = 0 then the function returns minimum of f() and
% if the calcType = 1 then the function returns maximum of f() and
% if the calcType = 2 then the function returns Average of f()
%
%
eval(strcat('x=',x,';')); % defining the values for x
eval(strcat('fun=',f,';')); % calculating the function values for x
switch calcType
case 0
output=min(fun); % computing the Minimum value of the function
case 1
output=max(fun); % computing the Maximum value of the function
case 2
output=mean(fun); % computing the Average value of the function
end
end
%% Run the below commands to verify the mfile code results
% [output]=processFunc('sin(x)','linspace(pi/4,pi/2)',0)
% [output]=processFunc('sin(x)','linspace(pi/4,pi/2)',1)
% [output]=processFunc('sin(x)','linspace(pi/4,pi/2)',2)
% [output]=processFunc('x.^3-4*x.^2+2','linspace(-4,6)',0)
% [output]=processFunc('x.^3-4*x.^2+2','linspace(-4,6)',1)
% [output]=processFunc('x.^3-4*x.^2+2','linspace(-4,6)',2)
% [output]=processFunc('x.^3-4*x.^2+2','linspace(-2,2)',0)
% [output]=processFunc('x.^3-4*x.^2+2','linspace(-2,2)',1)
% [output]=processFunc('x.^3-4*x.^2+2','linspace(-2,2)',1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.