Part 2: Numerical Integral Save your file as integral.m Create a function with t
ID: 1732553 • Letter: P
Question
Part 2: Numerical Integral Save your file as integral.m Create a function with the headerline function integral(x, y) Where x and y are vectors and x has a step size of h Your function should numerically estimate the integral from a to b of y (where a is the first value in the x-vector and b is the last value) using the following methods: Rectangular using the left hand value Rectangular using the right hand value -Trapezoidal You may not use any of MATLAB's built in functions for integrals, but you may use the sum() function If x is a vector, sum(x) will sum all of the entries in the vector. Your function should print out your solutions using the lines of code fprintf(['n the integral using the left Riemann sum is num2str(left)]) fprintf(['n the integral using the right Riemann sum is num2str(right)]) fprintf(['n the integral using the trapezoid sum is ' num2str(trap) 'In'1) Test vour function: yexp(-x.2); >> integral(x,y); the integral using the left Riemann sum is 0.83623 the integral using the right Riemann sum is 0.93623 the integral using the trapezoid sum is 0.88623Explanation / Answer
function r=myleftsum(f,a,b,n)
dx=(b-a)/n;
r=f(a);
for k=1:n-1
c=a+k*dx;
'r=r+f(c);
end
r=dx*r;
fprintf('the integral using the Left reiman sum is %d',L);
end
function R = myRightsum(x,y)
n = length(x);
R = 0;
for i = 1:n
R= R + y(i)*(x(i+1) - x(i));
end
fprintf('the integral using the right reiman sum is %d',R);
end
function T = mytrap(x,y)
n = length(x);
T = 0;
for i = 1:n-1
T = T+ .5*(y(i)+y(i+1))*(x(i+1) - x(i));
end
fprintf('the integral using the trapezoidal sum is %d',T);
end