0% found this document useful (0 votes)
33 views4 pages

MAT LAB SIMPSON

Uploaded by

Sathya Bhat
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)
33 views4 pages

MAT LAB SIMPSON

Uploaded by

Sathya Bhat
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/ 4

% 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy