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

See my Matlab code below. This deals with integral approximation using left rect

ID: 3422548 • Letter: S

Question

See my Matlab code below. This deals with integral approximation using left rectangle, right rectangle, trapezoid, and Simpson's approximations. The goal is to plot each approximation (R,L, T, and S) against each n value. But I'm having trouble making n, R, L, T, and S vectors.

ln = input('Enter the lower bound of n (must be an even number) ');

if mod(ln,2) == 0; %number is even
ln = ln;
else %number is odd
fprintf('Error: Input must be an even number ')
return
end

% Assures that upper bound of n is even

un = input('Enter the upper bound of n (must be an even number) ');

if mod(un,2) == 0;%number is even
un = un;
else %number is odd
fprintf('Error: Input must be an even number ')
return
end

for n = ln:2:un;
x = linspace(-4,4,n+1);
h = -8./n; % h = delta x; (b-a)/n = (-4-4)/n = -8/n
f = (2.*x +1/3).*cos(x.^2 + x/3);
  
% Create weight functions
  
wL = ones(1,n+1); %one row, n+1 columns
wL(n+1) = 0; %left sum doesn't use right endpoint
  
wR = ones(1,n+1);
wR(1) = 0; %right sum doesn't use left endpoint
  
wT = ones(1,n+1);
wT(2:n) = 2;
  
wS = ones(1,n+1);
wS(2:2:n) = 4;
wS(3:2:n-1) = 2;
  

% Compute the left and right rectangle approximation

L = 0;
R = 0;
for i = 0:n
L = L + f(i+1)*wL(i+1);
R = R + f(i+1)*wR(i+1);
end
L = h*L;
R = h*R;
  
  
% Compute the trapezoid approximation
  
T = 0;
for i = 0:n
T = T + f(i+1)*wT(i+1);
end
T = (h/2)*T;
  
  
% Compute the Simpson's approximation
  
S = 0;
for i = 0:n
S = S + f(i+1)*wS(i+1);
end
S = (h/3)*S;
end

Explanation / Answer

See my Matlab code below. This deals with integral approximation using left rect