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

The trapezoid rule for numerical integration assumes that the data in between da

ID: 3569395 • Letter: T

Question

The trapezoid rule for numerical integration assumes that the data in between data points linearly rises or falls to the next value. This is the assumption that is generally accepted as the most basic, but sufficiently accurate. A general algorithm for the numerical integration by trapezoidal rule looks like this: f(x),function for which the integral is calculated n, number of intervals o, the lower bound b, the upper bound integ, the value of the integral Write a function my trap which receives (n+1) samples of an input function f(x) and calculates the integral by using trapezoidal rule. The input samples are given in a form of an input vector z (dimension n.1) and f (dimension (n+). Function my trap returns the value integ. which is the integral of the input function. Your function has to check the input arguments and provide the user with the error report if the function is not called according to the following rules: - the function is called with two arguments, - the input argument are row vectors of the same size.

Explanation / Answer

function [y]=mytrap(x,f)

if nargin~=2
disp('not enough arguments!!!')
y=-1;
else
l1=length(x);
l2=length(f);
if l1~=l2
disp('input arguments are not of same size')
y=-1;

else
integ=0;
for i=1:l1-1
del_x=x(i+1)-x(i);

integ=integ+del_x*0.5*(f(i)+f(i+1));

end
y=integ;
end
end
end