Write a user-defined function, in MATLAB, with function call val=evalf(f,a,b) where f is an inline function, and a and b are constants such that a<b. The function calculates the midpoint m of the interval [a,b] and returns the value of (1/2)f(a)+(1/3)f(m)+(1/4)f(b). Execute the function for f(x)=e^(-x)*cos(2x), a=-1, b=3.
//Screenshot of the code:
//evalf.m
//main.m
//Sample Output:
//Code to copy:
//evalf.m
%Define a function evalf to evaluate the function f(x) = %e^(-x)*xos(2*x).
function value = evalf(f,a,b)
%Find the mid point of the a and b.
m = (a+b)/2;
%Calculate the value of the variable val. Function f is an %inline function and replace f(a), f(b), and f(m) with the %expression exp(-a)*cos(2*a), exp(-b)*cos(2*b), and %exp(-%mid)*cos(2*m).
value = (1/2).*f(a)+(1/3).*f(m)+(1/4).*f(b);
%End the function.
end
//main.m
%Make a function f(x) = e^-x*cos(2*x) inline.
f =inline('exp(-x).*cos(2*x)');
%Call the function evalf and pass the inline function, -1, %and 3 as arguments.
val = evalf(f,-1,3)
Get Answers For Free
Most questions answered within 1 hours.