MATLAB Part 2: Numerical Integral Save your file as integral.m Create a function
ID: 3712095 • Letter: M
Question
MATLAB
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 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(['In the integral using the left Riemann sum is num2str(left)]) fprintf(I In the integral using the right Riemann sum is num2str(right)]) fprintf(I In the integral using the trapezoid sum is num2str(trap) 'In'])Explanation / Answer
function R = myRightsum(x,y)
n = length(x);
R = 0;
for i = 1:n
R= R + y(i)*(x(i+1) - x(i));
end
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
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.