Implement write a code the composite trapezoidal rule Tha bf

Implement (write a code) the composite trapezoidal rule T_h^[a, b][f] and the composite Simpson rule S_h^[a, b][f] to approximate the definite integral I^[a, b][f] = integral_a^b f(x) dx. Test your routines appropriately. Produce a table with the approximations T_h^[0, 1][e^-x^2] and S_h^[0, 1][e^-x^2] to I^[0, 1][e^-x^2] for h = 0.1, 0.05, 0.025, and 0.0125 and verify the order of convergence of each quadrature. Write a code to implement Romberg Algorithm to approximate (1). The code should use an estimate of the error to determine the number of levels (rows) in the Romberg Algorithm so that the error is less than a user provide tolerance tol. Test your code with integral_-1^1 e^x dx and tol = 10^-6, 10^-8, 10^-10.

Solution

Q1)

function integral = cmptrap(a,b,n,f)

h = (b-a)/n;

x = [a+h:h:b-h];

integral = h/2*(2*sum(feval(f,x))+feval(f,a)+feval(f,b));

Run with

cmptrap(1,2,4,\'f\')

where ’f’ is the name of the function definition file

function y = f(t)

y = t.*log(t); % pay attention to the dot

Matlab code for the Composite Simpson’s rule

function integral = cmpsimp(a,b,n,f)

h = (b-a)/n;

xi0 = feval(\'f\',a)+feval(\'f\',b);

xi1 = 0;

xi2 = 0;

for i = 1:n-1

x = a+i*h;

if mod(i,2) == 0

xi2 = xi2+feval(\'f\',x);

else

xi1 = xi1+feval(\'f\',x);

end

end

Next question 3)

I wrote the code to implement Romberg Algorithm to approximate (1)

 Implement (write a code) the composite trapezoidal rule T_h^[a, b][f] and the composite Simpson rule S_h^[a, b][f] to approximate the definite integral I^[a, b

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site