0% found this document useful (0 votes)
13 views1 page

Simpson

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Simpson

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

% Simpson's 1/3 Rule

% Parameters that are used in Simpson's 1/3 Rule :

% func - The function handle to integrate (e.g., @(x) x^2)

% a, b - The limits of integration

%n - Number of subintervals (must be even)

f = @(x) x^2; % Define the function

%%%% initialization %%%%%%%%

a = 0; % Lower limit

b = 2; % Upper limit

n = 4; % Number of subintervals

if mod(n, 2) ~= 0

error('Number of subintervals (n) must be even.');

end

% Step size

h = (b - a) / n;

% Compute the sum of the function values

x = a:h:b; % Subdivision points

y = func(x); % Function values at these points

% Apply Simpson's 1/3 rule

integral = (h / 3) * (y(1) + 4 * sum(y(2:2:end-1)) + 2 * sum(y(3:2:end-2)) + y(end));

% Display the result

fprintf('The integral of the function over [%f, %f] is approximately %f\n', a, b, integral);

end

You might also like