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

Lab 07 210021110

The document outlines an experiment involving the Trapezoidal method and Simpson’s 1/3rd rule for numerical integration. It includes MATLAB code modifications to prompt user input for limits and iterations, and to allow function input from the console. The discussion highlights that while the Trapezoidal method is simpler, Simpson’s method provides greater accuracy by utilizing more data points.

Uploaded by

Saadman Abeer
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)
9 views5 pages

Lab 07 210021110

The document outlines an experiment involving the Trapezoidal method and Simpson’s 1/3rd rule for numerical integration. It includes MATLAB code modifications to prompt user input for limits and iterations, and to allow function input from the console. The discussion highlights that while the Trapezoidal method is simpler, Simpson’s method provides greater accuracy by utilizing more data points.

Uploaded by

Saadman Abeer
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

Experiment Name: Trapezoidal method and Simpson’s 1/3rd rule method.

Theory:

Trapezoidal rule is a closed form integration method.

𝑏 𝑛−1

∫ 𝑓(𝑥) = 2
{𝑓(𝑥0) + 2 ∑ 𝑓(𝑥𝑖) + 𝑓(𝑥𝑛)}
𝑎 𝑖=1
Where,
𝑏−𝑎
ℎ= 𝑛

Simpson’s ⅓ rd rule

𝑏 𝑛 𝑛
△𝑥
∫ 𝑓(𝑥) = 3
{𝑓(𝑥0) + 4 ∑ 𝑓(𝑥𝑖) + 2 ∑ 𝑓(𝑥𝑖) + 𝑓(𝑥𝑛)}
𝑎 𝑖=3,5,7... 𝑖=2,4,6...

Where,
𝑏−𝑎
△𝑥 = 𝑛

Task :
●​ Modify the Matlab code in a way that it will prompt the user to provide the upper
limit, lower limit, number of iterations. Then it will print n, h, I and Er in a table
starting the value of n from 1, then increasing 1 every time upto max. number of
iterations inserted by the user.
●​ Now change the code and ask the user to insert the function from the console.

Code:
clear all
clc
n = input('Number of Iteration:'); % number iterations
intervalBegin = input('Lower Limit:'); % lower limit
intervalEnd = input('Upper Limit:'); % upper limit
exact_val = 1.640533; % Exact value
integral = 0.0;
dx = (intervalEnd-intervalBegin)/n; % step size
y = @(x) 0.2 + 25 * x - 200 * (x^2) + 675 * (x^3) - 900*(x^4) + 400 * (x^5);
%functionofy= input('Enter the function as a string: ', 's');
% y = str2func(['@(x) ' functionofy]);
integral = integral + (1/2) * y(intervalBegin);
integral = integral + (1/2) * y(intervalEnd);
for i = 1:(n-1)
integral = integral + y( intervalBegin + i*dx); %for the rest of data
end
result = integral * dx;
error = (exact_val - result) ./ exact_val;
T = table(i, dx, integral, error, ...
'VariableNames', {'n', 'h', 'I', 'E_r'});
disp(T);
fprintf ('Value of h is : %.3f\n',dx);
fprintf ('The final result is: %.4f \n', result);
fprintf ('Error: %.2f % \n', (error *100));

Output:

Task for the lab report:

Implement the Simpson’s 1/3rd rule in Matlab.


Using 4 iterations numerically integrate
f(x) = 0.2 + 25x - 200x2 + 675x3 -900x4+ 400x5 from x = 0 to 0.8. The exact
value is 1.640533

Code:

clear all
clc
n = input('Number of Iteration:'); % number iterations
intervalBegin = input('Lower Limit:'); % lower limit
intervalEnd = input('Upper Limit:'); % upper limit
exact_val = 1.640533; % Exact value
dx = (intervalEnd-intervalBegin)/n; % step size
y = @(x) 0.2 + 25 * x - 200 * (x^2) + 675 * (x^3) - 900*(x^4) + 400 * (x^5);
% Initialize with first and last terms
integral = (dx/3) * (y(intervalBegin) + y(intervalEnd));
% Add contributions from interior points
for i = 1:n-1
x_i = intervalBegin + i*dx;
if mod(i, 2) == 0
integral = integral + (dx/3) * 2 * y(x_i); % for even points
else
integral = integral + (dx/3) * 4 * y(x_i); % for odd points
end
end
% Calculate relative error
error = (exact_val - integral) / exact_val;
% Display results
T = table(n, dx, integral, error, ...
'VariableNames', {'n', 'h', 'I', 'E_r'});
disp(T);
fprintf('Value of h is: %.3f\n', dx);
fprintf('The final result is: %.4f\n', integral);
fprintf('Error: %.2f%%\n', (error * 100));

Output:
Discussion:

In the experiment , we have integrated the equation as Simpson’s ⅓ rd method , Trapezoidal


method . Trapezoidal method is easier to compute but error is higher than simpson’s method .
Simson’s ⅓ rd method uses more than two data points to fit the equation more accurately.

You might also like