Sie sind auf Seite 1von 3

MEC 2456: Engineering Computational Analysis

% Name Xing Kai Tan % Student ID 24730106 % Date 16/8/2016 Tues

Recursive Trapezoidal Algorithm and Romberg integration

MEC2456 Engineering Computational Analysis Laboratory #3


% Name Xing Kai Tan % Student ID 24730106 % Date 16/8/2016 Tues
% Lab 3: Recursive Trapezoidal Rule
function [result,n]=RecurTrapzd(func, a, b, toler)
% func is the function file specified by the user.
% a: lower integral limit.
% b: upper integral limit.
% toler is the difference between the successive integrations.
% result is the estimated integral.
% n is the number of times that the integral (a,b) is subdivided.
%
% Finding out the TZero to start the recursive trapezoidal rule:
Tzero = (b-a)*0.5*(feval(func,a)+feval(func,b));
n = 1; % Setting initial number of the integral to be divided.
h = b-a; % Calculating the step size
% Using TZero to find TOne by adding the h_step to the a value.
Tzero = 0.5*(Tzero + h*feval(func,(a + 0.5*h)));
% The TOne is found.
TOne = Tzero;
Result = TOne;
% Finding out if the difference between the successive integral estimations
% is less than the tolerance value (1e-4). If not, keep on iterating using
while loop.
while abs(TOne-Tzero)>toler
% if abs value is > toler, reset the Tzero as the TOne.
Tzero = TOne;
% Increasing the amount to divide the (b-a).
n = n + 1;
% from the Recursive trap formula 2^(j-1).
tnm = 2^(n-1);
h = (b-a)/tnm;
x1 = a+(0.5*h);
% Figuring out the sum which was initially set to zero.
Sum = 0.0;
for Z = 1:tnm
% subsituting the x1 into function and sum
Sum = Sum + feval(func,x1);
% Increasing x1 by a step size
x1 = x1+h;
end
% new TZero value obatained
Tzero = 0.5*(Tzero+h*Sum);
TOne =Tzero; %T(1) is assigned with the new s value
Result(n+1)=TOne;
end

MEC 2456: Engineering Computational Analysis


% Name Xing Kai Tan % Student ID 24730106 % Date 16/8/2016 Tues

Recursive Trapezoidal Algorithm and Romberg integration

% Name Xing Kai Tan % Student ID 24730106 % Date 16/8/2016 Tues


% Lab 3: Romberg Integration
function [result,n]=RombergSimp(func, a, b, toler)
% function is the funcion file specifies by the user
% a is the lower integral limit.
% b is the upper integral limit.
% toler is the tolerance of difference between the successive integrations.
% Result is the estimated integral.
% n is the number of times that the interval (b,a) is subdivided.
% Using the recurring trapezoidal rule's Results:
[result,n] = RecurTrapzd(func, a, b, toler);
% First iteration.
tnm = 1;
% Begin the integration with an initial value.
s_diff = 1;
% values taken from recurring trapezoidal rule
romp(tnm) = (4/3)*(result(tnm+1))-(1/3)*(result(tnm));
% Integration estimation should be less than tolerance.
while s_diff > toler
tnm = tnm + 1;
romp = (4/3)*(result(tnm+1))-(1/3)*(result(tnm));
s_diff = abs(romp(tnm)-romp(tnm-1));
end
result = romp;
end

MEC 2456: Engineering Computational Analysis


% Name Xing Kai Tan % Student ID 24730106 % Date 16/8/2016 Tues

Recursive Trapezoidal Algorithm and Romberg integration

% Name Xing Kai Tan % Student ID 24730106 % Date 16/8/2016 Tues


clc;clear;
func = @(x)(sqrt(1-4*x^2));
% Calling the function.
[resultTrap,nTrap]= RecurTrapzd(func, -0.5, 0.5,10^-4);
[resultRomb,nRomb]= RombergSimp(func, -0.5, 0.5,10^-4);
% Displaying the answers.
disp(resultTrap,nTrap)
disp(resultRomb,nRomb)

from command Window:

RecurTrapzd [0.78537279, 10]


RombergSimp [0.78537013, 9]