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

Use your lecture notes to write the function for performing the trapezoid rule f

ID: 3824968 • Letter: U

Question

Use your lecture notes to write the function for performing the trapezoid rule for integration. You will need to write another function to test the trapezoid rule function. Write a function named fofx. The fofx-function will return values using the expression: y = f(x) = 1/squareroot 2pi exp(-1/2 x^2), function [yout] = fofx (xin) % input the x value % output the y value yout = ' f (x) expression goes here ' end You will pass the fofx function as an argument to the trapezoid function by preceding the fofx function name with the @ symbol, as in:... trapz (..., @fofx, ...), Where the @fofx goes in the argument list depends on how you wrote the function. The integral of f(x) defined above from -1 to 1 is approximately 0.68

Explanation / Answer

function I = trapezoidal(f_str, a, b, n)

I=0;
g = inline(f_str);
h = (b-a)/n;

I = I + g(a);
for ii = (a+h):h:(b-h)
    I = I + 2*g(ii);
end
I = I + g(b);
I = I*h/2;

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