0% found this document useful (0 votes)
11 views5 pages

NST File

Uploaded by

janhvilkw
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)
11 views5 pages

NST File

Uploaded by

janhvilkw
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/ 5

PROGRAM-13

WAP to implement Numerical Integration using Simpson’s 1/3 Rule.


Source Code:
function I = simpson13(f, a, b, n)
% Simpson's 1/3 rule for numerical integration
% Inputs:
% f: Function handle to be integrated
% a: Lower limit of integration
% b: Upper limit of integration
% n: Number of subintervals (must be even)
%
% Output:
% I: Approximate value of the integral
if mod(n, 2) ~= 0
error('n must be even');
end
h = (b - a) / n;
x = a:h:b;
y = f(x);
% Calculate the sum of even and odd terms efficiently
sum_even = sum(y(2:2:end-2));
sum_odd = sum(y(3:2:end-1));
% Apply Simpson's 1/3 rule
I = (h/3) * (y(1) + 4*sum_odd + 2*sum_even + y(end));
end
% Define the function f(x) = e^(x^3)
f = @(x) exp(x.^3);
% Set integration limits and step size
a = 1;
b = 2;
h = 0.25;
% Calculate the number of subintervals
n = (b - a) / h;
% Check if n is even
if mod(n, 2) ~= 0
error('n must be even');
end
% Apply Simpson's 1/3 rule
I = simpson13(f, a, b, n);
% Display the result
disp(['Approximate integral value: ', num2str(I)]);
Output:
PROGRAM-14
WAP to implement Numerical Integration using Simpson’s 3/8 Rule.
Source Code:
function I = simpson38(f, a, b, n)
% Simpson's 3/8 rule for numerical integration
%
% Inputs:
% f: Function handle to be integrated
% a: Lower limit of integration
% b: Upper limit of integration
% n: Number of subintervals (must be a multiple of 3)
%
% Output:
% I: Approximate value of the integral
if mod(n, 3) ~= 0
error('n must be a multiple of 3');
end
h = (b - a) / n;
x = a:h:b;
y = f(x);
% Calculate the sum of terms
sum = y(1) + y(end);
for i = 2:3:n-1
sum = sum + 2*y(i);
end
for i = 3:3:n-2
sum = sum + 3*y(i);
end
% Apply Simpson's 3/8 rule
I = (3*h/8) * sum;
end
% Define the function f(x) = 1/(1+x^2)
f = @(x) 1./(1+x.^2);
% Set integration limits and step size
a = 0;
b = 6;
h = 1; % Choose a step size that makes n a multiple of 3
% Calculate the number of subintervals
n = (b - a) / h;
% Check if n is a multiple of 3
if mod(n, 3) ~= 0
error('n must be a multiple of 3');
end
% Apply Simpson's 3/8 rule
I = simpson38(f, a, b, n);
% Display the result
disp(['Approximate integral value: ', num2str(I)]);

Output:
PROGRAM-15
WAP to implement Numerical Differentiation.
Source Code:
% Given data
x = [0.1, 0.2, 0.3, 0.4];
y = [0.9975, 0.9900, 0.9776, 0.9604];

% Calculate the forward difference at x = 0.1


h = x(2) - x(1); % Step size
dy_dx = (y(2) - y(1)) / h;

% Display the result


fprintf('The value of dy/dx at x = 0.1 is %.4f\n', dy_dx);

Output:

You might also like