solve with MATLAB. Programming Part: For each programming problem, please submit
ID: 3282662 • Letter: S
Question
solve with MATLAB.
Programming Part: For each programming problem, please submit the code and the output by pasting them into a word document and attaching them to your homework. 1. (7.2 1d) Write a numerical code (or use the textbook code) that implements Composite Trapezoid and Composite Simpson Rule. Apply this code to approximate the integral J edz for (a) Composite Trapezoid for M = 10 and (b) Composite Simpson for M = 5·Which produces less error? (Hint: You can integrate by parts twice to find the exact answer).Explanation / Answer
function simps(a, b, n)
h = (b-a)/n;
sum0 = 0;
for i = 1:n/2-1
x(i) = a + 2*i*h;
sum0 = sum0 + f(x(i));
end
sum1 = 0;
for i = 1:n/2
x(i) = a + (2*i-1)*h;
sum1 = sum1 + f(x(i));
end
integral = h*(f(a)+ 2*sum0 + 4*sum1 +f(b))/3
function y = f(x)
y = (x^2)*exp(-x);
function trapez(a, b, n)
h = (b-a)/n;
sum = 0;
for i = 1:n-1
x(i) = a + i*h;
sum = sum + f(x(i));
end
integral = h*(f(a) + 2*sum + f(b))/2
function y = f(x)
y = (x^2)*exp(-x);
Outputs
Trapezoidal = 1.52163
Simpson =1.523793
Exact Ans = 1.523793
Simpsons rule gives less error
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.