0% found this document useful (0 votes)
3 views3 pages

CLC

The document contains MATLAB code for multiple application numerical integration methods, specifically Trapezoidal, Simpson's 1/3, and Simpson's 3/8 rules. It calculates the integral of a given polynomial function over the interval [0, 0.8] and compares the results with an exact value. The results, including errors, are printed in a table format and visualized in a graph.

Uploaded by

2022334055
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

CLC

The document contains MATLAB code for multiple application numerical integration methods, specifically Trapezoidal, Simpson's 1/3, and Simpson's 3/8 rules. It calculates the integral of a given polynomial function over the interval [0, 0.8] and compares the results with an exact value. The results, including errors, are printed in a table format and visualized in a graph.

Uploaded by

2022334055
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

clc

clear
close all
% Given Data
a = 0;
b = 0.8;
exact = 1.640533;
% Define function
f = @(x) 0.2 + 25*x - 200*x.^2 + 675*x.^3 - 900*x.^4 +
400*x.^5;
% Storage
N = 2:10;
trap_val = zeros(size(N));
s13_val = NaN(size(N));
s38_val = NaN(size(N));
fprintf('\nMultiple Application Numerical Integration\n');
fprintf('Exact Value = %.6f\n\n', exact);
fprintf('----------------------------------------------------------------------------------
---------------\n');
fprintf(' n h Trap I Err(%%) Simp1/3 I Err(%
%) Simp3/8 I Err(%%)\n');
fprintf('----------------------------------------------------------------------------------
---------------\n');
for k = 1:length(N)

n = N(k);
h = (b-a)/n;
x = a:h:b;

%% -------- Trapezoidal --------


I_trap = f(a) + f(b);
for i = 2:n
I_trap = I_trap + 2*f(x(i));
end
I_trap = (h/2)*I_trap;
err_trap = abs((exact - I_trap)/exact)*100;
trap_val(k) = I_trap;

%% -------- Simpson 1/3 --------


if mod(n,2)==0
I_s13 = f(a) + f(b);
for i = 2:n
if mod(i,2)==0
I_s13 = I_s13 + 4*f(x(i));
else
I_s13 = I_s13 + 2*f(x(i));
end
end
I_s13 = (h/3)*I_s13;
err_s13 = abs((exact - I_s13)/exact)*100;
s13_val(k) = I_s13;
else
I_s13 = NaN;
err_s13 = NaN;
end

%% -------- Simpson 3/8 --------


if mod(n,3)==0
I_s38 = f(a) + f(b);
for i = 2:n
if mod(i,3)==0
I_s38 = I_s38 + 2*f(x(i));
else
I_s38 = I_s38 + 3*f(x(i));
end
end
I_s38 = (3*h/8)*I_s38;
err_s38 = abs((exact - I_s38)/exact)*100;
s38_val(k) = I_s38;
else
I_s38 = NaN;
err_s38 = NaN;
end

%% Print Table Row


fprintf('%2d %7.4f %8.4f %6.2f %8.4f %6.2f %8.4f %
6.2f\n',...
n, h, I_trap, err_trap, I_s13, err_s13, I_s38, err_s38);
end
fprintf('----------------------------------------------------------------------------------
---------------\n');
%% ================= GRAPH
=================
figure
hold on
grid on
plot(N, trap_val,'bo-','LineWidth',1.5)
plot(N, s13_val,'rs-','LineWidth',1.5)
plot(N, s38_val,'md-','LineWidth',1.5)
% Exact value line (Old version compatible)
plot([min(N) max(N)], [exact exact],'k--','LineWidth',1.5)
xlabel('Number of Subintervals (n)')
ylabel('Integral Value')
title('Comparison of Numerical Integration Methods')
legend('Trapezoidal','Simpson 1/3','Simpson 3/8','Exact
Value','Location','best')
hold off

You might also like