Numerical Integration in Matlab

Dec 9, 2017; last modified: Dec 11, 2017 • Arne Vogel

The code for the summed newton cotes formulas in matlab. The functions compute the integral of a function f(x) in the same file from a to b with step size n. Use a sufficient high n for accurate results.

See the example for usage.

h

x is a vector of equidistant nodes.

Trapezoid rule

trapezoid rule

function t = trapezoid_rule(a,b,n)
  t = 0;
  x = linspace(a,b,n);
  h = x(2)-x(1);
  for i = 1:length(x)-1
	t = t + (h/2 * (f(x(i)) + f(x(i+1))));
  end
end

Simpson’s rule

simpson rule

function s = simpson_rule(a,b,n)
  s = 0;
  x = linspace(a,b,n);
  h = x(2)-x(1);
  for i = 1:length(x)-1
	s = s + (f(x(i)) + 4* f((x(i)+x(i+1))/2) + f(x(i+1)));
  end
  s = s * h/6;
end

3/8 rule

38

function r = rule38(a,b,n)
  r = 0;
  x = linspace(a,b,n);
  h = x(2)-x(1);
  for i = 1:length(x)-1
	r = r + ( f(x(i)) + 3* f(((x(i) + x(i+1))*(4/3)/3)) + 3* f(((x(i) + x(i+1))*(5/3))/3) + f(x(i+1)));
  end
  r = r * h/8;
end