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

I already have 5 and 6 taken care of i just need help with the code in number 7

ID: 2246879 • Letter: I

Question

I already have 5 and 6 taken care of i just need help with the code in number 7 . thank you

By hand, apply the trapezoid rule to obtain integral^2pi/3_0 (cos(x) - sin(x))dx, with n = 2 and n = 3. What are the actual errors and what are the error bounds for these values of n? Also indicate how large n has to be in order for the error bound to be no greater than 10^-6. Repeat the above question but use Simpson's rule. Write Matlab code for computing the integrals in the above two problems by both the trapezoid rule and Simpson's rule. Obtain the approximation for n = 2^k with k = 1, 2, ..., 11. Plot the log_10 of the error vs the log_10 of the number of steps for both rules. Comment on the slopes of the error lines.

Explanation / Answer

% Implementation of Midpoint, Trapezoidal and Simpson Rule.

clc
clear all;

a = 5;
b = 10;
c = 4;

Int_trp = zeros(1,100);
Int_simp = zeros(1,100);
for N = 1:100
  
  
% Trapezoidal Rule
dx = (b-a)/(N-1);
sum_trpz = 0;
for q = 1:N
X = a + ((q-1)*dx);
Y = sqrt(((X^2)-(c^2)))/X;
if (q == 0)||(q == N)
coeff = 0.5;
else
coeff = 1;
end
sum_trpz = sum_trpz + (coeff*Y*dx);
end
Int_trp(N) = sum_trpz;
  
% Simpson's Rule
sum_simp = 0;
for r = 1:N
X = a + ((r-1)*dx);
Y = sqrt(((X^2)-(c^2)))/X;
if (mod(r,2) == 0)
coeff = 4;
else
coeff = 2;
end
if (r == 0)||(r == N)
coeff = 1;
end
sum_simp = sum_simp + (coeff*Y*dx*(1/3));
end
Int_simp(N) = sum_simp;
end

ExtInt = ((sqrt(b^2 - c^2)) - (abs(c)*asec(b/c))) - ((sqrt(a^2 - c^2)) - (abs(c)*asec(a/c)));
icf = ExtInt*ones(1,100);
plot(Int_trp,'-r','linewidth',2);hold on;
plot(Int_simp,'-sm','linewidth',2);hold on;
plot(icf,'*k');hold on;
legend('Midpoint','Trapeziodal','Simpson','Real Value');
xlabel('Number of Interval->','fontsize',12);
ylabel ('Error Events ->','fontsize',12);
grid on;