0% found this document useful (0 votes)
6 views6 pages

Numerical Integration

The document provides MATLAB code for numerical integration using three methods: the Trapezoidal Rule, Simpson’s 1/3 Rule, and Simpson’s 3/8 Rule. Each method includes input prompts for the function, limits, and intervals, as well as calculations and output of the integration results. The code also includes checks for valid input conditions for the number of intervals in Simpson's rules.

Uploaded by

heyho54290
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)
6 views6 pages

Numerical Integration

The document provides MATLAB code for numerical integration using three methods: the Trapezoidal Rule, Simpson’s 1/3 Rule, and Simpson’s 3/8 Rule. Each method includes input prompts for the function, limits, and intervals, as well as calculations and output of the integration results. The code also includes checks for valid input conditions for the number of intervals in Simpson's rules.

Uploaded by

heyho54290
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

NUMERICAL INTEGRATION

TRAPEZOIDAL RULE

clc;

clear;

% Input

f = input('Enter the function f(x): ');

N = input('Enter number of intervals: ');

a = input('Enter lower limit a: ');

b = input('Enter upper limit b: ');

% Step size

h = (b - a) / N;

% Generate x values

x = a:h:b;

% Evaluate function

fx = zeros(1, N+1);

for i = 1:N+1

fx(i) = f(x(i));

end

% Display table

disp(' x f(x)');

disp('---------------------------');
for i = 1:N+1

fprintf('%10.4f %10.4f\n', x(i), fx(i));

end

% Trapezoidal Rule calculation

sum = 0;

for i = 2:N

sum = sum + fx(i);

end

TR = (h/2) * (fx(1) + 2*sum + fx(N+1));

fprintf('\nResult using trapezoidal rule is: %.4f\n', TR);

SIMPSON’S 1/3 RULE

clc;

clear;

% Input

f = input('Enter the function f(x): ');

a = input('Enter the lower limit a: ');

b = input('Enter the upper limit b: ');

n = input('Enter the number of sub-intervals n: ');

% Check even n

if rem(n,2)==1
fprintf('\nEnter valid n!!!');

n = input('\nEnter n as an even number: ');

end

% Step size

h = (b - a)/n;

% x and f(x) table

x = zeros(1, n+1);

y = zeros(1, n+1);

x(1) = a;

y(1) = f(a);

for k = 2:n

x(k) = a + (k-1)*h;

y(k) = f(x(k));

end

x(n+1) = b;

y(n+1) = f(b);

% Display table

disp(' x f(x)');

disp('--------------------------------');

for k = 1:n+1

fprintf('%12.4f %12.4f\n', x(k), y(k));

end
% Simpson's 1/3 Rule calculation

so = 0; % sum of odd terms

se = 0; % sum of even terms

for k = 2:n

if rem(k,2)==0

so = so + y(k);

else

se = se + y(k);

end

end

I = (h/3)*(y(1) + y(n+1) + 4*so + 2*se);

fprintf('\nThe value of integration is I = %.4f\n', I);

SIMPSON’S 3/8 RULE

clc;

clear;

% Input

f = input('Enter the function f(x): ');

a = input('Enter the lower limit a: ');

b = input('Enter the upper limit b: ');

n = input('Enter the number of sub-intervals n: ');


% Check n is multiple of 3

if rem(n,3) ~= 0

fprintf('\nEnter valid n!!!');

n = input('\nEnter n as a multiple of 3: ');

end

% Step size

h = (b - a) / n;

% x and f(x) values

x = zeros(1, n+1);

y = zeros(1, n+1);

x(1) = a;

y(1) = f(a);

for k = 2:n

x(k) = a + (k-1)*h;

y(k) = f(x(k));

end

x(n+1) = b;

y(n+1) = f(b);

% Display table

disp(' x f(x)');

disp('--------------------------------');

for k = 1:n+1

fprintf('%12.4f %12.4f\n', x(k), y(k));


end

% Simpson's 3/8 Rule calculation

so = 0; % sum of terms not multiple of 3

sm3 = 0; % sum of terms multiple of 3

for k = 2:n

if rem(k-1,3) == 0

sm3 = sm3 + y(k);

else

so = so + y(k);

end

end

I = (3*h/8) * (y(1) + y(n+1) + 3*so + 2*sm3);

fprintf('\nThe value of integration is I = %.4f\n', I);

You might also like