% Simpson's 1/3 Rule Numerical Integration
% Step 1: Define the function to be integrated
function y = myFunction(x)
% Define your function here
y = x.^2; % Example: x^2
end
% Step 2: Define the Simpson's 1/3 rule integration function
function result = simpsons13(a, b, n)
% a: Lower limit of integration
% b: Upper limit of integration
% n: Number of subintervals (should be even)
% Step 2a: Calculate the interval width
h = (b - a) / n;
% Step 2b: Calculate the function values at the end points
fa = myFunction(a);
fb = myFunction(b);
% Step 2c: Calculate the function values at the odd and even points
oddSum = 0;
evenSum = 0;
for i = 1:n-1
x = a + i * h;
if mod(i, 2) == 1
oddSum = oddSum + myFunction(x);
else
evenSum = evenSum + myFunction(x);
end
end
% Step 2d: Apply Simpson's 1/3 rule formula
result = (h / 3) * (fa + 4 * oddSum + 2 * evenSum + fb);
end
% Step 3: Provide the interval limits and number of subintervals
a = 0; % Lower limit
b = 2; % Upper limit
n = 4; % Number of subintervals (should be even)
% Step 4: Call the simpsons13 function and display the result
integralResult = simpsons13(a, b, n);
fprintf('The numerical integral using Simpson''s 1/3 rule is: %.4f\n', integralResult);
% Simpson's 1/3 Rule Numerical Integration
% Step 1: Define the function to be integrated
function y = myFunction(x)
% Define your function here
y = sin(x); % Example: sin(x)
end
% Step 2: Define the Simpson's 1/3 rule integration function
function result = simpsons13(a, b, n)
% a: Lower limit of integration
% b: Upper limit of integration
% n: Number of subintervals (should be even)
% Step 2a: Calculate the interval width
h = (b - a) / n;
% Step 2b: Calculate the function values at the end points
fa = myFunction(a);
fb = myFunction(b);
% Step 2c: Calculate the function values at the odd and even points
oddSum = 0;
evenSum = 0;
for i = 1:n-1
x = a + i * h;
if mod(i, 2) == 1
oddSum = oddSum + myFunction(x);
else
evenSum = evenSum + myFunction(x);
end
end
% Step 2d: Apply Simpson's 1/3 rule formula
result = (h / 3) * (fa + 4 * oddSum + 2 * evenSum + fb);
end
% Step 3: Provide the interval limits and number of subintervals
a = 0; % Lower limit
b = pi/2; % Upper limit
n = 4; % Number of subintervals (should be even)
% Step 4: Call the simpsons13 function and display the result
integralResult = simpsons13(a, b, n);
fprintf('The numerical integral using Simpson''s 1/3 rule is: %.4f\n', integralResult);
% Simpson's 1/3 Rule Numerical Integration:
% Step 1: Define the function to be integrated
function y = myFunction(x)
% Define your function here
y = x^3 + 2*x^2 + x; % Example: x^3 + 2x^2 + x
end
% Step 2: Define the Simpson's 1/3 rule integration function
function result = simpsons13(a, b, n)
% a: Lower limit of integration
% b: Upper limit of integration
% n: Number of subintervals (should be even)
% Step 2a: Calculate the interval width
h = (b - a) / n;
% Step 2b: Calculate the function values at the end points
fa = myFunction(a);
fb = myFunction(b);
% Step 2c: Calculate the function values at the odd and even points
oddSum = 0;
evenSum = 0;
for i = 1:n-1
x = a + i * h;
if mod(i, 2) == 1
oddSum = oddSum + myFunction(x);
else
evenSum = evenSum + myFunction(x);
end
end
% Step 2d: Apply Simpson's 1/3 rule formula
result = (h / 3) * (fa + 4 * oddSum + 2 * evenSum + fb);
end
% Step 3: Provide the interval limits and number of subintervals
a = 0; % Lower limit
b = 2; % Upper limit
n = 4; % Number of subintervals (should be even)
% Step 4: Call the simpsons13 function and display the result
integralResult = simpsons13(a, b, n);
fprintf('The numerical integral using Simpson''s 1/3 rule is: %.4f\n', integralResult);
The numerical integral using Simpson's 1/3 rule is: 8.0000